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))
> #t
> #;4> (any even? '(1 3 5))
> #f
>
> 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?
Nick,
> #;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))
2
Replacing "if" with "and" is the way we usually handle that.
Jim
_______________________________________________
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users