[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Working my way through The Little Schemer- question

ritwikbhatia

10/20/2015 3:27:00 AM

In discussing two implementations of rember, TLS says

Q: do you think this is simpler?
A: functions like rember can always be simplified this way
Q: so why don't we simplify it right away?
A: because then a function's structure does not correspond to it's argument's structure

I understand both implementations but do not understand the last statement above.

Could someone help me understand this?

; this is the preferred version
(define rember1
(lambda (a lst)
(cond
((null? lst) '())
(else
(cond
( (eq? a (car lst)) (cdr lst) )
( else (cons (car lst) (rember1 a (cdr lst))) )
))
)))

;this is the simpler but less preferred version
(define rember
(lambda (a lst)
(cond
((null? lst) '())
( (eq? a (car lst)) (cdr lst) )
( else (cons (car lst) (rember a (cdr lst))) )
)))
3 Answers

Pascal J. Bourguignon

10/20/2015 4:16:00 AM

0

ritwikbhatia@gmail.com writes:

> In discussing two implementations of rember, TLS says

news:comp.lang.scheme would be a better usenet group to discuss scheme
questions.


> Q: do you think this is simpler?
> A: functions like rember can always be simplified this way
> Q: so why don't we simplify it right away?
> A: because then a function's structure does not correspond to it's argument's structure
>
> I understand both implementations but do not understand the last statement above.

In general, the structure of the code will match the structure of the
data it processes. But of the two functions below, clearly the last one
is the best and clearest.


> Could someone help me understand this?
>
> ; this is the preferred version
> (define rember1
> (lambda (a lst)
> (cond
> ((null? lst) '())
> (else
> (cond
> ( (eq? a (car lst)) (cdr lst) )
> ( else (cons (car lst) (rember1 a (cdr lst))) )
> ))
> )))
>
> ;this is the simpler but less preferred version
> (define rember
> (lambda (a lst)
> (cond
> ((null? lst) '())
> ( (eq? a (car lst)) (cdr lst) )
> ( else (cons (car lst) (rember a (cdr lst))) )
> )))

This is the prefered form:

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

Notice the lack of spaces after ( or before ).

--
__Pascal Bourguignon__ http://www.informat...
â??The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.� -- Carl Bass CEO Autodesk

ritwikbhatia

10/21/2015 6:18:00 PM

0

Informatimago:

1. I found this newgroup via a FAQ on Paul Graham's website, where it is mentioned as a newbie friendly location. As it has proven to be for me. Thank you. Didn't think of looking for comp.lang.scheme - smacking self on the head.

2. From your reply, I infer that you too are not seeing the point being made in TLS justifying the additional (cond ...) relative to the version with a single (cond ...) - "because then a function's structure does not correspond to it's argument's structure". Note, that after showing the two versions of rember, all subsequent examples in the chapter of TLS use the code structure with two cond statements.

3. The extra spaces before or after "(" or ")" is all me - I go cross eyed after about three consecutive ))) - guess I am quite far from the Jedi Master level of Scheming :-)

Cheers



On Tuesday, October 20, 2015 at 12:15:55 AM UTC-4, informatimago wrote:
> ritwikbhatia@gmail.com writes:
>
> > In discussing two implementations of rember, TLS says
>
> news:comp.lang.scheme would be a better usenet group to discuss scheme
> questions.
>
>
> > Q: do you think this is simpler?
> > A: functions like rember can always be simplified this way
> > Q: so why don't we simplify it right away?
> > A: because then a function's structure does not correspond to it's argument's structure
> >
> > I understand both implementations but do not understand the last statement above.
>
> In general, the structure of the code will match the structure of the
> data it processes. But of the two functions below, clearly the last one
> is the best and clearest.
>
>
> > Could someone help me understand this?
> >
> > ; this is the preferred version
> > (define rember1
> > (lambda (a lst)
> > (cond
> > ((null? lst) '())
> > (else
> > (cond
> > ( (eq? a (car lst)) (cdr lst) )
> > ( else (cons (car lst) (rember1 a (cdr lst))) )
> > ))
> > )))
> >
> > ;this is the simpler but less preferred version
> > (define rember
> > (lambda (a lst)
> > (cond
> > ((null? lst) '())
> > ( (eq? a (car lst)) (cdr lst) )
> > ( else (cons (car lst) (rember a (cdr lst))) )
> > )))
>
> This is the prefered form:
>
> (define rember
> (lambda (a lst)
> (cond
> ((null? lst) '())
> ((eq? a (car lst)) (cdr lst))
> (else (cons (car lst) (rember a (cdr lst)))))))
>
> Notice the lack of spaces after ( or before ).
>
> --
> __Pascal Bourguignon__ http://www.informat...
> "The factory of the future will have only two employees, a man and a
> dog. The man will be there to feed the dog. The dog will be there to
> keep the man from touching the equipment." -- Carl Bass CEO Autodesk

Kaz Kylheku

10/21/2015 6:47:00 PM

0

On 2015-10-21, AarBee <ritwikbhatia@gmail.com> wrote:
> 2. From your reply, I infer that you too are not seeing the point being made
> in TLS justifying the additional (cond ...) relative to the version with a

Cascading COND's is completely silly; I would expect only machine-generated
code to do that, not a thinking human.

That is to say:

(cond (condition1 forms1 ...)
(t (cond (condition2 forms2 ...)
t
... (cond (conditionN formsN ...)
(t alternatives ...)) ... )))

should be written:

(cond
(condition1 forms1 ...)
(condition2 forms2 ...)
...
(conditionN formsN ...)
(t alternatives ...))

The point of COND is that it expresses a cascade of tests with linear syntax.
Why introduce unnecessary nesting into it, plus additional clutter (cond's and
t's) to support that nesting.