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

2011-11-29 Thread Alaric Snell-Pym
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

On 11/29/2011 12:23 PM, Christian Kellermann wrote:

> Actually the result of the else branch of (if (even? x) x) is
> undefined.

Aye. A single-armed "if" is only really useful for conditionalising code
performed for its side effects, as the return value of such an if is
useless.

Because this can be confusing, it's recommended to use "when" for
side-effecting conditionals, which has the added advantage of the body
being arbitrarily long, as there's no need to use position to
distinguish yay and nay branches:

(when (...some test...)
 (...do something...)
 (...do something...)
 (...do something...))

Indeed, a single-armed "if", especially used in a context where the
result is checked (eg, a non-final element of a BEGIN), is potentially
grounds for a compiler warning...

ABS

- --
Alaric Snell-Pym
http://www.snell-pym.org.uk/alaric/
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk7Uz7kACgkQRgz/WHNxCGqVZACgjO+Alqkm/D7F4o7sEeuS0W0R
1BMAnRG6s2CjV+x0sHW8EjEhLmAS15d8
=3u1p
-END PGP SIGNATURE-

___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users


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

2011-11-29 Thread Christian Kellermann
* Nick Zarr  [29 13:15]:
> Thanks.  After reading the spec quite a few times and becoming more
> confused, it slipped my mind that (if (even? x) x) would return (void) and
> that it was a true value.  I assumed that Chicken was doing the right
> thing, thanks to you and Jim for pointing out my error!

Nitpicking ahead :)

Actually the result of the else branch of (if (even? x) x) is
undefined. CHICKEN happens to return (void), but don't rely on it.
I personally stick to the (and (even? x) x) check Jim mentioned
because you will always get a truth value. (void) is not Ã#f but
CHICKEN *could* just return #f for cases like this and still be
within specs...

Kind regards,

Christian

-- 
Who can (make) the muddy water (clear)? Let it be still, and it will
gradually become clear. Who can secure the condition of rest? Let
movement go on, and the condition of rest will gradually arise.
 -- Lao Tse. 

___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users


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

2011-11-29 Thread Nick Zarr
On Tue, Nov 29, 2011 at 12:05 AM, John Cowan  wrote:

> Nick Zarr scripsit:
>
> > I'm writing an implementation of SRFI-1 in Javascript (just for fun) and
> I
> > have a question about SRFI-1's any procedure.  In the docs it says that
> > "(any) does not return a simple boolean (#t or #f), but a general value",
> > however it also says that any must be called with a "procedure taking n
> > arguments and returning a boolean result".
>
> The latter is clearly in error: the predicate argument to ANY, EVERY,
> and LIST-INDEX can return any value.  The words "and returning a boolean
> result" should be removed from the specification.
>
> No, that's the difference between ANY and FIND.  FIND returns the
> element which satisfies the predicate, whereas ANY returns what the
> predicate returns, in this case (void), because that is a true value.
> Chicken is doing the Right Thing.
>

Thanks.  After reading the spec quite a few times and becoming more
confused, it slipped my mind that (if (even? x) x) would return (void) and
that it was a true value.  I assumed that Chicken was doing the right
thing, thanks to you and Jim for pointing out my error!

-- 
Nick Zarczynski
Rent a Geek IT Services 
Blog 4 
___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users


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

2011-11-28 Thread John Cowan
Nick Zarr scripsit:

> I'm writing an implementation of SRFI-1 in Javascript (just for fun) and I
> have a question about SRFI-1's any procedure.  In the docs it says that
> "(any) does not return a simple boolean (#t or #f), but a general value",
> however it also says that any must be called with a "procedure taking n
> arguments and returning a boolean result".

The latter is clearly in error: the predicate argument to ANY, EVERY,
and LIST-INDEX can return any value.  The words "and returning a boolean
result" should be removed from the specification.

> 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?

No, that's the difference between ANY and FIND.  FIND returns the
element which satisfies the predicate, whereas ANY returns what the
predicate returns, in this case (void), because that is a true value.
Chicken is doing the Right Thing.

-- 
La mayyitan ma qadirun yatabaqqa sarmadiJohn Cowan
Fa idha yaji' al-shudhdhadh fa-l-maut qad yantahi.  co...@ccil.org
--Abdullah al-Hazred, Al-`Azif  http://www.ccil.org/~cowan

___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users


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

2011-11-28 Thread Jim Ursetto
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