difference between 2.2.2 and 2.2.3

54 views
Skip to first unread message

Andy

unread,
Aug 14, 2013, 8:35:53 AM8/14/13
to eo...@googlegroups.com
Hi,

I am a novice computer enthusiast studying eopl 3rd edition these days. Could anyone please point out the difference between procedural and data structure representation of the environment interfaces, explained in the sections mentioned in the subject? I am having a hard time trying to come up with an solution as I have experience only in Ocaml and am new to Scheme. Please help.

Dan Friedman

unread,
Aug 14, 2013, 11:09:19 AM8/14/13
to eo...@googlegroups.com
You can represent an environment, a procedure that maps symbols to values
as a function for example, the roman environment is

(define roman-fun
  (lambda (y)
    (cond
      ((eq? y 'I) 1)
      ((eq? y 'V) 5)
      ((eq? y 'X) 10)
      ((eq? y 'L) 50)
      ((eq? y 'C) 100)
      ((eq? y 'D) 500)
      ((eq? y 'M) 1000)
      (else (error)))))

And then define lookup like this

   (define lookup
      (lambda (y env)
         (env y))))

> (lookup 'X roman-fun)  ==> 10
> (lookup 'Z roman-fun)  ==> error.

or as a data structure:

(define roman-data
  '((I . 1) (V . 5) (X . 10) (L . 50) (C . 100) (D . 500) (M . 1000))

(define lookup
  (lambda (y env)
    (cond
      ((null? env) (error))
      ((eq? (car (car env)) y) (cdr (car env)))
      (else (lookup y (cdr env)))))  

> (lookup 'X roman-data) ==> 10
> (lookup 'Z roman-data) ==> error.

... Dan


On Wed, Aug 14, 2013 at 8:35 AM, Andy <nuv...@gmail.com> wrote:
Hi,

I am a novice computer enthusiast studying eopl 3rd edition these days. Could anyone please point out the difference between procedural and data structure representation of the environment interfaces, explained in the sections mentioned in the subject? I am having a hard time trying to come up with an solution as I have experience only in Ocaml and am new to Scheme. Please help.

--
You received this message because you are subscribed to the Google Groups "EOPL3" group.
To unsubscribe from this group and stop receiving emails from it, send an email to eopl3+un...@googlegroups.com.
To post to this group, send email to eo...@googlegroups.com.
Visit this group at http://groups.google.com/group/eopl3.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages