Jump to content

Conditional structures: Difference between revisions

Line 3,017:
===[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
<lang Racketracket>
(if (< x 10)
"small"
Line 3,025:
===[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 Racketracket>
(when (< x 10)
(define y (* x 10))
Line 3,033:
===[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 Racketracket>
(printf "x is ~a\n"
(cond [(< x 1) "tiny"]
Line 3,045:
===[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 Racketracket>
(case x
[(1) "one"]
Cookies help us deliver our services. By using our services, you agree to our use of cookies.