s c h e m a t i c s : c o o k b o o k

/ Cookbook.DisallowOneArmedIf

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Disallow One-Armed If with a Custom Language

Problem

It's idiomatic PLT Scheme to use WHEN and UNLESS if one wants to do a side-effect conditional. But the standard mzscheme language still allows one-armed if expressions. This can invite bug-prone code, so we may want to restrict the language to prevent one-armed if's in our own code.

Solution

We can write a custom language, and then use that language. Our new language will have mostly everything from mzscheme, but leave out the one-armed if.

(module no-one-armed-if mzscheme
  ;; This module reduces the mzscheme language and disallows
  ;; the one-armed if expression.
  (provide (all-from-except mzscheme if))
  (provide (rename my-if if))
  
  (define-syntax (my-if stx)
    (syntax-case stx ()
      [(my-if test arm-1 arm-2)
       (syntax/loc stx
         (if test arm-1 arm-2))]
      [else (raise-syntax-error #f "both arms of an if are required" stx)])))
Once we have such a module language, we can then use it. The following code will raise a syntax error:
> (module test "no-one-armed-if.ss"
    (if #t 'ok))
stdin::36: if: both arms of an if are required in: (if #t (quote ok))
but normal two-armed if usage won't error out:
> (module test "no-one-armed-if.ss"
    (if #t 'ok 'oh-no))
>

Discussion

See http://download.plt-scheme.org/doc/352/html/mzscheme/mzscheme-Z-H-12.html#node_sec_12.5 for an extended example of this technique.


Comments about this recipe

Contributors

-- DannyYoo - 16 Nov 2006

CookbookForm
TopicType: Recipe
ParentTopic: IdiomRecipes
TopicOrder: 999

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest