Re: [sicp-vsg] some exercises problem
- peterzhuang2005 writes:
> Hi, Pascal BourguignonYou didn't try it, did you?
> 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))))
"Use the REPL, Luke!"
If you tried to run this little program:
(let ((test-env (extend-env '(a b c) '(() (c d) #f) (empty-env))))
(map (lambda (sym)
(if (has-association test-env sym)
(display " has association : ")
(display " hasn't association : "))
(display (has-association test-env sym))
(newline)) '(a b c)))
You might have seen some suprizing results:
a has association : s has no associated value.
b has association : (c d)
c hasn't association : #f
dError reported by apply-env:
No binding for d
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" environmentA rib is a list of two lists of same length. It's a list with some
> 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
(list (list 'a 'b 'c) (list '() '(c d) #f))
--> ((a b c) (() (c d) #f)) ; this is a rib.
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 ribYou are not returning a list of two sublist, you are returning a single list.
> 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)))
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'.
__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