简体   繁体   中英

How to get rid of funcall in common lisp

According to this document: http://cl-cookbook.sourceforge.net/functions.html

(defun adder (n)
  (lambda (x) (+ x n)))
(funcall (adder 12) 1)

I have to use funcall to call (adder 12), And it is very ignoring to write funcall over and over, is there any way to write code like it in scheme:

((adder 12) 1)

No. There is none.

You can also see it as a feature: it makes calls of function objects explicit and improves understandability of source code.

However, you could use something like this (not sure why would you, but the number of characters typed would be the same as it is in Scheme):

(set-macro-character
 #\[
 #'(lambda (stream char)
     (declare (ignore char))
     (set-syntax-from-char #\] #\;)
     (let ((forms (read-delimited-list #\] stream t)))
       (set-syntax-from-char #\] #\x)
       (append '(funcall) forms))))

(defun adder (n)
  #'(lambda (x) (+ x n)))

(format t "sum: ~s~&" [(adder 12) #x128]) ;; 308

This may give you some problems if you will encounter a variable name with brackets in it. Sure, using it is up to you, consider yourself warned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM