【发布时间】:2020-12-02 17:06:56
【问题描述】:
在下面的代码中,我尝试使用 (conj listvalue countString)
在列表中添加一个元素(defn unique-character [parms]
(let [split (#(str/split % #"") parms)
countString (count split)
listvalue #{}]
(dbg (doseq [countString split]
(println countString)
(conj listvalue countString)
(println listvalue)
))listvalue))
(unique-character "Leeeeeerrroyyy")
Output to be - Leroy
但我得到一个空列表作为输出结果
有人可以帮我为什么这个角色没有被添加到列表中,也许这不是好的代码,但我想了解 conj 在doseq 中的行为方式
【问题讨论】:
-
Clojure 使用 immutalbe 数据结构,您不能将
conj放到一个列表中并修改该列表。你必须掌握结果。 -
请参阅此示例项目中的文档列表。学习“Brave Clojure”和“Getting Clojure”等书籍。 github.com/io-tupelo/clj-template#documentation
-
手册 (clojure.github.io/clojure/clojure.core-api.html) 解决了您关于“为什么...不”的具体问题。至于要做什么,@AlanThompson 给出了一个很好的阅读清单。第 1 步是,您不需要编写整个函数然后想知道为什么它不起作用! Clojure 比这更容易。打开一个 REPL 并自行尝试 conj,然后循环尝试...
标签: clojure