Eduardo Cavazos wrote:

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.

There's a similar problem with the version of curry which I posed awhile back.

(define-syntax curry
  (syntax-rules ()
    ((curry (f x)) f)
    ((curry (f x y ...))
     (lambda (x)
       (curry ((lambda (y ...)
                 (f x y ...))
               y ...))))))

Let's make a 'sub' procedure in a purely higher-order fashion:

(define (swap c)
  (lambda (x y)
    (c y x)))

(define sub (curry ((swap -) x y)))

(let ((f (sub 5)))

  (time (f 10)))

running stats for (f 10):
    no collections
    0 ms elapsed cpu time, including 0 ms collecting
    0 ms elapsed real time, including 0 ms collecting
    8 bytes allocated

This curry fixes the problem:

(define-syntax curry
  (syntax-rules ()
    ((curry (f x)) f)
    ((curry (f x y ...))
     (let ((f* f))
       (lambda (x)
         (curry ((lambda (y ...)
                   (f* x y ...))
                 y ...)))))))

running stats for (f 10):
    no collections
    0 ms elapsed cpu time, including 0 ms collecting
    0 ms elapsed real time, including 0 ms collecting
    0 bytes allocated

By the way, 'sub-from' is:

    (define sub-from (curry (- x y)))

Ed

Reply via email to