Haskell Debugging

How can I print a list or something in haskell with every call, for example:

funct a list = funct (a + 1) (a : list) print list here ??????? but how ? 
+6
source share
2 answers

There is for debugging

 import Debug.Trace funct :: Integer -> [Integer] -> Bool funct a list = trace (show list) $ funct (a + 1) (a : list) 

where trace :: String -> a -> a . It uses unsafePerformIO under the hood, so it is evil and only for debugging.

Remember that due to lazy evaluation, the debug output may appear in unexpected order and alternates with the output that the program usually generates.

FROM

 module TraceIt where import Debug.Trace funct :: Integer -> [Integer] -> Bool funct 10 list = null list funct a list = trace (show list) $ funct (a + 1) (a : list) 

I get

 *TraceIt> funct 1 [] [] [1] [2,1] [3,2,1] [4,3,2,1] [5,4,3,2,1] [6,5,4,3,2,1] [7,6,5,4,3,2,1] [8,7,6,5,4,3,2,1] False 

as was expected.

+11
source

Same as Daniel Fisher, but only with unsafePerformIO .

 > import System.IO.Unsafe > let funct a list = unsafePerformIO $ do { print list; return $ funct (a + 1) (a : list) } 

Take a look at a similar question that describes what happens when you use unsafePerformIO .

+1
source

Source: https://habr.com/ru/post/913816/


All Articles