chickadee » srfi-116 » ifold-right

ifold-right kons knil ilist1 ilist2 ...procedure

The fundamental ilist recursion operator.

First, consider the single ilist-parameter case. If ilist1 is (e1 e2 ... en), then this procedure returns

(kons e1 (kons e2 ... (kons en knil)))

That is, it obeys the recursion

(ifold-right kons knil lis) ;=> (kons (icar lis) (ifold-right kons knil (icdr lis)))
(ifold-right kons knil '()) ;=> knil

Examples:

(ifold-right ipair '() lis)     ; Copy LIS.

;; Filter the even numbers out of LIS.
(ifold-right (lambda (x l) (if (even? x) (ipair x l) l)) '() lis))

If n ilist arguments are provided, then the kons procedure must take n+1 parameters: one element from each ilist, and the "seed" or fold state, which is initially knil. The fold operation terminates when the shortest ilist runs out of values:

(ifold-right ipair* '() (iq a b c) (iq 1 2 3 4 5)) ;=> (a 1 b 2 c 3)