【问题标题】:How to use expt function in Clojure?如何在 Clojure 中使用 expt 函数?
【发布时间】:2013-11-21 03:18:52
【问题描述】:

我正在尝试根据 this answer 使用 expt 函数,但是当我尝试在 REPL 中执行 (use 'clojure.math.numeric-tower) 时出现错误

user> (use 'clojure.math.numeric-tower)
(use 'clojure.math.numeric-tower)FileNotFoundException Could not locate clojure/math/numeric_tower__init.class or clojure/math/numeric_tower.clj on classpath:   clojure.lang.RT.load (RT.java:443)

我认为我需要按照 here 的说明输入 Leiningen 依赖信息

[org.clojure/math.numeric-tower "0.0.2"]

在我的project.clj 中,我这样做了,但我仍然遇到同样的错误。我做错了什么?


编辑

As in this answer我去了我的项目目录,做了lein deps

a@b:~/command-line-args$ lein deps
Retrieving org/clojure/math.numeric-tower/0.0.2/math.numeric-tower-0.0.2.pom from central
Retrieving org/clojure/math.numeric-tower/0.0.2/math.numeric-tower-0.0.2.jar from central
a@b:~/command-line-args$ 

但我在 REPL 中仍然遇到同样的错误。


编辑2

根据 Vidya 的回答,我正在尝试使用 Pomegranate 但没有成功。这是我尝试过的。我做错了什么:

user> (use '[cemerick.pomegranate :only (add-dependencies)])
nil
user> (add-dependencies :coordinate '[[org.clojure/math.numeric-tower "0.0.2"]]
                        :repositories (merge cemerick.pomegranate.aether/maven-central
                                             {"clojars" "http://clojars.org/repo"}))
{}
user> (require '(numeric-tower core stats charts))
FileNotFoundException Could not locate numeric_tower/core__init.class or numeric_tower/core.clj on classpath:   clojure.lang.RT.load (RT.java:443)
user> (require 'clojure.contrib.math)
FileNotFoundException Could not locate clojure/contrib/math__init.class or clojure/contrib/math.clj on classpath:   clojure.lang.RT.load (RT.java:443)
user> 

【问题讨论】:

  • 您在更改部门后是否重新启动了您的 repl? deps 仅在启动时发现
  • 一切看起来都很好。您是否也重新启动了 REPL?
  • 是的,我重新启动了 REPL。
  • 我在我的项目目录中做了lein deps,但我仍然在 REPL 中遇到同样的错误(请参阅我的问题的编辑)。
  • 你是否从项目目录执行了lein repl?

标签: clojure dependencies leiningen read-eval-print-loop


【解决方案1】:

这是一个正确配置的项目示例,可与之进行比较:

project.clj:

(defproject math-example "0.1.0-SNAPSHOT"                            
  :description "FIXME: write description"                            
  :url "http://example.com/FIXME"                                    
  :license {:name "Eclipse Public License"                           
            :url "http://www.eclipse.org/legal/epl-v10.html"}        
  :dependencies [[org.clojure/clojure "1.5.1"]                       
                 [org.clojure/math.numeric-tower "0.0.2"]]) 

src/math_example/core.clj:

(ns math-example.core                                                
  (:require [clojure.math.numeric-tower :as math]))                  

(def x (math/expt 2 10)) 

回复:

math-example.core> (math/expt 2 10)                                  
1024                                                                 
math-example.core> x                                                 
1024                                                                 
math-example.core> 

一般来说,使用大多数 clojure 库应该不会比添加依赖项并向命名空间添加 :require 标记(或者,如果您愿意,还可以添加 :use 标记)更难。

【讨论】:

  • 太好了,谢谢。这行得通。我唯一不明白的是#<Namespace math-example.core>。当我输入时,我得到一个错误。我在这里使用了clojure-doc.org/articles/tutorials/emacs.html#using-the-repl 的说明来获取 REPL 的命名空间
  • 当我从 emacs 中按 ctrl-c alt-n 以将 repl 切换到该命名空间时打印的。我实际上并没有输入它。等价的应该是(in-ns 'math-example.core)。我已将其从答案中删除。
猜你喜欢
  • 1970-01-01
  • 2014-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-08
  • 1970-01-01
相关资源
最近更新 更多