Commit implementation of (foldl ...) function.

This commit is contained in:
Jesse D. McDonald 2010-04-11 05:50:29 -05:00
parent 3a418484c8
commit c6500a53b3
3 changed files with 115 additions and 1 deletions

View File

@ -0,0 +1,35 @@
#S(#="lambda"
; (define (test-foldl)
; (foldl + 0 '(2 3 4 5)))
#(
#i"../lib/primitive/foldl.rla"
(
#S(#="lambda"
; (define (+ x y)
; (fix+ x y))
#(#f)
#()
2
"\x00\x80\x03\xfd\; (set! f0 (car argv))
\x00\x81\x04\xfd\; (set! f1 (cdr argv))
\x00\x81\x03\x81\; (set! f1 (car f1))
\x08\x80\x80\x81\; (set! f0 (fix+ f0 f1))
\x02\x80\x80\x00"; (set! f0 (cons f0 nil))
0xfe ; k
0x80 ; f0
0x01 ; g1
0x01 ; g1
)
0
(2 3 4 5)
)
)
#()
0
""
0x01 ; g1
0x02 ; g2
0xfe ; k
0xff ; ctx
)
; vim:set syntax= sw=2 expandtab:

View File

@ -8,7 +8,7 @@
#()
0
""
0x01 ; g0
0x01 ; g1
0x02 ; g2
0xfe ; k
0xff ; ctx

View File

@ -0,0 +1,79 @@
;; Fold a function over a list; left-associative
;; (foldl fn init lst)
;; ==> (foldl fn (fn init (car lst)) (cdr lst))
; (define (foldl fn init lst)
; (if (pair? lst)
; (foldl fn (fn init (car lst)) (cdr lst))
; init))
#0=#S(#="lambda"
; (define (foldl fn init lst)
; ((if (pair? lst)
; (lambda () (foldl fn (fn init (car lst)) (cdr lst))
; (lambda () init))))
#(
#S(#="template"
; (lambda () init)
#(#f)
"\x81" ; f1
1
"\x02\x80\x40\x00"; (set! f0 (cons i0 nil))
0xfe ; k
0x80 ; f0
0x01 ; g1
0x01 ; g1
)
#S(#="template"
; (lambda ()
; (let/cc k
; (call-with-multiple-values
; (lambda (new-init)
; (k (foldl fn new-init (cdr lst))))
; (lambda () (fn init (car lst))))))
#(
#S(#="template"
; (lambda (new-init)
; (k (foldl fn new-init (cdr lst))))
#(#=0)
"\x40\x41\x42\xfe\xff" ; i0 i1 i2 k ctx
2
"\x00\x80\x04\x42\; (set! f0 (cdr i2))
\x02\x80\x80\x00\; (set! f0 (cons f0 nil))
\x00\x81\x03\xfd\; (set! f1 (car argv))
\x02\x80\x81\x80\; (set! f0 (cons f1 f0))
\x02\x80\x40\x80"; (set! f0 (cons i0 f0))
0x01 ; g1
0x80 ; f0
0x43 ; i3
0x44 ; i4
)
)
"\x80\x81\x82" ; f0=fn f1=init f2=lst
2
"\x00\x80\x03\x42\; (set! f0 (car i2))
\x02\x80\x80\x00\; (set! f0 (cons f0 nil))
\x02\x80\x41\x80\; (set! f0 (cons i1 f0))
\x00\x81\x1b\x01"; (set! f1 (lambda g1))
0x40 ; i0
0x80 ; f0
0x81 ; f1
0xff ; ctx
)
)
#()
4
"\x00\x80\x03\xfd\; (set! f0 (car argv)) ; f0=fn
\x00\x82\x04\xfd\; (set! f2 (cdr argv))
\x00\x81\x03\x82\; (set! f1 (car f2)) ; f1=init
\x00\x82\x04\x82\; (set! f2 (cdr f2))
\x00\x82\x03\x82\; (set! f2 (car f2)) ; f2=lst
\x00\x83\x0b\x82\; (set! f3 (pair? f2))
\x83\x83\x02\x01\; (set! f3 (if f3 g2 g1))
\x00\x83\x1b\x83"; (set! f0 (lambda f3))
0x83 ; f0
0x00 ; nil
0xfe ; k
0xff ; ctx
)
; vim:set syntax= sw=2 expandtab: