Hello,
Here's the 'ifte' I was using in the fib scripts yesterday:
(define (ifte f g h)
(lambda (x)
(if (f x)
(g x)
(h x))))
As I've mentioned before, I've been trying out macro versions of such
higher-order utilities to accomodate variable arity. A naive macro for
the above is:
(define-syntax ifte
(syntax-rules ()
( (ifte f g h (x ...))
(lambda (x ...)
(if (f x ...)
(g x ...)
(h x ...))) )
( (ifte f g h)
(ifte f g h (x)) )))
The trouble with that is, in an expression like this:
(define fib
(ifte (less-than= 1)
(constant 1)
(bi (uni (sub 1) (lambda (x) (fib x)))
(uni (sub 2) (lambda (x) (fib x))) +)))
you end up with a procedure which when called does a ton of allocation.
One way to fix the problem is to write the macro like so:
(define-syntax ifte
(syntax-rules ()
( (ifte f g h (x ...))
(let ((f* f)
(g* g)
(h* h))
(lambda (x ...)
(if (f* x ...)
(g* x ...)
(h* x ...)))) )
( (ifte f g h)
(ifte f g h (x)) )))
Of course, it's not as pretty.
If this is pretty much the way to deal with this issue then OK. But I
thought I'd bring it up to see if anyone has other suggestions.
Another approach is to simply have separate procedures for the different
arities. I.e. ifte, ifte2, ifte3, and then maybe an iften.
Ed