In standard Scheme it is possible to write
(if (> x 2)
(set! x (- x 1)))
but this is not possible in Racket — Racket’s if always requires two arms. Why?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Rationale
The one-armed variant of
ifwas removed from Racket to prevent bugs.In functional code one always uses the two-armed variant of
if.Forgetting the second arm
expr-on-falsewould not lead to a syntax-error, but to a runtime error (the expression would return#<void>).To prevent these often occurring bugs in functional code, it was decided to introduce the form
whenfor the one-armed variant ofif.Besides preventing accidental bugs, the new form clearly indicated to a reader of code, that the code relies on side effects.
Porting code from standard Scheme to Racket
If you try running Scheme code in Racket and see the error message
you must rewrite the
ifexpression towhenorunless.Simply rewrite:
and