开发者

scheme2lisp::define function and pass it as parameter

开发者 https://www.devze.com 2023-01-01 16:29 出处:网络
I need to translate some code f开发者_JS百科rom Scheme to Common Lisp.Now, I have something like this:

I need to translate some code f开发者_JS百科rom Scheme to Common Lisp. Now, I have something like this:

(defun sum (term a next b)
  (if (> a b)
    0
    (+ (term a) (sum term (next a) b))))

(defun sum-int (a b)
  (defun (ident x) x)
  (sum ident a 1+ b))

but it produces errors.

*** - DEFUN: the name of a function must be a symbol, not (IDENT X)

Help me plese. Thanks

upd original code:

(define (sum term a next b)
  (if (> a b)
    0
    (+ (term a) (sum term (next a) b))))

(define (sum-int a b)
  (defun (identity x) x)
  (define identity a 1+ b))


(defun sum (term a next b)
  (if (> a b)
      0
      (+ (funcall term a) (sum term (funcall next a) next b))))

(defun sum-int (a b)
  (flet ((ident (x) x))
   (sum #'ident a #'1+ b)))

Just another CL take with FLET (untested).


I think I got the gist of what you were looking for...

(defun sum (term a next b)
  (if (> a b)
      0
      (+ (funcall term a) (sum term (funcall next a) next b))))

(defun ident (x) x)

(defun sum-int (a b)
  (sum #'ident a #'1+ b))

Or more CLish, without explicitly defuning ident:

(defun sum-int (a b)
  (sum (lambda (x) x) a #'1+ b))

You need #' quoting to get a function object since CL has separate namespaces for functions (defined with defun) and variables.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号