函数和数据有什么区别?

在函数式编程中,我们倾向于区分数据和函数,但有什么区别呢?

如果我考虑一个常量,我可以把它看作一个函数,它只返回相同的值:

(def x 5)

那么数据和函数有什么区别呢?我看不出区别。

回答

数据

函数(又名“代码”)

    • 两个函数永远不会相等,即使它们彼此重复。

评估


后记

我突然想到原来的问题还没有完全回答。考虑以下:

; A Clojure Var pointing to the value 5
(def five 5)
; A Clojure Var pointing to a function that always returns the value 5
(def ->five  (fn [& args] 5))

然后使用这两个变量:

; A Clojure Var pointing to the value 5
(def five 5)
; A Clojure Var pointing to a function that always returns the value 5
(def ->five  (fn [& args] 5))

括号使一切变得不同。

也可以看看:

  • 勇敢的Clojure
  • LispCast

以上是函数和数据有什么区别?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>