(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)
4clojure第135个问题:中缀计算器
最新推荐文章于 2024-06-08 09:30:55 发布