[plt-scheme] Passing procedures are parameters.

From: Marco Morazan (morazanm at gmail.com)
Date: Mon Feb 2 20:13:28 EST 2009

> Hello guys , i am new to scheme and still learning to think in the
> procedural way.I have a question.Is there a way to pass  a predicate as a
> parameter .for example
> (
> every? predicate lst) returns #f if any element of lst fails to
> satisfy predicate, and returns #t otherwise.
>
>> (every? number? '(a b c 3 e))
> #f
>> (every? number? '(1 2 3 5 4))
> #t
>

Yes, you can pass as an argument any predicate (whether built-in or
user-defined) you wish. It is that simple. :-)

> I can use map and check is the elements in the list are numbers .But what if
> i have to check for any other predicate.I mean can this be generalized.

Careful! map returns a list not a boolean. map is not what you would
want to use to define every? . You can, however, use a function
similar to map, say andmap, that traverses the list and returns a
boolean.

-- 

Cheers,

Marco


Posted on the users mailing list.