Clojure def vs defn对于没有参数的函数 [英] Clojure def vs defn for a function with no arguments

查看:121
本文介绍了Clojure def vs defn对于没有参数的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在clojure中编写了一个程序,但是一些函数没有参数。将这样的函数编码为def而不是defn而没有参数的好处是什么?

I have written a program in clojure but some of the functions have no arguments. What would be the advantages of coding such functions as a "def" instead of a "defn" with no arguments?

推荐答案

code> def s只计算一次,而 defn s(有或无参数) 。因此,如果你的函数总是返回相同的值,你可以改变它们到 def s但不是其他。

defs are evaluated only once whereas defns (with or without arguments) are evaluated (executed) every time they are called. So if your functions always return the same value, you can change them to defs but not otherwise.

这篇关于Clojure def vs defn对于没有参数的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆