tags:

views:

61

answers:

1

The function I wrote for SICP 2.20 is:

(define (same-parity x . y)
  (if (null? (car y)
  '()
  (if (= (even? (car y)) (even? x))
     (cons (car y) (same-parity (cons x (cdr y))))
     (same-parity (cons x (cdr y))))))

And then I try to call it with

(same-parity 1 2 3 4 5 6 7)

The error I get is: "The object #t, passed as the first argument to integer-equal? is not the correct type."

I thought that equal works with #t and #f...

An example of code I found online is the following, I ran it and it works. But, what am I doing wrong?

(define (same-parity a . rest)
  (define (filter rest)
    (cond ((null? rest) '())
          ((= (remainder a 2) (remainder (car rest) 2))
           (cons (car rest) (filter (cdr rest))))
          (else
            (filter (cdr rest)))))
  (filter (cons a rest)))
+3  A: 

The = procedure accepts numbers. But even? returns a boolean not a number.

Use equal? instead of =.

equal? works with booleans.

For instance at the REPL:

> (even? 2)
#t

> (= (even? 2) (even? 2))
=: expects type <number> as 1st argument, given: #t; other arguments were: #t

> (equal? (even? 2) (even? 2))
#t
Greg H
Thanks, that solved the main problem with that bit of code.
aeroegnr