我试图在 clojure 中对一个列表取幂。我编写了以下函数(基于建议):
(defn myfunc [x n] (map #(Math/exp % n) x))
Run Code Online (Sandbox Code Playgroud)
但是当输入 x = (list 'a 'b) 和 n = 2 时,(list 'a 'b 'a 'b)它不会抛出##NaN ##NaN
任何建议表示赞赏。我想我仍然缺少函数式编程语言中涉及的一些抽象。
您好,我无法解决这个问题:
(defn integrate
"Finding the definite integral from 0 to stop"
([f dx]
(let [itg (memoize
(fn [itg stop n]
(if (<= n 0)
0
(+ (let [b (* n dx) a (- b dx)]
(println "[DEBUG] stop = " stop " and n =" n)
(* (- b a) (/ (+ (f a) (f b)) 2))
)
(itg itg stop (dec n))))))
itg (partial itg itg)]
(fn [x] (itg x (quot x dx))))))
(time ((integrate (fn [x] (* x …Run Code Online (Sandbox Code Playgroud) 我有一个字符串:
{:id 1, :name "Ab Bc Cd", :sex "M", :birth "01.01.1999", :address "Street 1", :oms "0001"}
Run Code Online (Sandbox Code Playgroud)
我需要将它转换为 HashMap 但是
(hash-map my-str)
Run Code Online (Sandbox Code Playgroud)
返回 {} 所以我把它分开了:
(s/split my-str ",")
Run Code Online (Sandbox Code Playgroud)
它返回
[:id 1 :name "Ab Bc Cd" :sex "M" :birth "01.01.1999" :address "Street 1" :oms "0001"]
Run Code Online (Sandbox Code Playgroud)
然后
for [x my-arr]
(hash-map x)
Run Code Online (Sandbox Code Playgroud)
返回
({} {} {} {} {} {})
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我是 clojure 的新手我试图找出 clojure 中的向量是否具有连续元素:在 python 中使用 numpy 很简单
(np.diff(np.sort(np.array(numbers))))
但是我在寻找类似的方法时迷失了方向:我的策略是
例如
(def x `(5 7 3 6 4))
输出将是 (1 1 1 1)
我很困惑如何解决这个问题。
我已经使用 Clojure 玩了一段时间,我陷入了一些我认为对许多人来说非常微不足道的事情......但不是我。我有以下代码;
;; Define a Record structure
(defrecord Person [first-name last-name age occupation])
(def john (->Person "John" "Frusciante" 50 "Guitarist"))
;; People map
(def people {"1" john
"2" (->Person "Pablo" "Neruda" 90 "Poet")
"3" (->Person "Stefan" "Zweig" 120 "Author")
}
)
(defn get-120-year-old-guy
[peeps]
(filter #(= (:age %) 120) peeps)
)
(println "who's 120?: " (get-120-year-old-guy people))
Run Code Online (Sandbox Code Playgroud)
此调用返回一个空列表。我知道我检索值的方式有问题,但看不到那到底是什么。
我刚开始学习Clojure,我习惯于使用其他函数式语言创建一些具有类似功能的管道
val result = filter(something)
map(something)
reduce(something)
collection
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用 Clojure 结合两个filter功能来实现类似的功能
(defn filter-1 [array] (filter
(fn [word] (or (= word "politrons") (= word "hello"))) array))
(defn filter-2 [array] (filter
(fn [word] (= word "politrons")) array))
(def result (filter-1 ["hello" "politrons" "welcome" "to" "functional" "lisp" ""]))
(println "First:" result)
(println "Pipeline:" ((filter-2 result)))
Run Code Online (Sandbox Code Playgroud)
但我无法让它发挥作用。
您能否提供一些有关如何将两个predicate功能组合在一起的建议或文档collection?
问候
在这个过程中,我正在学习 Clojure 并解决 SICP 书中的练习。评估(search-for-primes 1001 10000)时出现此错误,但当调用较小的值时,(search-for-primes 101 1000)它工作正常。
这似乎是一个内存问题,但我无法将其归零。很想得到你的帮助。谢谢你。下面是代码。
(defn square [x]
(* x x))
(defn divides? [a b]
(= (rem b a) 0))
(defn find-divisor [n test-divisor]
(cond
(> (square test-divisor) n) n
(divides? test-divisor n) test-divisor
:else (find-divisor n (+ test-divisor 1))))
(defn smallest-divisor [n]
(find-divisor n 2))
;;return true if prime
(defn prime?
[n]
(= n (smallest-divisor n)))
;;return the first of the three consecutive prime numbers
(defn search-for-primes [low high]
(if …Run Code Online (Sandbox Code Playgroud) 有没有更好的方法在 clojure 中实现嵌套循环?
作为初学者,我编写了这段嵌套循环代码,用于比较以天为单位的日期之间的差异。
将此与 Java 中使用 for 或 while 的嵌套循环进行比较。
(def my-vector [{:course-type "clojure"
:start-date "2021-01-25"
:end-date "2021-02-06"}
{:course-type "r"
:start-date "2021-01-15"
:end-date "2021-02-06"}
{:course-type "python"
:start-date "2020-12-05"
:end-date "2021-01-05"}
{:course-type "java"
:start-date "2020-09-15"
:end-date "2020-10-20"}
])
(defn find-gap-in-course [mycourses]
(println "Finding gap between dates....")
(loop [[course1 & mycourses] mycourses]
(loop [[course2 & mycourses] mycourses]
(when (and
(and (not-empty course1) (not-empty course2))
(> (-> java.time.temporal.ChronoUnit/DAYS
(.between
(LocalDate/parse (course2 :end-date))
(LocalDate/parse (course1 :start-date)))) 30))
(println "Dates evaluated are =" (course2 …Run Code Online (Sandbox Code Playgroud) 我现在正在学习 Clojure,我来自命令式编程,所以我在理解该代码的执行流程时遇到了问题:
(defn chop-chop [coll]
(let [x (partition-by identity coll)]
(map list (map (comp str first) x)
(map count x))))
=> (chop-chop "aaaabbbcca")
=> (("a" 4) ("b" 3) ("c" 2) ("a" 1))
Run Code Online (Sandbox Code Playgroud)
我明白我们:
在此处创建带有参数 coll 的函数chop-chop
然后我们为 coll 应用函数 partition-by,但我不确定身份是什么
接下来我们将之前操作的结果绑定到 x
但我无法理解下一行代码:
(map list (map (comp str first) x)
(map count x))))
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释该程序的逐步执行流程吗?非常感谢!
我正在尝试使用 Clojure 对一组特定的数据进行建模
一开始,我希望那段复杂的数据只是一个空集合。在这一点上,我不在乎它是列表、集合还是向量。
(ns clojure-playground.core
(:gen-class))
(def my-complex-object (hash-set))
Run Code Online (Sandbox Code Playgroud)
当我编译这个文件并进入(my-complex-object)REPL 时,出现错误。
同
(def my-complex-object ()),(def my-complex-object []),(def my-complex-object (list)), 和(def my-complex-object '(list)).如何绑定my-complex-object到空集合?