[racket] arity of + versus <=

From: Carl Eastlund (cce at ccs.neu.edu)
Date: Fri Oct 28 14:08:04 EDT 2011

On Fri, Oct 28, 2011 at 2:04 PM, Joe Marshall <jmarshall at alum.mit.edu> wrote:
> On Fri, Oct 28, 2011 at 10:58 AM, Stephen Bloch <sbloch at adelphi.edu> wrote:
>>
>> OK, here's a variant of "binary->nary" that produces the results we want when "op" has contract "X X -> boolean" rather than "X X -> X"
>>
>> (define (binary->nary relop)
>>   (letrec ((f (lambda args
>>                 (or (empty? args)
>>                     (empty? (rest args))
>>                     (and (relop (first args) (second args))
>>                          (apply f (rest args)))))))
>>     f))
>>
>> This applies relop to each successive (overlapping) pair of elements and "and"s the results.
>
> Yes!  Thank you!
>
> Of course this fails for n-ary add and multiply, but presumably a
> person who preferred
> this generalization would have to answer the dual question of the
> original poster:
> Why do the relational operators generalize to 0 or 1 arguments but addition and
> multiplication do not?

You seem to be assuming that we have to pick one binary->nary for all
binary operators.  I would choose this one for relations and the other
one for associative operators with identities.  Other kinds of
operators may have other kinds of natural generalizations.

--Carl



Posted on the users mailing list.