[Haskell-cafe] How to show a variable with "show"?

Mads Lindstrøm mads_lindstroem at yahoo.dk
Thu May 5 09:02:18 EDT 2005


> > module Main where
> > 
> > main = do putStr (show (foo 3))
> >           putStr "\n"
> >           putStr (show (foo 7))
> >           putStr "\n"
> >           putStr (show y)
> > 
> > foo x = (x + 2) / y where
> >		y = 2 * 2
> And how do I see functions or variables defined
> inside of other functions? In example above - y

Short answer -- you don't. 

Haskell is a purely functional langauge and functions can therefore not
have side effects. Side effects can only occur in the IO monad (and
maybe in some other monads) as shown above in main, which really is a IO
monad with type "main :: IO()". Therefore you cannot print the value of
y to the screen in the middle of the foo function.

This may seem like a stupid limitation, but restricting your functions
to be pure eliminates a lot of errors. And in my, albeit limited Haskell
experience, there is rarely a need to see the value of variables writen
inside other funcitons (as shown in your example).

If you really need this kind of feedback, then there are some debugging
tools here: http://www.haskell.org/libraries/#tracing which may do it
for you. However, I have tried none of them and do not really know what
they can do.

hope it helps,

Mads Lindstrøm <mads_lindstroem at yahoo.dk>

> Yahoo! Mail
> Stay connected, organized, and protected. Take the tour:
> http://tour.mail.yahoo.com/mailtour.html
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe

More information about the Haskell-Cafe mailing list