What do the authors in The Little Schemer mean by the "function's structure" and "argument's structure"?

  • Permalink
  • submit to reddit
  • Email
  • Follow


Hi all,

I finished reading The Little Schemer a few days ago and one of the
answers was not clear to me.

It's on page 40, 3rd from the top. It's about simplifying the rember
function (which removes the first occurrence of an element from a
list).

The question asks "So why don't we simplify right away?" and the
answer is "Because then a function's structure does not coincide
with its argument's structure."

Any ideas what "a function's structure" and "argument's structure"
are? And how do they coincide or not coincide?

Here is the original function:

(define rember
  (lambda (a lat)
    (cond
      ((null? lat) '())
      ((eq? (car lat) a) (cdr lat))
      (else (cond
              ((eq? (car lat) a) (cdr lat))
              (else (cons (car lat)
                          (rember a (cdr lat)))))))))

And here is the simplified version:

(define rember
  (lambda (a lat)
    (cond
      ((null? lat) '())
      ((eq? (car lat) a) (cdr lat))
      (else (cons (car lat)
                  (rember a (cdr lat)))))))


Thanks!


Peteris Krumins
0
Reply peteris.krumins (62) 2/14/2010 4:40:54 AM

See related articles to this posting


On Feb 13, 11:40 pm, Peteris Krumins <peteris.krum...@gmail.com>
wrote:
> Any ideas what "a function's structure" and "argument's structure"
> are? And how do they coincide or not coincide?
>
> Here is the original function:
>
> (define rember
>   (lambda (a lat)
>     (cond
>       ((null? lat) '())
>       ((eq? (car lat) a) (cdr lat))
>       (else (cond
>               ((eq? (car lat) a) (cdr lat))
>               (else (cons (car lat)
>                           (rember a (cdr lat)))))))))

This is not the original function: you've inserted the second clause
of the cond expression. A list is either null, or a cons of something
onto a list. That is the structure of the argument, and so should be
the basis for the structure of the function. Since the argument can
take on one of exactly two forms, the function should naturally handle
exactly those two forms. The structure of the argument informs the
nature of the recursion in the function definition.

Once you have accounted for all possible forms your argument(s) can
take on, then you can think about transformations that preserve the
meaning of your function.

Anthony
0
Reply newsgrouptony (4) 2/15/2010 3:38:13 PM
comp.lang.scheme 4554 articles. 1 followers. Post

1 Replies
179 Views

Similar Articles

[PageSpeed] 39


  • Permalink
  • submit to reddit
  • Email
  • Follow


Reply:

Similar Artilces: