From: Conrad Taylor
Subject: Confused about Scheme...???
Date: 
Message-ID: <C4tp5t.2n4@cs.uiuc.edu>
    In short, 'reduce-list', is take a list of variable length, 'b',
below and reduce it if the (caar ls) and (caadr ls) are equal...this
is the first atom within the pair of consecutive sublists and if this
is true contruct a list,  (list (caar ls) (+ (cadar ls) (cadadr ls))) 
, and add second atom of the consective pairs.  For example, 
(reduce-list b) ==> ((4 3) (3 7) (2 1) (1 2) (0 1)).  I can get it to
work for the first two terms without using recursion, produces (4 3),
but when I implement recursion it barfs.  Could some one tell me what
I'm doing wrong because I know that I'm trying to do to much at once?

Thanks in advance,

-Conrad


(define (reduce-list ls)
    (cond ((null? ls) ls)
    (else 
      (cond ((null? (cadr ls)) ls)
      (else 
        (cond ((eq? (caar ls) (caadr ls))
                 (list (caar ls) (+ (cadar ls) (cadadr ls))) 
                 (reduce-list (cdr ls)))
              (else (list (car ls) (reduce-list (cdr ls)))))))))))


(define b '((4 1) (4 2) (3 3) (3 4) (2 1) (1 2) (0 1)))

(reduce-list b)

From: Brian Harvey
Subject: Re: Confused about Scheme...???
Date: 
Message-ID: <1pfo4q$j65@agate.berkeley.edu>
·······@cs.uiuc.edu (Conrad Taylor) writes:
>    (cond ((null? ls) ls)
>    (else 
>      (cond ((null? (cadr ls)) ls)
>      (else 

Well, the first problem, although not the one that makes your program
fail, is that you're using COND as if it were IF.  You mean

(cond ((null? ls) ls)
      ((null? (cadr ls)) ls)
      ((eq? (caar ls) (caadr ls)) (list ...))
      (else (list ...)))

>        (cond ((eq? (caar ls) (caadr ls))
>                 (list (caar ls) (+ (cadar ls) (cadadr ls))) 
>                 (reduce-list (cdr ls)))

But the real problem is here.  I can't tell whether you just miscounted
parentheses, or whether you really want to program this as a sequence of
events instead of a function composition, but you are saying

        (list (...) (...))
	(reduce-list ...)

as two separate expressions, whereas you really mean

        (list (list (...) (...))
	      (reduce-list ...))

to combine the result of the recursive call with the result from this time.

What you wrote computes a two-number list, then *throws that value away* and
makes a recursive call.
From: Matt Wright
Subject: Re: Confused about Scheme...???
Date: 
Message-ID: <1993Apr1.215843.2919@pasteur.Berkeley.EDU>
··@anarres.CS.Berkeley.EDU (Brian Harvey) writes:
> [...] you really mean
>
>        (list (list (...) (...))
>	      (reduce-list ...))
>
>to combine the result of the recursive call with the result from this time.

Er, Brian really means

(CONS (list (...) (...))
      (reduce-list ...))

-Matt