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

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))))

      (define extend-env
      (lambda (syms vals env)
      (lambda (sym)
      (let ((pos (list-find-position sym syms)))
      (if (number? pos)
      (list-ref vals pos)
      (apply-env env sym))))))

      (define apply-env
      (lambda (env sym)
      (env sym)))

      (define list-find-position
      (lambda (sym los)
      (list-index (lambda (sym1) (eqv? sym1 sym)) los)))

      (define list-index
      (lambda (pred ls)
      (cond
      ((null? ls) #f)
      ((pred (car ls)) 0)
      (else (let ((list-index-r (list-index pred (cdr ls))))
      (if (number? list-index-r)
      (+ list-index-r 1)
      #f))))))

      I guess i'm not quite understand the the whole procedure. What
      exactly the "environment interface" means and can you give me a more
      concrete example for this concept? Thank you!

      peter
    • Pascal Bourguignon
      ... When you define an abstraction, you need to specify the interface to this abstraction. Here, we re defining the abstraction environment, with procedures.
      Message 2 of 4 , Oct 9, 2005
      • 0 Attachment
        peterzhuang2005 writes:
        > 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))))
        >
        > (define extend-env
        > (lambda (syms vals env)
        > (lambda (sym)
        > (let ((pos (list-find-position sym syms)))
        > (if (number? pos)
        > (list-ref vals pos)
        > (apply-env env sym))))))
        >
        > (define apply-env
        > (lambda (env sym)
        > (env sym)))
        >
        > (define list-find-position
        > (lambda (sym los)
        > (list-index (lambda (sym1) (eqv? sym1 sym)) los)))
        >
        > (define list-index
        > (lambda (pred ls)
        > (cond
        > ((null? ls) #f)
        > ((pred (car ls)) 0)
        > (else (let ((list-index-r (list-index pred (cdr ls))))
        > (if (number? list-index-r)
        > (+ list-index-r 1)
        > #f))))))
        >
        > I guess i'm not quite understand the the whole procedure. What
        > exactly the "environment interface" means and can you give me a more
        > concrete example for this concept? Thank you!

        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.

        (empty-env) --> environment
        (extend-env symbols values environment) --> environment
        (apply-env environment symbol) --> value


        This interface defines an abstract data type: environment.


        You can build an empty environment calling the procedure empty-env.

        You can extend an environment with a list of symbols and a corresponding
        list of values, giving a new environment.

        You can find the value corresponding to a symbol in an environment.


        These three operations _define_ what an environment is.


        The two other and all the rest of the code belongs to some specific
        implementation of the environment procedural abstraction. They don't
        matter.


        So you need to parse "environment interface" as:

        "The interface of the procedural abstraction named 'environment'."



        --
        __Pascal Bourguignon__ http://www.informatimago.com/

        In a World without Walls and Fences,
        who needs Windows and Gates?
      • peterzhuang2005
        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?

          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)))
          (define apply-env
          (lambda (env sym)
          (if (null? env)
          (epol: error 'apply-env "No binding for ~s" sym)
          (let ((syms (car (car env)))
          (vals (car (car env)))
          (env (cdr (car env)))
          (let ((pos (rib-find-position sym syms)))
          (if (number? pos)
          (list-ref (cadr rib) pos)
          (apply-env env sym )))))))


          peter
        • 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 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 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.