Lecture overview -- Keyboard shortcut: 'u'  Previous page: Classes and objects in Scheme [Section] -- Keyboard shortcut: 'p'  Next page: Classes and objects -- Keyboard shortcut: 'n'  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 8 - Page 4 : 11
Functional Programming in Scheme
Object-oriented programming in Scheme
Functions with private context

It is possible to define a private context around a function

(define function-with-private-context
 (let (CONTEXT)
   (lambda (PARAMETERS)
      BODY)))

A template of a function with private context. The lambda expression appears in the context of the let name binding construct. When the definition is evaluated a name binding context is established around the lambda expression. The lambda expression is the only place in the program which have a possibility to reach the name bindings. Therefore the name bindings are local to the lambda expression. The CONTEXT is a list of name bindings, as such name bindings appear in a let construct.

Expression

Value

(define document
 (let
   ((html 
     (xml-modify-element html 
      'xmlns "http://www.w3.org/1999/xhtml"))
    (body 
     (xml-modify-element body 
      'bgcolor (rgb-color-encoding 255 0 0)))
      )
   (lambda (ttl bdy)
    (html
      (head (title ttl))
      (body bdy)))))
 
(document "A title" "A body")
<html xmlns = 
       "http://www.w3.org/1999/xhtml">
 <head>
  <title>A title</title>
 </head> 
 <body bgcolor = "#ff0000">
  A body
 </body>
</html>

An example in which we abstract a (html (head (title..)) (body ...)) expression in a lambda expression, of which we define a private context. The context redefines the html and body functions to 'specialized' versions, with certain attributes. Notice the importance of the 'simultaneous name bindings' of let in the example (as explained in an earlier lecture). Also notice that we have discussed the modify-element higher-order function before.