This site is a static rendering of the Trac instance that was used by R7RS-WG1 for its work on R7RS-small (PDF), which was ratified in 2013. For more information, see Home.

Ticket 382: Formal Comment: Allow "if" to accept arbitrarily many if-then pairs

2012-10-12 03:07:14
WG1 - Core
cowan
major
cowan
wontfix
source
closed
2012-04-23 06:52:13
defect

Submitter's name: John Boyle

Submitter's email: johnthescavenger at gmail.com

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 ifs 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 two 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 here, near the phrases "An if" and "cond operator":

owneralexshinncowan
statusnewaccepted
resolutionwontfix
statusacceptedclosed