4clojure第135个问题:中缀计算器

来源:互联网 发布:局域网远程关机软件 编辑:程序博客网 时间:2024/06/05 11:43
(ns for-clojure.problem135)(defn infix-calculator  "前缀计算机,从左到右执行,不论优先级"  [& xs]  (loop [r (first xs)         xs (rest xs)]    (if (empty? xs)      r      (recur ((first xs) r (second xs)) (nthrest xs 2)))))(= 72 (infix-calculator 20 / 2 + 2 + 4 + 8 - 6 - 10 * 9)) 
(nthrest xs n)返回序列xs第n个元素后的子序列,所以 (rest (rest xs))等同于(nthrest xs 2)
原创粉丝点击