Hacker News new | past | comments | ask | show | jobs | submit login

Actually, Haskell does let you write print statements for debugging.

If we have the following function:

    foo :: Int -> Int
    foo x = x `div` 0
and we want to add debugging, we can do:

    import Debug.Trace

    foo :: Int -> Int
    foo x
      | trace (show x) False = undefined
      | otherwise = x `div` 0
The above will print the value of x before throwing an error due to division by zero. You don't have to make foo return an IO Int or change any other aspect of your program.



Something I like to do is:

    import Debug.Trace
    wtf v x = trace (show x) v

    someFunction x = anotherFunction x `wtf` x
This allows me to tack `wtf` onto the end of otherwise unchanged expressions to do print debugging




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: