我试图理解Lisp-1和Lisp-2之间的区别以及它与Clojure的关系,但我仍然不理解.任何人都可以开导我吗?
你们对Clojure有什么看法?我正在考虑接下来学习它,目前正在使用Erlang并且除了记录惨败之外总体上对它很满意...... Clojure和LISP一样强大吗?
我想print在变量中存储一个函数,这样我就可以输入一些简短的函数p,例如:
In Scheme:
(define print display)
(print "Hello world\n")
;; alternate way
(define print 'display)
((eval print) "Hello world\n")
Run Code Online (Sandbox Code Playgroud)
同样的方法似乎不起作用Common Lisp:
(defvar p 'print)
;;(print (type-of p))
(p "Hello world") ;; Attempt 1
((eval p) "Hello world") ;; >> Attempt 2
((eval (environment) p) "Hello world") ;; Attempt 3
Run Code Online (Sandbox Code Playgroud)
我Attempt 1上面得到这个错误:
*** - EVAL: undefined function P
Run Code Online (Sandbox Code Playgroud)
而这与Attempt 2和3在Clisp:
*** - EVAL: (EVAL (ENVIRONMENT) P) …Run Code Online (Sandbox Code Playgroud) 我想实现filter基于条件过滤列表的函数
(defun filter (func xs)
(mapcan
(lambda (x)
(when (func x) (list x))) xs ))
Run Code Online (Sandbox Code Playgroud)
但是我收到一个错误:
*** - EVAL: undefined function FUNC
Run Code Online (Sandbox Code Playgroud)
我认为lambda应该看到func.如何传递func到lambda正确的?
我使用CLISP.
我有以下 lisp 代码
(defun sum (vec)
"Summiert alle Elemente eines Vektors."
(apply '+ vec))
(defun square (item)
"Hilfsfunktion zum Quadrieren eines Elements."
(* item item))
(defun calcVarianz (vec)
"Berechnet die Varianz eines Vektors."
(loop with len = (length vec)
with mean = (/ (sum vec) len)
with some_func = (lambda (x) (* x x))
; causes the error
for item in vec
collecting (square (- item mean)) into squared
collecting (some_func item) into some_vector
; some_func cannot be found
finally …Run Code Online (Sandbox Code Playgroud)