rosella/src/lib/primitive/foldl.rla

80 lines
2.1 KiB
Plaintext

;; Fold a function over a list; left-associative
;; (foldl fn init lst)
;; ==> (foldl fn (fn (car lst) init) (cdr lst))
; (define (foldl fn init lst)
; (if (pair? lst)
; (foldl fn (fn (car lst) init) (cdr lst))
; init))
#0=#S(#="lambda"
; (define (foldl fn init lst)
; ((if (pair? lst)
; (lambda () (foldl fn (fn (car lst) init) (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 (car lst) init)))))
#(
#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
"\x02\x80\x41\x00\; (set! f0 (cons i1 nil))
\x00\x81\x03\x42\; (set! f1 (car i2))
\x02\x80\x81\x80\; (set! f0 (cons f1 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: