Elm中runTrace函数栈溢出及Ellie运行异常问题求助
Elm尾递归函数
runTrace栈溢出异常(添加日志后消失) 免责声明:我已在reddit上提出相同问题
我正在将一款面向学生的Haskell应用重写为Elm应用——一方面用来学习Elm语言,另一方面也能将其部署为静态Web应用以简化托管流程。但遇到了一个异常问题:runTrace函数明明是尾递归实现,却抛出了栈溢出错误(RangeError: Maximum call stack size exceeded)。更奇怪的是,只要在函数中启用日志输出,这个错误就会完全消失。我无法理解其中原因,也不知道该如何修复。此外,当我尝试在Ellie环境中运行代码时,即使启用了日志也无法正常工作,会抛出异常:Uncaught TypeError: Cannot read properties of undefined (reading 'init')。
- 以下是启用和未启用日志的代码片段
- 完整代码已整合到单个未整理文件中,可供参考
启用日志的代码
runTrace : ComputationModel model state input output -> model -> input -> ( List state, output ) runTrace cm instance input = let runTrace_ acc state = case cm.update instance state of Err newState -> (log <| "newState: " ++ Debug.toString newState) <| runTrace_ (\trace -> acc (newState :: trace)) newState Ok output -> ( acc [], output ) in runTrace_ identity <| cm.init instance input
未启用日志的代码
runTrace : ComputationModel model state input output -> model -> input -> ( List state, output ) runTrace cm instance input = let runTrace_ acc state = case cm.update instance state of Err newState -> runTrace_ (\trace -> acc (newState :: trace)) newState Ok output -> ( acc [], output ) in runTrace_ identity <| cm.init instance input
内容的提问来源于stack exchange,提问作者D.F.F
相关产品推荐
相关产品推荐

