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!
== Examples == Here are examples of Common Lisp code. The basic "[[Hello, World!]]" program: <syntaxhighlight lang="Lisp"> (print "Hello, World!") </syntaxhighlight> Lisp syntax lends itself naturally to recursion. Mathematical problems such as the enumeration of recursively defined sets are simple to express in this notation. For example, to evaluate a number's [[factorial]]: <syntaxhighlight lang="Lisp"> (defun factorial (n) (if (zerop n) 1 (* n (factorial (1- n))))) </syntaxhighlight> An alternative implementation takes less stack space than the previous version if the underlying Lisp system optimizes [[tail recursion]]: <syntaxhighlight lang="Lisp"> (defun factorial (n &optional (acc 1)) (if (zerop n) acc (factorial (1- n) (* acc n)))) </syntaxhighlight> Contrast the examples above with an iterative version which uses [[Common Lisp]]'s {{Lisp2|loop}} macro: <syntaxhighlight lang="Lisp"> (defun factorial (n) (loop for i from 1 to n for fac = 1 then (* fac i) finally (return fac))) </syntaxhighlight> The following function reverses a list. (Lisp's built-in ''reverse'' function does the same thing.) <syntaxhighlight lang="Lisp"> (defun -reverse (list) (let ((return-value)) (dolist (e list) (push e return-value)) return-value)) </syntaxhighlight>
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)