[Haskell-beginners] Tracing a program with putStrLn

Michael Snoyman michael at snoyman.com
Sat Jan 30 17:01:57 EST 2010


I think you want the Debug.Trace module. The documentation is available at
http://haskell.org/ghc/docs/latest/html/libraries/base/Debug-Trace.html.

Let me know if you have any questions,
Michael

On Sun, Jan 31, 2010 at 12:00 AM, legajid <legajid at free.fr> wrote:

> 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)
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20100130/a4fcda1f/attachment.html


More information about the Beginners mailing list