#haskell - Sun 25 Mar 2007 between 19:34 and 19:50

NY Lost Funds



sorearnote that if the list is used once, ghc can fuse the consumer and producer
yshhqbut still i think my question is that difficult
ndmsorear, fusion would probably eliminate the list totally, and be much faster than bytestring
sorearndm: not if there are multiple consumption loops
yshhqsince to calculate sum(x) you need to calculate partial sum
so it should be able to stop in the middle if I only want to test sum(x) == n
because of laziness, right?
dmwityshhq: Take a look at scanl, dropWhile, and null.
Some combination of those should get you what you want.
yshhq3x
dmwit> [1..] == [1]
lambdabotFalse
dmwitProbably don't even need null, come to think of it.
> let checkSum n xs = [n] == dropWhile (< n) $ scanl1 (+) xs in checkSum 55 [1..10]
lambdabotCouldn't match expected type `[a]'
dmwit:t scanl1
lambdabotforall a. (a -> a -> a) -> [a] -> [a]
dmwit> let checkSum n xs = [n] == (dropWhile (< n) $ scanl1 (+) xs) in checkSum 55 [1..10]
lambdabotTrue
dmwit> let checkSum n xs = [n] == (dropWhile (< n) $ scanl1 (+) xs) in checkSum 54 [1..10]
lambdabotFalse
dmwit> let checkSum n xs = [n] == (dropWhile (< n) $ scanl1 (+) xs) in checkSum 56 [1..10]
lambdabotFalse
dmwit> let checkSum n xs = [n] == (dropWhile (< n) $ scanl1 (+) xs) in checkSum 56 [1..]
lambdabotFalse
EvilTerranmight you want "scanl (+) 0 xs" ?
dmwitSure, why not.
Seems like he's dealing with some uber-long lists, though, if he's that worried about summing it.
EvilTerranmight be a "protection from infinite lists" sort of thing. there's no harm in covering the null case, anyway.
hpastedpiponi pasted "I'm so crap I couldn't have invented monads" at http://hpaste.org/1136
dpiponiI still can't find the error. This is going to be so embarassing...
CaleI'll have a look
augustsserror?
yshhqhow about let runningSum = scanr1 (+) x in if all (<=n) runningSum then head runningSum == n else False
dmwityshhq: IIRC, scanr is exactly the wrong one for your use.
yshhqdoes using scanl or scanr matter here?
dpiponiaugustss: I've had too many comments that that post has an error for me to ignore them.
Calescanr won't save you any time
ddarius> foldM (\x y -> guard (x+y < 10) >> return (x+y)) 0 [1..3]
lambdabotadd an instance declaration for (Show (m b))

Page: 5 12 19 26 33 40 47 54 61 68 75 82 89 96 103 110 

IrcArchive

NY Lost Funds