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

NY Lost Funds



campusblook last question
Pastornhehe
campusblocould 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]
campusbloah right
quicksilverfoldr (:) [] is very handy :)
campusbloso for cons its always []
dmwit> foldr (++) [] ["Hello, ", "world!"]
lambdabot"Hello, world!"
campusbloand in most other cases it will be 0 or 1 ?
Pastorn> foldl (concat . (+)) [] [[1,2,3] [4,5,6]]
lambdabotCouldn't match expected type `[[a]]'
Pastornwhoops
quicksilverfoldr ((:).(+1)) [] [1,2,3,4,5,6]
Vq^campusblo: no, it can really be anything
quicksilver> foldr ((:).(+1)) [] [1,2,3,4,5,6]
campusbloor (0,0)
lambdabot[2,3,4,5,6,7]
quicksilvercampusblo: foldr ((:).f) [] is more commonly known as 'map'
Vq^campusblo: fold is very widely applicable
Pastorncampusblo: the base case is what [] should be interpreted as
dmwitcampusblo: It can be used with pretty much any base case, depending on what you want to do.
Pastornnot the other way around =)
dmwitcampusblo: 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.
quicksilverif you want to draw an analogy with an imperative loop, the base case is something like 'initial setup' and the function is 'one step'
ToRAcampusblo: [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
lambdabot5040
ToRA> foldr (liftM2 (***) min max) (maxBound, minBound) [2,7,4,2,1,5,9] :: (Int, Int)
lambdabot(1,9)
quicksilverToRA: cute :)
kuribascampusblo: do you know foldr1 and foldl1 ?
dmwit> (maximum *** minimum) [2,7,4,2,1,5,9]
campusbloso basically you do everything then you do the base case
lambdabotCouldn't match expected type `([a], [a1])'
dmwit:t (***)
lambdabotforall (a :: * -> * -> *) b c b' c'. (Arrow a) => a b c -> a b' c' -> a (b, b') (c, c')
campusbloyes i know foldr1 well kinda

Page: 4 11 18 25 32 39 46 53 60 67 74 81 88 95 102 109 116 123 130 

IrcArchive

NY Lost Funds