Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Lisp (programming language)
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Lisp (programming language)
(section)
Add topic