[racket] arity of + versus <=

From: Stephen Bloch (bloch at adelphi.edu)
Date: Fri Oct 28 13:26:17 EDT 2011

On Oct 28, 2011, at 1:01 PM, Joe Marshall wrote:

> ... I was generalizing in a different way:
> 
> (define (binary-add a b) (+ a b))
> (define (binary-multiply a b) (* a b))
> 
> (define (binary->nary op base-case)
>  (lambda args (fold-left op base-case args))) ; *****

And as you point out correctly, this doesn't make sense for = or <=.  But the REASON it doesn't make sense is that = and <= have contract "number number -> boolean", whereas binary-add and binary-mult have contract "number number -> number".  If the return type of "op" isn't the same as the type of [at least one of] "op"'s arguments, the above approach to generalizing a binary operator to an n-ary operator can't work.

Since there is in fact a well-defined and useful meaning for "(= a b c d e)", to wit "all the numbers a, b, c, d, and e are equal," and a well-defined and useful meaning for "(<= a b c d e)", to wit "the sequence a, b, c, d, e is non-decreasing", it seems reasonable to implement these.


Stephen Bloch
sbloch at adelphi.edu




Posted on the users mailing list.