Skip to content

Instantly share code, notes, and snippets.

@yihuang
Created August 25, 2012 03:33
Show Gist options
  • Save yihuang/3460106 to your computer and use it in GitHub Desktop.
Save yihuang/3460106 to your computer and use it in GitHub Desktop.
sum
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
@tiye
Copy link

tiye commented Aug 25, 2012

多了个 0 有没有?

@lilydjwg
Copy link

为什么要这样做呢?

@Liutos
Copy link

Liutos commented Aug 25, 2012

在我这里还是很慢,黄毅兄你是64位的系统吗?我是32位的~

@metalman
Copy link

大哥,这个把几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]

参照:
http://www.iteye.com/topic/366101

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment