codex/site-src/lisp.wtn

76 lines
2.1 KiB
Plaintext

* Lisp
> [Lisp] has assisted a number of our most gifted fellow humans in thinking
> previously impossible thoughts.
_- Edsger Dijkstra_
Lisp is a family of {programming-languages} which uses s-expressions for
expressing both source code and data. Its syntax is fully parenthesized and
features prefix notation.
Some dialects of Lisp include Common Lisp, Scheme and Clojure.
** Prefix notation
A Lisp form consists of an operator followed by zero or more operands.
``(operator operand1 operand2 ...)``
The operator will be evaluated first, then the operands. Finally, the form will
evaluate to the result of applying the operands to the operator. This is the
basic evaluation model for Lisp.
** Lisp-1 vs Lisp-2
Dialects of Lisp generally fall into one of two categories, Lisp-1 or
Lisp-2. The difference between them is the number of namespaces they have. A
Lisp-1 shares the same namespace for variables and functions, while a Lisp-2 has
separate namespaces for each. Scheme is an example of a Lisp-1 and Common Lisp
is an example of a Lisp-2. Lisp-1's tend to be more elegant since they can share
the assignment operators for both.
** Macros
Since Lisp dialects are generally homoiconic, they lend themselves particularly
well to macro systems. Invocations of macros are replaced at compile time with
another Lisp form.
An example in Common Lisp:
``
;; Example definition of let using a lambda for creating a local context
(defmacro my-let (bindings &body body)
(let ((vars (mapcar #'car bindings))
(vals (mapcar #'cadr bindings)))
`((lambda ,vars ,@body) ,@vals)))
;; Example invocation:
(my-let ((a 1)
(b 2))
(+ a b))
;; This is expanded at compile time to the following form:
((lambda (a b)
(+ a b))
1 2)
``
** Other
Follows is an implementation of the average function in Scheme. Because of the
homoiconicity of the language, it is rather elegant in my opinion.
``
(define average
(lambda xs
(/ (apply + xs)
(length xs))))
``
{https://tildegit.org/opfez/lisp-hacks Various Common Lisp hacks}
{http://www.phyast.pitt.edu/~micheles/syntax-rules.pdf JRM's syntax-rules primer
for the merely eccentric}