## #haskell - Wed 14 Mar 2007 between 08:24 and 08:28

### NY Lost Funds

 campusblo ok last question Pastorn hehe campusblo could somebody please give me an example of a fold function where [] is the base case? Vq^ ask already :) quicksilver > foldr (:) [] [1,2,3,4,5,6] lambdabot [1,2,3,4,5,6] campusblo ah right quicksilver foldr (:) [] is very handy :) campusblo so for cons its always [] dmwit > foldr (++) [] ["Hello, ", "world!"] lambdabot "Hello, world!" campusblo and in most other cases it will be 0 or 1 ? Pastorn > foldl (concat . (+)) [] [[1,2,3] [4,5,6]] lambdabot Couldn't match expected type `[[a]]' Pastorn whoops quicksilver foldr ((:).(+1)) [] [1,2,3,4,5,6] Vq^ campusblo: no, it can really be anything quicksilver > foldr ((:).(+1)) [] [1,2,3,4,5,6] campusblo or (0,0) lambdabot [2,3,4,5,6,7] quicksilver campusblo: foldr ((:).f) [] is more commonly known as 'map' Vq^ campusblo: fold is very widely applicable Pastorn campusblo: the base case is what [] should be interpreted as dmwit campusblo: It can be used with pretty much any base case, depending on what you want to do. Pastorn not the other way around =) dmwit campusblo: For example, the base case might be the result of some previous partial computation, and the list is the instructions for doing the rest of the computation. quicksilver if you want to draw an analogy with an imperative loop, the base case is something like 'initial setup' and the function is 'one step' ToRA campusblo: [jumping back a bit] shouldn't the answer to you a,b,c,d question be none of a,b,c,d and instead (maxBound, minBound) ? dmwit > foldr (*) 24 [5, 6, 7] -- 7 factorial lambdabot 5040 ToRA > foldr (liftM2 (***) min max) (maxBound, minBound) [2,7,4,2,1,5,9] :: (Int, Int) lambdabot (1,9) quicksilver ToRA: cute :) kuribas campusblo: do you know foldr1 and foldl1 ? dmwit > (maximum *** minimum) [2,7,4,2,1,5,9] campusblo so basically you do everything then you do the base case lambdabot Couldn't match expected type `([a], [a1])' dmwit :t (***) lambdabot forall (a :: * -> * -> *) b c b' c'. (Arrow a) => a b c -> a b' c' -> a (b, b') (c, c') campusblo yes i know foldr1 well kinda