繁体   English   中英

如何在Clojure中使用expt函数?

[英]How to use expt function in Clojure?

我试图根据这个答案使用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)

我想我需要把Leiningen的依赖信息放在这里解释

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

在我的project.clj ,我做到了,但我仍然得到同样的错误。 我究竟做错了什么?


编辑

在这个答案中,我去了我的项目目录并做了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中得到同样的错误。


EDIT2

根据Vidya的回答,我试图使用石榴,但没有成功。 这是我试过的。 我究竟做错了什么:

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> 

以下是与以下内容进行比较的正确配置项目的示例:

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)) 

REPL:

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

一般而言,使用大多数clojure库并不比添加依赖项和向命名空间添加:require标记(或者如果您愿意, :use标记)更难。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM