假设我们定义如下函数,Clojure中的运算符优先级是什么?
(defn leap-year? [input-year] (or (and (= (rem input-year 4) 0) (> (rem input-year 100) 0)) (= (rem input-year 400) 0)))
user2864740.. 10
S表达式明确具有优先级和关联性,因为它们代表树.
像许多LISP方言一样,Clojure以"大量刺激的多余括号"直截了当地揭露了这些:
在计算中,s表达式,sexprs或sexps(用于"符号表达式")是嵌套列表(树形结构)数据的符号,由编程语言Lisp发明并推广,它将它们用于源代码和数据.在通常的带括号的Lisp语法中,s表达式归类地定义为
原子,或
形式(x.y)的表达式,其中x和y是s-表达式.
就优先级和关联性而言,"运算符"与任意函数调用(或宏)没有区别.也就是说,Clojure代码有效地启动了一个抽象语法树,并且该形式(+ a b)
本质上不同于(fn a b)
- +
令牌,如fn
或add
,只是生成的S表达式中的原子.
格式化代码应该更多地显示树结构(并且可以扩展此格式,直到一行只包含一个原子和0..n括号):
(defn leap-year? [input-year] (or (and (= (rem input-year 4) 0) (> (rem input-year 100) 0)) (= (rem input-year 400) 0)))
当源和扩展形式还是 S-表达式,and
并or
为宏.执行and
是:
(defmacro and "Evaluates exprs one at a time, from left to right. If a form returns logical false (nil or false), and returns that value and doesn't evaluate any of the other expressions, otherwise it returns the value of the last expr. (and) returns true." {:added "1.0"} ([] true) ([x] x) ([x & next] `(let [and# ~x] (if and# (and ~@next) and#))))
这允许(and ..)
(通过宏的递归膨胀),但是并没有允许"或"生产作为表单术语是已经由外S-表达式树建立的.
此外,从实现中可以看出,逻辑条件形式也像许多其他流行语言一样从左到右进行延迟评估.
S表达式明确具有优先级和关联性,因为它们代表树.
像许多LISP方言一样,Clojure以"大量刺激的多余括号"直截了当地揭露了这些:
在计算中,s表达式,sexprs或sexps(用于"符号表达式")是嵌套列表(树形结构)数据的符号,由编程语言Lisp发明并推广,它将它们用于源代码和数据.在通常的带括号的Lisp语法中,s表达式归类地定义为
原子,或
形式(x.y)的表达式,其中x和y是s-表达式.
就优先级和关联性而言,"运算符"与任意函数调用(或宏)没有区别.也就是说,Clojure代码有效地启动了一个抽象语法树,并且该形式(+ a b)
本质上不同于(fn a b)
- +
令牌,如fn
或add
,只是生成的S表达式中的原子.
格式化代码应该更多地显示树结构(并且可以扩展此格式,直到一行只包含一个原子和0..n括号):
(defn leap-year? [input-year] (or (and (= (rem input-year 4) 0) (> (rem input-year 100) 0)) (= (rem input-year 400) 0)))
当源和扩展形式还是 S-表达式,and
并or
为宏.执行and
是:
(defmacro and "Evaluates exprs one at a time, from left to right. If a form returns logical false (nil or false), and returns that value and doesn't evaluate any of the other expressions, otherwise it returns the value of the last expr. (and) returns true." {:added "1.0"} ([] true) ([x] x) ([x & next] `(let [and# ~x] (if and# (and ~@next) and#))))
这允许(and ..)
(通过宏的递归膨胀),但是并没有允许"或"生产作为表单术语是已经由外S-表达式树建立的.
此外,从实现中可以看出,逻辑条件形式也像许多其他流行语言一样从左到右进行延迟评估.
在Clojure中没有运算符优先级.所有功能从左到右和从里到外进行评估,所有功能都在宏扩展阶段之后完成.