[plt-scheme] Conditional bind then do something
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))
David