Search code examples
lambdaschemepredicate

Scheme - predicate gets null


I just build a scheme code that uses predicate and iterate over a list.

this is the code:

(define (filter para lst)
       (foldr (lambda (x y) 
                (if (para x) 
                    (cons x y) y)) '() lst))

for example (filter odd? '(1 2 3)) will return '(1 3)

The lamda gets in the start '() and 3 - and make (para x) which is (odd? '()) and it works.

But if I write (odd? '()) it will print an error.

What am I missing?


Solution

  • The first parameter, x, is the current element of the list. The second one, y, is the accumulator. So on the first invocation x is 3 and y is '(). So you're not doing (odd? '()) and (cons '() 3), you're doing (odd? 3) and (cons 3 '()).