Lecture overview -- Keyboard shortcut: 'u'  Previous page: Scheme in Scheme -- Keyboard shortcut: 'p'    Lecture notes - all slides and notes together  slide -- Keyboard shortcut: 't'  Textbook -- Keyboard shortcut: 'v'  Help page about these notes  Alphabetic index  Course home  Lecture 6 - Page 22 : 22
Functional Programming in Scheme
Linguistic abstraction
The eval and apply primitives

The implementation primitive eval of a Lisp systems is typically made available in the language, hereby providing access to evaluation of syntactical expressions (lists) in a given environment

The apply primitive is also available as a convenient mechanism for application of a function, in cases where all the parameters are available in a list

Expression

Value

(let* ((ttl "My Document")
       (bdy (list 'p "A paragraph"))
       (doc
        (list 'html
         (list 'head
          (list 'title ttl))
         (list 'body bdy)))
      ) 
 (render (eval doc)))
<html>
 <head>
  <title>My Document</title>
 </head>
 <body>
  <p>A paragraph</p>
 </body>
</html>
(let* ((ttl "My Document")
       (bdy (list 'p "A paragraph"))
       (doc
        `(html 
           (head (title ,ttl))
           (body ,bdy))))
  (render (eval doc)))
<html>
 <head>
  <title>My Document</title>
 </head>
 <body>
  <p>A paragraph</p>
 </body>
</html>
(+ 1 2 3 4)
10
(+ (list 1 2 3 4))
Error: + expects argument of type number; 
 given (1 2 3 4)
(apply + (list 1 2 3 4))
10

An illustration of eval and apply. In the first two rows we construct a list structure of the usual html , head , title , and body HTML mirror functions. In the first row, the list structure is made by the list function. In the second row, we use the convenient backquote (semiquote) facility. In both cases we get the same result. The last three rows illustrate the use of apply . apply is handy in the cases where the parameters of a function is already organized in a list. What it interesting in our current context, however, is that apply is really an implementation primitive of Scheme, which is made available in the language itself.