[racket] (if ...) form without an else

From: Danny Yoo (dyoo at cs.wpi.edu)
Date: Thu Apr 12 12:20:52 EDT 2012

[I'm adding Racket-users to cc.  Please make sure to Reply to All so
that the rest of the mailing list can contribute.]

> i generally do use when/unless for one armed (if ...) syntax and agree
> strongly with strict enforcement of three-part ifs, as described in
> http://lists.racket-lang.org/users/archive/2009-September/035423.html
> however, i have a quantity of old code that i have picked up here and
> there that contains one-armed ifs.
> since they work i was not aware that racket enforced the three-part
> syntax.
> the following module, for example, causes no complaint:
> (module xx mzscheme
> (if #t 1))

Yes, that's right.  The particular language you're using in a module
enforces this restriction.  In your example, you're using 'mzscheme'
as the language of the module.  'mzscheme' is a legacy language, so it
allows the one-armed if.  If you were to rewrite it as:

    (module xx racket
      ... (if #t 1) ...)

then the "racket" language's version of "if" will enforce the
restriction, and you'd see an error at compile time.

If you really need to weaken the restriction for a selection of
modules, you can add something like the following in the beginning of
each module:

    (require (only-in mzscheme if))

This declares that the 'if' being used by the rest of the module is to
be the one that follows mzscheme's legacy rules, rather than racket's
rules.  Alternatively,

    (require (only-in r5rs if))

where 'r5rs' is another legacy language that allows one-armed if.

For example:

#lang racket
(require (only-in mzscheme if))

(if true (displayln "one armed angel"))

Posted on the users mailing list.