Haskell

Haskell 中文群

u-facafichciiiheihbb 4 months ago
。。。
u-facafichciiiheihbb 4 months ago
你这个跟FOLD没关系
u-egi 4 months ago
没 Bang 的话,写成这样尾递归也会死。
u-facafichciiiheihbb 4 months ago
我是说FOLD的问题
u-egi 4 months ago
所以不是 fold* 的问题。
u-facafichciiiheihbb 4 months ago
你这个是LAZY的,跟FOLD不一样。
u-facafichciiiheihbb 4 months ago
我是说你以前的一个FOLD表达式我跑不出来。
u-facafichciiiheihbb 4 months ago
你这是递归的,严格计算可以,FOLD这个TRUNK不好搞。
u-egi 4 months ago
On my workstation running GHC 7.10.2, foldl' (\a e -> (mod e 10)*a) 1 [1..10^7] has a compiled run-time of 422 ms (all of it calculation) and allocates over 400 MBytes on the heap. Conversely, foldr (\e a -> (mod e 10)*a) 1 [1..10^7] has a compiled run-time of 2203 ms (the same 422 ms calculation time and 1781 ms of garbage collection) and allocates more than 550 MBytes on the heap. That is what you should expect from reading the previous sections; foldr will build up a huge stack of nested expression before evaluating the result and that stack needs to be garbage collected.
u-facafichciiiheihbb 4 months ago
fst $ foldl (\(x,y) z -> (y, x+z)) (0,1) [1..10^8] 你这个加BANG可以跑出来?
u-jjibbjdi 4 months ago
你随便搞个data SP a b = SP !a !b
u-jjibbjdi 4 months ago
就完事了 这个技巧很多地方都有
u-jjibbjdi 4 months ago
如果能unpack更好
u-facafichciiiheihbb 4 months ago
Prelude Data.List> fst $ foldl (\(!x,!y) !z -> (y, x+z)) (0,1) [1..10^8] cabal: repl failed for fake-package-0. The build process was killed (i.e. SIGKILL). The typical reason for this is that there is not enough memory available (e.g. the OS killed a process using lots of memory).
u-egi 4 months ago
f :: Integer f = fst $ foldl (\(!x,!y) !z -> (y, x+z)) (0,1) [1..10^8]
u-facafichciiiheihbb 4 months ago
你狠。。。
u-facafichciiiheihbb 4 months ago
我是没跑出来过。
u-egi 4 months ago
@火锅boy韩大冬 感谢!
u-facafichciiiheihbb 4 months ago
难道是我电脑问题?
u-egi 4 months ago
! 加在参数里面了没有?
u-facafichciiiheihbb 4 months ago
我上面不是复制了。
u-facafichciiiheihbb 4 months ago
跟你的一样
u-egi 4 months ago
这个 1MB 内存就能跑出来啊。
u-egi 4 months ago
ghc -O2 -XBangPatterns -rtsopts fib.hs && time ./fib +RTS -M1024K -RTS 9
u-egi 4 months ago
你是用了这个命令吗?
u-facafichciiiheihbb 4 months ago
我直接GHCI运行呃。
u-egi 4 months ago
GHCI 不做数的啊。
u-facafichciiiheihbb 4 months ago
为啥?
u-facafichciiiheihbb 4 months ago
你用GHCI可以跑么?
u-facafichciiiheihbb 4 months ago
为啥呢?
u-egi 4 months ago
GHCI 有没有做编译优化我都不知道。。。
u-facafichciiiheihbb 4 months ago
你把-O2关了可以跑么?
u-facafichciiiheihbb 4 months ago
没 有O2不行了吧?
u-facafichciiiheihbb 4 months ago
[larluo@nixos-larluo:~]$ ghc -XBangPatterns -O2 larluo.hs [1 of 1] Compiling Main ( larluo.hs, larluo.o ) Linking larluo ... [larluo@nixos-larluo:~]$ ./larluo (2500000000000000,2500000050000001) [larluo@nixos-larluo:~]$ ghc -XBangPatterns larluo.hs [1 of 1] Compiling Main ( larluo.hs, larluo.o ) [Optimisation flags changed] Linking larluo ... [larluo@nixos-larluo:~]$ ./larluo Killed
u-egi 4 months ago
f :: Integer f = fst $ foldl (\(!x,!y) !z -> (y, x+z)) (0,1) [1..10^8]
u-facafichciiiheihbb 4 months ago
你的电脑跟我不一样么?
u-egi 4 months ago
ghc 版本多少?
u-facafichciiiheihbb 4 months ago
你按我最简单的来运行试试。。。我不加O2就跑不了。
u-facafichciiiheihbb 4 months ago
8.6.3
u-egi 4 months ago
(我觉得我俩拉低了本群的讨论水平。。。)
u-facafichciiiheihbb 4 months ago
所以我就怪异你为啥 加了BANG就能跑出来
u-facafichciiiheihbb 4 months ago
这个是问题,没搞清楚
u-facafichciiiheihbb 4 months ago
我用foldl'就能跑出来
u-facafichciiiheihbb 4 months ago
我GHC没加BANG也跑出来了,
u-facafichciiiheihbb 4 months ago
[larluo@nixos-larluo:~]$ ./larluo (2500000000000000,2500000050000001) [larluo@nixos-larluo:~]$ cat larluo.hs import Data.List main :: IO () main = print $ foldl' (\(x,y) z -> (y, x+z)) (0,1) [1..10^8]