This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

on function...

3 posts · 2671 views

HI
It seems that I still have some doubt on ‘function’ operator..
For example some code that I’m learning on the book (and also on the web)use it with lambda:

In compose definition:
(defun compose (&rest fns)
      (destructuring-bind (fnl . rest) (reverse fns)
          #'(lambda (&rest args)
                 (reduce #'(lambda (v f) (funcall f v))
                    rest  :initial-value (apply fnl args)))))
but that code works also without function in front of lambda …of course CL interpreter to know that lambda is a function and not a variable…
it’s the same also for reduce, apply, setf,..ecc
Have I misunderstood something? (maybe yes.... :? )
Thanks in AV

Re: on function...

LAMBDA is a macro that expands into (FUNCTION (LAMBDA ...)), so in CL they are equivalent.

In some other contexts, like with many standard higher order functions, you can use both (FUNCTION function-name) (or #'function-name syntax) and (QUOTE function-name) (or 'function-name syntax), but they behave differently. The former retrieves the function at read time from lexical bindings function space, whereas the latter at runtime by name from dynamic bindings function space. It manifests when, for example, using lexical function bindings:
CL-USER> (defun test () 'test)
TEST
CL-USER> (test)
TEST
CL-USER> (funcall #'test)
TEST
CL-USER> (funcall 'test)
TEST
CL-USER> (flet ((test () 'test2)) (funcall 'test))
TEST
CL-USER> (flet ((test () 'test2)) (funcall #'test))
TEST2

Re: on function...

Thank Ramarren for explanation !! ;)