Haskell不支持循环计算,而是提供使用递归算法.但是这种方法导致堆栈增长,甚至堆栈溢出.我认为应该有办法解决这个问题.这是样本.我想知道,每5秒可以调用多次getClockTime:
import System.Time
nSeconds = 5
main = do
initTime <- totalPicoSeconds `fmap` getClockTime
doWork initTime 1
where
doWork initTime n = do
currTime <- totalPicoSeconds `fmap` getClockTime
if (currTime - initTime) `div` 10 ^ 12 >= nSeconds
then print n
else doWork initTime (n+1)
totalPicoSeconds :: ClockTime -> Integer
totalPicoSeconds (TOD a b) = a * 10 ^ 12 + b
Run Code Online (Sandbox Code Playgroud)
该程序耗时5秒,但最终我得到了:
堆栈空间溢出:当前大小为8388608字节.
使用`+ RTS -Ksize -RTS'来增加它.
在特定情况下,手动管理堆栈大小可能有所帮助,但如果我希望运行此算法10秒钟,它可能会再次溢出.所以这不是一个解决方案.我怎样才能使这段代码有效?
s我正在考虑在字符处分割字符串的问题c。
这表示为
break (c ==) s
Run Code Online (Sandbox Code Playgroud)
其中 Haskell 库的定义break (c ==)足够接近
br [] = ([],[])
br s@(h:t) = if (c == h)
then ([],s)
else let (h',t') = br t in (h:h',t')
Run Code Online (Sandbox Code Playgroud)
(假设我立即想要访问返回值的第二项,以便强制执行任何惰性求值。) 的递归调用似乎存储br t在h调用堆栈上,但该算法的一般意义表明:这应该是没有必要的。这是在常量堆栈空间中使用具有可变性的伪代码语言执行此操作的一种方法,其中 & 表示通过引用进行传递,列表以 LISPy 对的形式实现:
br(c,s) =
allocate res_head,res_rest
iter(c,s,&res_head,&res_rest)
return (res_head,res_rest)
iter(c,s,&res_head,&res_rest) =
case s of
[] -> set res_head = res_rest = [] -- and terminate
c:ss -> set res_head = [], res_rest = s …Run Code Online (Sandbox Code Playgroud)