[plt-scheme] Conditionals define ==> (when #t (define....

From: Thomas Chust (chust at web.de)
Date: Wed Apr 29 14:10:27 EDT 2009

2009-04-29 Ziboon <ziboon at gmail.com>:
> [...]
> Why I can't do this :
>
> (if (= n 0)
>      (define lst (list 1 2 3))
>      (define lst2 (list 1 2 )) )
>
> How I can do this ?
> [...]

Hello,

in my humble opinion you are asking the wrong question -- you should
ask yourself what you are expecting that code to do or what you
actually want to achieve.

If you want two different variables bound for the two different
branches of execution, you can write that like this:

  (if (= n 0)
      (let ([lst (list 1 2 3)]) ... more code ...)
      (let ([lst2 (list 1 2)]) ... more code ...))

If you want the same variable bound to different values depending on
what is found in n, you can write that like this:

  (define lst
    (if (= n 0) (list 1 2 3) (list 1 2)))

If you want two or more variables bound to different values depending
on n's content, you can write that like this:

  (define-values (lst lst2)
    (if (= n 0)
        (values (list 1 2 3) #f)
        (values #f (list 1 2))))

But you certainly don't want two differently named variables bound in
the same scope depending on the value of a third variable, do you? How
would you access those variables and how would the compiler be able to
figure out which access was legal? Even if it was allowed using
dynamic resolution of variable names, this would certainly lead to
fragile code...

cu,
Thomas


-- 
When C++ is your hammer, every problem looks like your thumb.


Posted on the users mailing list.