Foo Bar User
Foo Bar User

Reputation: 2491

racket function, is it wrong or am i missing somethng

Found this function in the book realm of racket:

(define (my-filter pred lst)
  (cond [(empty? lst) empty]
        [(pred (first lst))
         (cons (first lst) (my-filter (rest lst)))]
        [else (my-filter (rest lst))]))

Calling it with:

(my-filter (lambda (i) (> i 5)) `(1 2 3 4 5 6 7 8))

gives:

my-filter: arity mismatch;
 the expected number of arguments does not match the given number
  expected: 2
  given: 1
  arguments...:
   '(2 3 4 5 6 7 8)

So i changed it to:

(define (my-filter pred lst)
  (cond [(empty? lst) empty]
        [(pred (first lst))
         (cons (first lst) (my-filter pred (rest lst)))]
        [else (my-filter pred (rest lst))]))

And now it works fine. So i was wandering if there's anything i missed there or if the book is wrong?

Upvotes: 0

Views: 2120

Answers (1)

Foo Bar User
Foo Bar User

Reputation: 2491

it turns out the book was wrong. found this in the realm of racket home page. I thought it could be something i didn't understand because it was my first day doing racket.

page 114 The definition of the my-filter function takes in two parameters, but only one is supplied when recurring. The definition should read:

(define (my-filter pred lst)
  (cond [(empty? lst) empty]
        [(pred (first lst))
         (cons (first lst) (my-filter pred (rest lst)))]
        [else (my-filter pred (rest lst)))))

Upvotes: 3

Related Questions