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
):
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).
Sample output (in ghci):
*Main> recur 99
recur 99
recur 110
recur 100
recur 111
recur 101
91
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.