[plt-scheme] required else for if ?

From: Doug Evans (dje at sebabeach.org)
Date: Sun Sep 6 15:07:07 EDT 2009

Hi.

How come the default behaviour of `if' in mzscheme is to
require the else clause,
and not have an optional else clause?

$ mzscheme --version
Welcome to MzScheme v4.1.2 [3m], Copyright (c) 2004-2008 PLT Scheme Inc.
$ mzscheme -e '(if #f 1)'
#f::0: if: bad syntax (must have an "else" expression) in: (if #f 1)
$ 

I thought Standard Scheme (rnrs) had the else clause as being optional,
with the result of an elided else clause being "unspecified",
or "void" I guess in plt-scheme-speak.
This seems like a gratuitous divergence.

I realize there's `when', which is in (rnrs control (6)),
but I'm porting a bit of code, and converting everything
to use `when' seems like unnecessary pain.

P.S. I found this in r5rs/main.ss

(define-syntax r5rs:if
  (syntax-rules ()
    [(_ test then)
     (if test then (void))]
    [(_ test then else)
     (if test then else)]))

and tweaked it to become

(define-syntax if
  (syntax-rules ()
    [(_ test then)
     (when test then)]
    [(_ test x-then x-else)
     (cond (test x-then) (else x-else))]))

which seems to work.
[Recorded here for future folks googling for a solution.
It mightn't be perfect, but it's got me past this issue.
Any recommended improvements are welcome of course.]


Posted on the users mailing list.