Conditional structures: Difference between revisions
Content added Content deleted
Line 3,014: | Line 3,014: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
===[http://docs.racket-lang.org/reference/if.html#%28form._%28%28quote._~23~25kernel%29._if%29%29 if]=== |
===[http://docs.racket-lang.org/reference/if.html#%28form._%28%28quote._~23~25kernel%29._if%29%29 if]=== |
||
If-expressions in Racket must have both branches |
If-expressions in Racket must have both branches |
||
Line 3,021: | Line 3,022: | ||
"big") |
"big") |
||
</lang> |
</lang> |
||
===[http://docs.racket-lang.org/reference/when_unless.html#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._when%29%29 when/unless]=== |
|||
One-sided conditional expressions use "when" and "unless". These are more convenient for side-effects since they have an implicit "begin" around their body, and you can also include new definitions |
|||
<lang Racket> |
|||
(when (< x 10) |
|||
(define y (* x 10)) |
|||
(printf "small\n")) |
|||
</lang> |
|||
===[http://docs.racket-lang.org/reference/if.html#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._cond%29%29 cond]=== |
|||
Used for multiple conditions: |
|||
<lang Racket> |
|||
(printf "x is ~a\n" |
|||
(cond [(< x 1) "tiny"] |
|||
[(< x 10) "small"] |
|||
[(< x 100) "medium"] |
|||
[(< x 10000) "big"] |
|||
[(< x 100000000) "huge"] |
|||
[else "gigantic"])) |
|||
</lang> |
|||
===[http://docs.racket-lang.org/reference/case.html#%28form._%28%28lib._racket%2Fprivate%2Fmore-scheme..rkt%29._case%29%29 case]=== |
|||
Similar to a "switch" statement in other languages |
|||
<lang Racket> |
|||
(case x |
|||
[(1) "one"] |
|||
[(2) "two"] |
|||
[(3) "three"] |
|||
[(4) "four"] |
|||
[(6 8) "even"] |
|||
[(5 7 9) "odd"] |
|||
[else "something else"]) |
|||
</lang> |
|||
===etc=== |
|||
Racket has macros, which means that you can define whatever new conditional you think is useful... |
|||
=={{header|Retro}}== |
=={{header|Retro}}== |