[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] Correct behavior of SRFI-1 any

From: Jim Ursetto
Subject: Re: [Chicken-users] Correct behavior of SRFI-1 any
Date: Mon, 28 Nov 2011 23:46:40 -0600

On Nov 28, 2011, at 9:01 PM, Nick Zarr wrote:

I've been testing my implementation against Chicken for correctness and here's what I get on Chicken:

#;2> (any (lambda (x) (if (even? x) x)) '(1 2 3))
#;3> (any (lambda (x) (even? x)) '(1 2 3))
#;4> (any even? '(1 3 5))

For #3 and #4 any correctly returns #t and #f respectively.  However for #2 shouldn't any return 2 instead of, what I'm assuming is, (void)?  What is the correct behavior here?


#;2> (any (lambda (x) (if (even? x) x)) '(1 2 3))

Change that to

(any (lambda (x) (if (even? x) x #f)) '(1 2 3))

or more idiomatically

(any (lambda (x) (and (even? x) x)) '(1 2 3))

Your if statement returns void in the false case (technically, an unspecified value; in Chicken, it's void).   But the only false value in Scheme is #f.  Therefore your predicate always returns a true value for the first value it hits, so it will return void for 1.  It would have returned 2 if the first value were 2:

#;7> (any (lambda (x) (if (even? x) x)) '(2 3))

Replacing "if" with "and" is the way we usually handle that. 


reply via email to

[Prev in Thread] Current Thread [Next in Thread]