简体   繁体   English

调试Haskell

[英]Debugging Haskell

I'm new to functional programming and I'd like to debug a recursive function to see why I am getting particular value as return value. 我是函数编程的新手,我想调试一个递归函数,以了解为什么我得到特定的值作为返回值。 How do I accomplish that? 我该怎么做? I found some answers on this site as well as on online, but I can't get my head wrap around the idea of doing that. 我在本网站以及在线上都找到了一些答案,但是我无法理解这样做的想法。 Any help would be appreciated. 任何帮助,将不胜感激。

recur = \a -> if a>100 then  a-10 else recur (recur (a+11))

You could do this (using Debug.Trace ): 您可以执行此操作(使用Debug.Trace ):

import Debug.Trace (trace)

recur a | trace ("recur " ++ show a) False = undefined
recur a = if a>100 then  a-10 else recur (recur (a+11))

This produces output each time the function recur is called (or rather, because Haskell is lazy, each time the result of applying recur is needed). 每次调用函数recur时,这都会产生输出(或者,因为Haskell是惰性的,因此每次需要应用recur的结果时)。

Sample output (in ghci): 样本输出(以ghci为单位):

*Main> recur 99
recur 99
recur 110
recur 100
recur 111
recur 101
91

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM