SICP Virtual Study Group is a Public Group with 128 members.

Primary Navigation

HI,every one. i got some problems needing your help

Expand Messages

peterzhuang2005

hi,everyone. actually i m taking course . the textbook uses Scheme to introduce the basic ideas in programming. But some

Message 1 of 4
, Oct 8, 2005

0 Attachment

hi,everyone.
actually i'm taking course <<foundations of programming
language>>. the textbook uses Scheme to introduce the basic ideas in
programming. But some part are too abstract for me. The second part
introduces the Data abstraction. there is a concept "the environment
interface". the procedural representaion of environment is
(define empty-env
(lambda ()
(lambda (sym)
(eopl:error 'apply-env "No binding for ~s" sym))))

When you define an abstraction, you need to specify the interface to
this abstraction. Here, we're defining the abstraction environment,
with procedures. Therefore we define the procedure that compose the
interface to this abstraction.

Hi, Pascal Bourguignon Thank you for your patient explanation.There are also some exercises that i m not quite sure my answer is right or got the idea about

Message 3 of 4
, Oct 10, 2005

0 Attachment

Hi, Pascal Bourguignon
Thank you for your patient explanation.There are also
some exercises that i'm not quite sure my answer is right or got the
idea about it.Can you explain more concrete?
1.Add to the environment interface a predicate called
has-association?that takes an environment env and a symbol s and tests
to see if s has an associated value in env.entend the procedural
representation to implement this by representing the environment by
two procedures:one that returns the value associated with a symbol and
onethat returns whether or not the symbol has an association.
my answer is :(define has-association
(lambda (env s)
(if (null? (apply-env env s))
"s has no associated value."
(apply-env env s))))

2. repeat the first question using the usual "rib" environment
implementation.
actually the definition of rib is :
(define rib-find-position
(lambda (sym los)
(list-find-position sym los)))
but i didn't see any difference between "rib" and "list".So i don't
know the answer to this question. can you explain the difference
carefully?

... You didn t try it, did you? Use the REPL, Luke! If you tried to run this little program: (let ((test-env (extend-env (a b c) (() (c d) #f)

Message 4 of 4
, Oct 10, 2005

0 Attachment

peterzhuang2005 writes:

> Hi, Pascal Bourguignon
> Thank you for your patient explanation.There are also
> some exercises that i'm not quite sure my answer is right or got the
> idea about it.Can you explain more concrete?
> 1.Add to the environment interface a predicate called
> has-association?that takes an environment env and a symbol s and tests
> to see if s has an associated value in env.entend the procedural
> representation to implement this by representing the environment by
> two procedures:one that returns the value associated with a symbol and
> onethat returns whether or not the symbol has an association.
> my answer is :(define has-association
> (lambda (env s)
> (if (null? (apply-env env s))
> "s has no associated value."
> (apply-env env s))))

You were asked for a predicate, but you didn't follow the convention
to name the predicate with a '?'. Name it: has-association?

You were asked for a predicate, it doesn't return a boolean; it always
return a value, most of the time it's not #f, and if the symbol passed
has no association, it breaks with an error.

> 2. repeat the first question using the usual "rib" environment
> implementation.
> actually the definition of rib is :
> (define rib-find-position
> (lambda (sym los)
> (list-find-position sym los)))
> but i didn't see any difference between "rib" and "list".So i don't
> know the answer to this question. can you explain the difference
> carefully?

A rib is a list of two lists of same length. It's a list with some
more structure.

The above definition of rib-find-position is not consistent with this
definition of a rib. Perhaps your book has a different definition
for rib, but I doubt it given:

> 3.a simpler representation of environments would be a single rib
> consisting of a list of symbols and a list of values.Implement the
> environment interface for this representation.
> my answer is that there are two procedures of environment needed to be
> changed. (define extend-env
> (lambda (syms vals env)
> (cons rib env)))

You are not returning a list of two sublist, you are returning a single list.
Therefore your implementation doesn't match the specifications.

More over, you've never tested it, otherwise you'd have got errors
such as 'rib: underfined variable'.

Try harder!

--
__Pascal Bourguignon__ http://www.informatimago.com/
Our enemies are innovative and resourceful, and so are we. They never
stop thinking about new ways to harm our country and our people, and
neither do we. -- Georges W. Bush

Your message has been successfully submitted and would be delivered to recipients shortly.