[plt-scheme] Conditional bind then do something

From: Neil Van Dyke (neil at neilvandyke.org)
Date: Tue Feb 24 16:27:50 EST 2009

y1wm6lg02 at sneakemail.com wrote at 02/24/2009 04:10 PM:
> def some_function(arg):
>    if some_test(arg):
>       var1=123
>       var2=456
>    else:
>       var1="xyz"
>       var2="abc"
>    do_blaaa(var1, var2)
>   

I would normally do this:

(define (some-function arg)
  (let-values (((var1 var2) (if (some-test arg)
                                (values 123 456)
                                (values "xyz" "abc"))))
    (do-blaa var1 var2)))

In this particular case, you can also use "call-with-values", but 
"let-values" is more convenient in most algorithms.

I think you will find multiple-value returns like this very helpful when 
coding functional (no mutations) algorithms.

(Plug: Hopefully someday the syntax for "let" will be changed to support 
multiple-values.  This can be done in a backwards-compatible way.  
Discussion at: http://srfi.schemers.org/srfi-71/mail-archive/maillist.html )

-- 
http://www.neilvandyke.org/


Posted on the users mailing list.