-
Notifications
You must be signed in to change notification settings - Fork 2
fold
TurtleKitty edited this page May 12, 2019
·
2 revisions
Generic left-fold procedure. It is defined for multiple lists, multiple vectors, and single tables and sets.
(fold (lambda (x acc) (* x acc)) 1 '(2 3 5)) ; 30
(let ()
(def multiplexer
(lambda (x y z acc) (pair (list x y z) acc)))
(fold multiplexer () '(1 2 3) '(a b c) '(foo bar baz)))
; ((3 c baz) (2 b bar) (1 a foo))
(fold (lambda (x acc) (* x acc)) 1 #(vector 2 3 5)) ; 30
(fold (lambda (x acc) (pair x.head (* x.tail acc.tail)))
(0 . 1)
#(vector 2 3 5)
index: true)
; (2 . 30)
(fold (lambda (x y z acc) (* x y z acc))
10
(vector 1 2 3)
(vector 4 5 6)
(vector 7 8 9))
; 3628800
(let (t (: foo 2 bar 3))
(proc combiner (x acc)
(acc.add x.head x.tail))
(fold combiner (set) t))
; (set foo 2 3 bar)
(let (s (set 2 3 5))
(fold * 1 s))
; 30