[Haskell-beginners] Tracing a program with putStrLn

legajid legajid at free.fr
Sat Jan 30 17:00:23 EST 2010


Hi,

i wrote a program that doesn't behave as i wanted.
So i tried to putStrLn my data structures to see what happened. Since i 
did this, all my functions changed type, so i had to modify them (main 
and calcul : use <- instead of let , add returns) and so on for each 
function called. Debugging this way causes  more trouble than it should 
save.

What is the way to trace data thru function recursive calls without 
changing program structures? Is putStrLn a good idea ?
I'm not used to ghci debug and i find it hard to manage (:break, :trace, 
:cont ...).
Is the 'debugged' version as efficient as the first one, due to impure 
functional code? What if i want to remove 'debug code' ?  Should i  
modify back my functions ?

Here are two very simplified versions of my program, the first one 
without 'trace', the second modified to include putStrLn as wanted.


Thanks in advance,
Didier


First version
-------------

main=do
    let valeurs=[0,1,3,0]
    let (valeurs_new)=calcul valeurs
    afficher_resultat valeurs
    afficher_resultat valeurs_new

calcul :: [Int] -> [Int]
calcul xv 
    | nblibr == 0    = map (*2) xv
    | otherwise    = map (+1) xv
                    
    where
            libres = [ x | x <- [1..length xv] ,  xv !! (x-1) == 0]
            nblibr=length libres

afficher_resultat xv = do
    putStrLn (show xv)


Second version
----------------

main=do
    let valeurs=[0,1,3,0]

    valeurs_new <- calcul valeurs

    afficher_resultat valeurs
    afficher_resultat valeurs_new

calcul :: [Int] -> IO [Int]
calcul xv  
    | nblibr == 0    = do
                putStrLn "ok"
                return (map (*2) xv)
    | otherwise    =     return (map (+1) xv)
                   
    where
            libres = [ x | x <- [1..length xv] ,  xv !! (x-1) == 0]
            nblibr=length libres

afficher_resultat xv = do
    putStrLn (show xv)



More information about the Beginners mailing list