Table of Contents

Debugging

Example with trace

module Main where
 
import Debug.Trace (trace)
 
main :: IO ()
main = do
    putStrLn $ reverse' "1234abcd"
 
reverse' :: Show a => [a] -> [a]
reverse' l = rev l []
    where
        rev [] acc = acc
        rev (e:r) acc = trace (show acc) (rev r (e : acc))

Output:

""
"1"
"21"
"321"
"4321"
"a4321"
"ba4321"
"cba4321"
dcba4321

Example with traceShow

module Main where
 
import Debug.Trace (traceShow)
 
main :: IO ()
main = do
    putStrLn $ reverse' "1234abcd"
 
reverse' :: Show a => [a] -> [a]
reverse' l = rev l []
    where
        rev [] acc = acc
        rev (e:r) acc = traceShow (e, r, acc) (rev r (e : acc))

Output:

('1',"234abcd","")
('2',"34abcd","1")
('3',"4abcd","21")
('4',"abcd","321")
('a',"bcd","4321")
('b',"cd","a4321")
('c',"d","ba4321")
('d',"","cba4321")
dcba4321