gpt4 book ai didi

clojure - 是否可以分解 Clojure 函数?

转载 作者:行者123 更新时间:2023-12-04 13:11:37 25 4
gpt4 key购买 nike

虽然我可能会错误地解释同质性的概念,但我已经将其理解为“代码即数据”。

所以,我可以写这样的代码:

(def subject "world")
(def helo '(str "Hello " subject))

此时, helo只是数据,但可以作为如下代码执行:
(eval helo)

返回“Hello world”。

我也可以继续治疗 helo作为数据:
(first helo)
(count helo)

分别返回 str3 .

到现在为止还挺好。但是,一旦我将代码包装在一个函数中,我似乎就失去了将代码视为数据的能力:
(defn helofn [subject]
(str "Hello " subject))

我如何分解 helofn ?看来我不能把它当作数据;如果我这样做:
(count helofn)

我得到一个异常(exception):

java.lang.UnsupportedOperationException: count not supported on this type: user$helofn



有没有其他分解方法 helofn ,或者我只是对同质性期望过高?

最佳答案

helofn定义是数据,但你让它被评估(就像你明确评估 helo 列表一样)。如果您以与 helo 相同的方式处理定义,那么它将保留数据,并适合您要应用的任何转换:

(def helofndata '(defn helofn [subject]
(str "Hello " subject))

=> (second helofndata)
helofn
=> (eval helofndata)
#'user/helofn

关于clojure - 是否可以分解 Clojure 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16718148/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com