Loading ...
Sorry, an error occurred while loading the content.

Re: [sicp-vsg] some exercises problem

Expand Messages
  • Pascal Bourguignon
    ... 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 1 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 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) (empty-env))))
      (map (lambda (sym)
      (display 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" 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.

      (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 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.