Eduardo Cavazos wrote:

In principle, can a compiler be smart enough to perform the inlines so as to achive the zero-allocation behaviour? Or is there something about the design of the combinator itself which prevents this?

Wow:

Larceny v0.97 "Funny in the Head" (Aug 21 2009 05:09:12, precise:Linux:unified)
larceny.heap, built on Fri Aug 21 05:09:23 CDT 2009

>
(define (zero x) 0)

(define add-1-recur
  (lambda (loop vl hd tl)
    (+ 1 (loop tl))))

(define recur
  (lambda (loop vl hd tl)
    (loop tl)))

(define (count fun lst)
  (let loop ((lst lst))
    (if (null? lst)
        (zero lst)
        (let ((hd (car lst))
              (tl (cdr lst)))
          (let ((val (fun hd)))
            (if val
                (add-1-recur loop val hd tl)
                (recur       loop val hd tl)))))))


>
>
>
> (time (count odd? '()))
Words allocated: 0
Words reclaimed: 0
Elapsed time...: 0 ms (User: 4 ms; System: 0 ms)
Elapsed GC time: 0 ms (CPU: 0 in 0 collections.)
0

> (time (count odd? '(1 2 3 4 5 6)))
Words allocated: 0
Words reclaimed: 0
Elapsed time...: 0 ms (User: 0 ms; System: 0 ms)
Elapsed GC time: 0 ms (CPU: 0 in 0 collections.)
3

>

Reply via email to