[plt-scheme] Conditional bind then do something

From: David Van Horn (dvanhorn at ccs.neu.edu)
Date: Tue Feb 24 16:28:31 EST 2009

y1wm6lg02 at sneakemail.com wrote:
> Just posted to comp.lang.scheme...
> Hi,  I'd like a let... binding to extend beyond the let.  The idea
> is to bind the variables to one thing or another, then pass them to
> some subsequent procedure, without using set!, etc.
> Something like the following python
> def some_function(arg):
>    if some_test(arg):
>       var1=123
>       var2=456
>    else:
>       var1="xyz"
>       var2="abc"
>    do_blaaa(var1, var2)

There are multiple ways to do this.  The simplest for this example:

(define (some-function arg)
   (if (some-test arg)
       (do-blaaa 123 456)
       (do-blaaa "xyz" "abc")))

If it truly bothers you that do-blaaa is referenced twice, you can do this:

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


Posted on the users mailing list.