functional programming - How to understand Bird and Hughes foldr -


got lost trying understand charles bird's introduction functional programming john hughes' why functional programming matters. discussion of foldr.

length = foldr count 0 count n = n + 1 

when applied list, say, [1, 2, 3], should come 3. here's bird (p 66):

(#) = foldr oneplus 0        oneplus x n = 1 + n 

and here's more lambda treatment:

length = foldr (λx.λn.(1 + n)) 0 

i'm @ loss understand what's going on foldr once start trying apply list [1, 2, 3]. i'm not seeing variables x , n refer to. simple 1 sum:

sum = foldr (+) 0 

for [1, 2, 3] is

(+) 1 ((+) 2 ((+) 3 0)) = 6 

using hughes' notation of replacing list's implied cons function/operator , identity list's implied nil, (prefix) adding -- understand. not when dealing these mysterious variables. maybe can walk me through how list , variables interact.

the arguments function passed foldr element of list being folded on , accumulator (essentially, thing being calculated).

more explicit names these arguments might help:

length = foldr count 0 count ignored_elt length_so_far = length_so_far + 1 

another approach understanding folds write them recursive function, , write fold, , inspect both definitions see how similar. after few of these should start understand pattern folds capture. (hint: when doing this, try avoid commutative operations + can obscure difference between left , right folds).


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -