[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Scheme-reports] Formal comment: Allow "if" to accept arbitrarily many if-then pairs



Submitter's name: John Boyle
Submitter's email: johnthescavenger@x
Relevant draft: r7rs-small-current

Type: enhancement
Priority: major
Relevant section of draft: Conditionals

Summary: Allow "if" to accept arbitrarily many if-then pairs.

Description:
The conditional operator "if" should be made to accept arbitrarily many if-then pairs, so that:

  (if (blip)
      (blop)
      (flip)
      (flop)
      (floop))

is equivalent to:

  (cond ((blip)
         (blop))
        ((flip)
         (flop))
        (else (floop)))

This is more terse and provides for a better user experience.  (In the case of 3 arguments, one who writes "if" and later discovers that he needs to add more cases must either write nested "if"s or go back and rewrite the whole _expression_ with "cond"; either choice is unpleasant.  For precisely this reason, there are people who preempt it by always using "cond" over "if" in the first place; these people pay a cost in extra parentheses, letters typed, and indentation.)

I anticipate reluctance to alter the syntax of a primitive operator.  But this change is entirely backwards compatible with code that uses the previous "if", and skeptical people are free to continue only writing ifs with three arguments and using "cond" otherwise.  The only thing they will lose is error-checking if they accidentally give four or more arguments to "if".  I point out that the same objection could be made to "map", +, -, *, /, and other functions.  I add that the Arc language has used this form of "if" without any "cond" at all, and I didn't find anyone complaining in any of 14,000 posts on the Arc Forum that they wanted "cond" back.

Here is an example implementation in terms of "cond".  (I see no reason not to allow *zero* or more arguments, rather than 2 or more, but I expect that is pushing it.)

  (define-syntax my-if
    (syntax-rules ()
      ((my-if a b c ...) ;at least two arguments
       (expand-if a b c ...))))
 
  (define-syntax expand-if
    (syntax-rules ()
      ((expand-if) (cond (#f #f))) ;the unspecified fall-through result
      ((expand-if x) x)
      ((expand-if a b rest ...)
       (cond (a b)
             (else (expand-if rest ...))))))

This suggestion comes from the Arc language.  Further discussion may be found in the below link, near the phrases "An if" and "cond operator":

http://ycombinator.com/arc/tut.txt

--John Boyle
Science is what we understand well enough to explain to a computer. Art is everything else we do. --Knuth
_______________________________________________
Scheme-reports mailing list
Scheme-reports@x
http://lists.scheme-reports.org/cgi-bin/mailman/listinfo/scheme-reports