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
Eval
(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!
=== Lisp === [[Lisp programming language|Lisp]] was the original language to make use of an <code>eval</code> function in 1958. In fact, definition of the <code>eval</code> function led to the first implementation of the language interpreter.<ref name="mccarthy">[https://www-formal.stanford.edu/jmc/history/lisp/node3.html John McCarthy, "History of Lisp - The Implementation of Lisp"]</ref> Before the <code>eval</code> function was defined, Lisp functions were manually compiled to [[assembly language]] statements. However, once the <code>eval</code> function had been manually compiled it was then used as part of a simple [[read-eval-print loop]] which formed the basis of the first Lisp interpreter. Later versions of the Lisp <code>eval</code> function have also been implemented as compilers. The <code>eval</code> function in Lisp expects a form to be evaluated as its argument. The resulting value of the given form will be the returned value of the call to <code>eval</code>. This is an example Lisp code: <syntaxhighlight lang="lisp"> ; A form which calls the + function with 1,2 and 3 as arguments. ; It returns 6. (+ 1 2 3) ; In Lisp any form is meant to be evaluated, therefore ; the call to + was performed. ; We can prevent Lisp from performing evaluation ; of a form by prefixing it with "'", for example: (setq form1 '(+ 1 2 3)) ; Now form1 contains a form that can be used by eval, for ; example: (eval form1) ; eval evaluated (+ 1 2 3) and returned 6. </syntaxhighlight> Lisp is well known to be very flexible and so is the <code>eval</code> function. For example, to evaluate the content of a string, the string would first have to be converted into a Lisp form using the <code>read-from-string</code> function and then the resulting form would have to be passed to <code>eval</code>: <syntaxhighlight lang="lisp">(eval (read-from-string "(format t \"Hello World!!!~%\")"))</syntaxhighlight> One major point of confusion is the question, in which context the symbols in the form will be evaluated. In the above example, <code>form1</code> contains the symbol <code>+</code>. Evaluation of this symbol must yield the function for addition to make the example work as intended. Thus some dialects of Lisp allow an additional parameter for <code>eval</code> to specify the context of evaluation (similar to the optional arguments to Python's <code>eval</code> function - see below). An example in the [[Scheme (programming language)|Scheme]] dialect of Lisp (R<sup>5</sup>RS and later): <syntaxhighlight lang="scheme"> ;; Define some simple form as in the above example. (define form2 '(+ 5 2)) ;Value: form2 ;; Evaluate the form within the initial context. ;; A context for evaluation is called an "environment" in Scheme slang. (eval form2 user-initial-environment) ;Value: 7 ;; Confuse the initial environment, so that + will be ;; a name for the subtraction function. (environment-define user-initial-environment '+ -) ;Value: + ;; Evaluate the form again. ;; Notice that the returned value has changed. (eval form2 user-initial-environment) ;Value: 3 </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)