возврат лямбды функционируют в clisp, затем оценка его

У меня нет определенного ограничения по времени на фиксацию, я склонен фиксировать, как только тест передал, и я доволен кодом. Я wouldn; t код фиксации, который не компилирует или находится иначе в состоянии, что я не чувствовал бы себя хорошо о возвращении к в случае отказа

5
задан Pillsy 3 December 2009 в 13:51
поделиться

4 ответа

Вам нужна функция FUNCALL :

* (defun foo () (lambda () 42))
FOO
* (funcall (foo))
42
11
ответ дан 18 December 2019 в 07:29
поделиться

Ваша функция foo возвращает функцию . Используйте функцию funcall , чтобы применить функцию к аргументам, даже если набор аргументов пуст.

Здесь вы можете видеть, что foo возвращает значение типа function :

CL-USER> (let ((f (foo)))
           (type-of f))
FUNCTION
CL-USER> (let ((f (foo)))
           (funcall f))
42
CL-USER> (type-of (foo))
FUNCTION
CL-USER> (funcall (foo))
42
2
ответ дан 18 December 2019 в 07:29
поделиться

Другой вариант, помимо FUNCALL, - APPLY:

(apply (foo) nil)

FUNCALL - идиоматический способ здесь, но вам понадобится APPLY, когда у вас есть список параметров .

2
ответ дан 18 December 2019 в 07:29
поделиться

The relevant terms here are "Lisp-1" and "Lisp-2".

Your attempt at calling would work in a Lisp-1 like e.g. Scheme or Clojure. Common Lisp is a Lisp-2 however which roughly means that variable names and function names are separate.

So, in order to call the function bound to a variable you either need to use the special forms funcall or apply as others have pointed out or set the function value of the symbol foo rather than the variable value.

The former basically takes the variable value of the symbol, assumes/checks that value is a function and then calls the function (with whatever arguments you passed to funcall/apply.

You don't really want to do the latter as that is quite silly in all but very specialised cases, but for completness sake this is roughly how you'd do it:

CL-USER> (setf (symbol-function 'foo) (lambda () 42))
#<FUNCTION (LAMBDA ()) {C43DCFD}>
CL-USER> (foo)
42

You should also want to look into the labels and flet special forms (which are commonly used) - there you actually do use the latter methods (these forms create a temporary function binding for symbols).

So your problem would there look like this:

(flet ((foo () 
         42))
  (foo))

i.e. here you temporarily bind the function value of the symbol foo to the function returning 42. Within that temporary context you can then call (foo) like regular global functions.

6
ответ дан 18 December 2019 в 07:29
поделиться