相关疑难解决方法(0)

在堆栈使用效率和时间方面哪个函数最好

我写了3个函数来计算a-element出现在a-list中的次数.我尝试了各种输入并对其进行了分析,但我仍然不知道哪种功能在堆栈使用效率和时间效率方面最佳.请帮帮我.

;; Using an accumulator
    (defn count-instances1 [a-list an-element]
      (letfn [(count-aux [list-aux acc]
                         (cond
                           (empty? list-aux) acc
                           :else (if (= (first list-aux) an-element)  
                                   (count-aux (rest list-aux) (inc acc))
                                   (count-aux (rest list-aux) acc))))]
        (count-aux a-list 0)))

;; Normal counting 
    (defn count-instances2 [a-list an-element]
     (cond
       (empty? a-list) 0
       :else
          (if (= (first a-list) an-element )
              (+ 1 (count-instances2 (rest a-list) an-element))
              (count-instances2 (rest a-list) an-element))))

;; using loop. does this help at all?
   (defn count-instances3 [a-list an-element]
        (loop [mylist a-list acount 0] …
Run Code Online (Sandbox Code Playgroud)

lisp functional-programming clojure

6
推荐指数
1
解决办法
401
查看次数

用于"让"匹配Clojure的Common Lisp宏

Clojure的let比Common Lisp更简洁,括号更少:

 ;Clojure
(let [a 1 b 2]
     (+ a b))


;Common Lisp
(let ( (a 1) (b 2))
    (+ a b))
Run Code Online (Sandbox Code Playgroud)

你如何在Common Lisp中编写一个宏来等效?:

(letmac ( a 1 b 2)
    (+ a b))
Run Code Online (Sandbox Code Playgroud)

macros clojure common-lisp

3
推荐指数
1
解决办法
160
查看次数