Mapcan, sharp quote and closure

I am somewhat new to the CL and is currently trying to wrap my head around mapcan, #', funcalland closing. Here is a closure that applies the predicate to n and, if correct, returns (list n), else nil:

(defun all-those (predicate)
  (lambda (n)
    (if (funcall predicate n) (list n))))

I understand what I need to call funcallto turn this closure into a function. This works great:

>  (funcall (all-those #'evenp) 8)
(8)

Now I tried to pass the function created here as an argument to mapcan:

>  (mapcan #'(funcall (all-those #'evenp)) '(1 2 3 4))

I get a compile-time error: (FUNCALL (ALL-THOSE #'EVENP)) is not a legal function name.

But it works if I lower #'as wellfuncall:

>  (mapcan (all-those #'evenp) '(1 2 3 4))
(2 4)

. , mapcan, (*), funcall, " ".

, #' funcall ? .


(*) , , . mapcan, : (mapcan #'(lambda ...

+4
2

mapcar, funcall .. , . , . , .

all-the . , (mapcan (all-them & hellip;) & hellip;) .

(# ') . # 'foo ( foo):

function - .

, flet, , . .

-, . , ,

, # ' . (, # 'car), - (, #' ( (x) x)). , ( ):

#'(funcall (all-those #'evenp))

. , mapcan (*) funcall, " ".

mapcar .. , :

function --- , , .

:

. ; : ( , ), ( ). undefined, , . . .

, mapcar, funcall .., :

(mapcan (all-those …) …)

:

(mapcan (lambda (x) ...) ...)
(mapcan #'(lambda (x) ...) ...)
(mapcan 'car ...)
(mapcan #'car ...)
(mapcan (symbol-function 'car) ...)
+5

(function foo) - , . foo. .

(funcall foo) , , - foo. funcall = FUNTION CALL = . .

, n , , ( n), else nil:

(defun all-those (predicate)
  (lambda (n)
    (if (funcall predicate n) (list n))))

, . all-those , .

? #'all-those
#<Compiled-function ALL-THOSE #x302000C9631F>

? (all-those #'evenp)
#<COMPILED-LEXICAL-CLOSURE (:INTERNAL ALL-THOSE) #x302000E5ECFF>

, funcall, .

- .

? (functionp (all-those #'evenp))
T

: . .

- . .

, . (function (lambda () ())) , . , , CL , .

, Common Lisp , , .

, funcall, .

funcall ( , ) .

, :

  • named: (foo bar baz)

  • : (foo bar bar)

  • : (funcall 'foo bar baz)

  • : (funcall foo bar baz)

  • ( ): (funcall #'foo bar baz)

  • : (funcall (function (lambda (foo) foo)) 'bar) (funcall #'(lambda (foo) foo) 'bar) (funcall (lambda (foo) foo) 'bar)

  • : ((lambda (foo) foo) 'bar)

APPLY, funcall, .

(apply #'+ 1 2 3 (list 4 5 6))  is similar to  (funcall #'+ 1 2 3 4 5 6)

FUNCALL - . . . funcall ( , .

- . . . FUNCTION - . FUNCTION , ( , ), -.

? (defun foo (bar) (list bar 'baz))
FOO
? (function foo)        ; a function object from the global function
#<Compiled-function FOO #x302000CC0D1F>
? #'foo                 ; the same, differently written
#<Compiled-function FOO #x302000CC0D1F>
? (funcall #'foo 42)    ; calling the function object
(42 BAZ)
? (funcall 'foo 42)     ; calling the symbol-function of the symbol
(42 BAZ)
? (funcall (symbol-function 'foo) 42)    ; as above
(42 BAZ)
? (flet ((foo (arg) (list arg :foo)))   ; a local lexical function
    (list (foo 43)                      ; calling the local lexical function
          (funcall #'foo 42)            ; calling a function object,
                                        ; from the local lexical function
          (funcall 'foo 41)))           ; calling the symbol-function of the symbol
((43 :FOO) (42 :FOO) (41 BAZ))
+4

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


All Articles