2013-04-10 41 views
4

我想在Elisp中使用闭包来实现类Haskell的高阶函数。Emacs lisp高阶函数支持

;;; -*- lexical-binding: t -*- 
(defun foo (pair) 
    (car pair)) 
(defun* .curry (fn) 
    (lambda (x y &rest args) (apply fn (cons x y) args))) 

((lambda (x y) (1+ x)) 2 3) 
((lambda (&rest args) (apply (.curry #'foo) args)) 2 3) 
(funcall (.curry #'foo) 2 3) 
((.curry #'foo) 2 3) 

问题是最后一行返回错误Invalid function。所以,似乎封闭不认为理智的功能。我仍然可以在mapc中使用(.curry #'foo),但不能挂钩。 我能做些什么吗?

+0

将'.curry'重写为宏?这是主意!感谢所有这些信息! – KAction 2013-04-11 05:07:54

回答

2

您的((.curry #'foo) 2 3)在我所知的任何Lisp中除非Scheme(以及Scheme等衍生产品,如Racket)无效。说明问题的最简单代码如下:

(defun f() (lambda())) 
(funcall (f)) ; tested in Emacs 23 and CLISP, works 
((f)) ; tested in Emacs 23 and CLISP, results in an error