Main_Page/Snack Code/Functions as arguments

fold an operator or function into a list of values to combine list items


foldr':: (a -> b -> b) -> b -> [a] -> b
-- arguments: operator value-for-empty-list list 
-- result: value of type 'b'

foldr' f s [] = s
foldr' f s (x:xs) = f x (foldr' f s xs)

-- Exampe of using foldr
testFoldr = foldr' (+) 0 [1,2,3,5]
-- result: 11


(define (foldr f s ls)
  (cond ((null? ls) s)
        (else (f (car ls) (foldr f s (cdr ls)))

(define testFoldr (foldr + 0 '(1 2 3 5)))

--Dokondr 16:27, 20 February 2006 (UTC)

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.