Why prohibit a functional object in a function position (but allow lambda forms)?

lambda form expression in function position

The lambda expression in the function position compiles just fine:

> ((lambda (n) (> n 10)) 42)
T

Closure built from lambda in functional position

On the other hand:

> (defun greater-than (x)
     (lambda (n) (> n x)))
GREATER-THAN

> ((greater-than 10) 42)
Compile-time error:  illegal function call

does not work.

(I need to call FUNCALLfor it to work: (funcall (greater-than 10) 42)=> T.

Why is this design?

I understand why SYMBOL with the object functions as a reference value should not work, for example (let ((foo (lambda () 42))) (foo)). Separate namespaces and all that.

But why forbid the function object itself in the function position? What was the rationale for this decision?

+4
source share
3

-?

- :

Lisp a form. - , , -. , - . : lambda, (lambda ...) (function (lambda ...)) - , function.

(                       ; the whole list is a valid lambda form

 (lambda (n) (> n 10))   ; a lambda expression, not a form

 42)                     ; 42 is a form, too

:

(let ((n 42))
  (> n 10))

? ?

Lisp ( ) .

(sin 3)
((lambda (x) (sin 3)) 3)
(unknown-function 3)

: , . . DEFUN, FLET , (, ,...).

, :

((if (> foo bar) 42 #'sin) 3)

(> foo bar) (sin 3) (42 3), , .

Lisp FUNCALL, APPLY, MULTIPLE-VALUE-CALL . , / . , , .

. Gabriel/Pitman.

+7

, " ", .. , , . . .

Lambda - ; , . , /.

, , #', #^, "" - , , "" - #'. funcall:

(#^(make-adder 5) 3) & equiv; (funcall (make-adder 5) 3)

. , funcall, , .

+6

Common Lisp . , CL , , . CL.

, .

, , , ( , , , 70- ), funcall even . , , funcall, , Common Lisp , .

, (+ 2 3) , (funcall #'+ 2 3), , . , funcall, , . , ,

((if (some-fun ..) 
     fun1 
     fun2) 
 3 
 4)

.., , , :

(if (some-fun ..)
    (fun1 3 4)
    (fun2 3 4))

CL , :

(funcall (if (some-fun ..) 
             #'fun1
             #'fun2) 
         3 
         4)

, - . , , , .

+4

Source: https://habr.com/ru/post/1681996/


All Articles