Created
August 25, 2012 03:33
-
-
Save yihuang/3460106 to your computer and use it in GitHub Desktop.
sum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sum' :: Int -> Int | |
sum' n = loop n 0 | |
where | |
loop 0 x = x | |
loop n x = loop (n-1) (x+n) | |
main = print $ sum' 1000000000 |
为什么要这样做呢?
在我这里还是很慢,黄毅兄你是64位的系统吗?我是32位的~
大哥,这个把几G的内存耗光了等了很久也没结果……
这个如何:
foldl' f z [] = z
foldl' f z (x:xs) = let z' = f z x in z' `seq` foldl' f z' xs
newsum = foldl' (+) 0
main = print $ newsum [1..100000000]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
多了个 0 有没有?