Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Lisp (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Lambda expressions and function definition=== Another special operator, {{Lisp2|lambda}}, is used to bind variables to values which are then evaluated within an expression. This operator is also used to create functions: the arguments to {{Lisp2|lambda}} are a list of arguments, and the expression or expressions to which the function evaluates (the returned value is the value of the last expression that is evaluated). The expression <syntaxhighlight lang="Lisp"> (lambda (arg) (+ arg 1)) </syntaxhighlight> evaluates to a function that, when applied, takes one argument, binds it to {{Lisp2|arg}} and returns the number one greater than that argument. Lambda expressions are treated no differently from named functions; they are invoked the same way. Therefore, the expression <syntaxhighlight lang="Lisp"> ((lambda (arg) (+ arg 1)) 5) </syntaxhighlight> evaluates to {{Lisp2|6}}. Here, we're doing a function application: we execute the [[anonymous function]] by passing to it the value 5. Named functions are created by storing a lambda expression in a symbol using the defun macro. <syntaxhighlight lang="Lisp"> (defun foo (a b c d) (+ a b c d)) </syntaxhighlight> {{Lisp2|(defun f (a) b...)}} defines a new function named {{Lisp2|f}} in the global environment. It is conceptually similar to the expression: <syntaxhighlight lang="Lisp"> (setf (fdefinition 'f) #'(lambda (a) (block f b...))) </syntaxhighlight> where {{Lisp2|setf}} is a macro used to set the value of the first argument {{Lisp2|fdefinition 'f}} to a new function object. {{Lisp2|fdefinition}} is a global function definition for the function named {{Lisp2|f}}. {{Lisp2|#'}} is an abbreviation for {{Lisp2|function}} special operator, returning a function object.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)