
Hi, I am trying to something like f1 :: Int -> Int f1 x = f2 x f2 :: Int -> Int f2 x = 2 * x but before f2 returns its result I'd like it to print something like "The initial value is " ++ show x. How could I do this? Thank you

In general, you can't, not with these type signatures, since printing something is a side effect. If this is only for debugging purposes, you might try using trace from IOExts. You can use it in a nice fashion like:
f1 :: Int -> Int f1 x | trace ("The initial value is " ++ show x) False = undefined | otherwise = f2 x
In general, the 'trace ... False = undefined' thing is quite useful, but note that trace uses unsafePerformIO and very little is guarenteed about the output (especially the ordering might be different than you would expect...but it's usually fine for debugging purposes). On 13 Oct 2003, Jose Morais wrote:
Hi,
I am trying to something like
f1 :: Int -> Int f1 x = f2 x
f2 :: Int -> Int f2 x = 2 * x
but before f2 returns its result I'd like it to print something like "The initial value is " ++ show x.
How could I do this?
Thank you
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Hal Daume III | hdaume@isi.edu "Arrest this man, he talks in maths." | www.isi.edu/~hdaume

Hal Daume III
f1 :: Int -> Int f1 x | trace ("The initial value is " ++ show x) False = undefined | otherwise = f2 x
In general, the 'trace ... False = undefined' thing is quite useful
How is it better than
f1 x = trace ("The initial value is " ++ show x) $ f2 x
? Feri.

Hal Daume III
writes: f1 :: Int -> Int f1 x | trace ("The initial value is " ++ show x) False = undefined | otherwise = f2 x
In general, the 'trace ... False = undefined' thing is quite useful
How is it better than
f1 x = trace ("The initial value is " ++ show x) $ f2 x
It is much easier to use in real code. Consider f1 :: Foo -> Int f1 Alpha = 0 f1 (Beta i) = i f1 (Gamma x) = x*x f1 Delta = 4 f1 (Epsilon xs) = length xs Now which is easier: f1 :: Foo -> Int f1 Alpha = trace ("The initial value is " ++ show Alpha ) $ 0 f1 (Beta i) = trace ("The initial value is " ++ show (Beta i) ) $ i f1 (Gamma x) = trace ("The initial value is " ++ show (Gamma x) ) $ x*x f1 Delta = trace ("The initial value is " ++ show Delta ) $ 4 f1 (Epsilon xs) = trace ("The initial value is " ++ show (Epsilon xs)) $ length xs or f1 :: Foo -> Int f1 x | trace ("The initial value is " ++ show x) False = undefined f1 Alpha = 0 f1 (Beta i) = i f1 (Gamma x) = x*x f1 Delta = 4 f1 (Epsilon xs) = length xs ? --KW 8-)

Keith Wansbrough
Hal Daume III
writes: f1 :: Int -> Int f1 x | trace ("The initial value is " ++ show x) False = undefined | otherwise = f2 x
In general, the 'trace ... False = undefined' thing is quite useful
How is it better than
f1 x = trace ("The initial value is " ++ show x) $ f2 x
It is much easier to use in real code. Consider [...]
You went to extreme lengths answering my silly question. Thanks a lot, I see the point now. Feri.

In addition to what Keith said, it's also guarenteed that the trace is evaluated as soon as the function is entered. - Hal On Tue, 2003-10-14 at 02:36, Ferenc Wagner wrote:
Hal Daume III
writes: f1 :: Int -> Int f1 x | trace ("The initial value is " ++ show x) False = undefined | otherwise = f2 x
In general, the 'trace ... False = undefined' thing is quite useful
How is it better than
f1 x = trace ("The initial value is " ++ show x) $ f2 x
?
Feri. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe -- Hal Daume III | hdaume@isi.edu "Arrest this man, he talks in maths." | www.isi.edu/~hdaume

On Monday 13 October 2003 11:54 am, Jose Morais wrote:
Hi,
I am trying to something like
f1 :: Int -> Int f1 x = f2 x
f2 :: Int -> Int f2 x = 2 * x
but before f2 returns its result I'd like it to print something like "The initial value is " ++ show x.
f1 :: Int -> IO Int f1 x = f2 x f2 :: Int -> IO Int f2 x = do putStrLn $ "The initial value is " ++ show x return (2 * x) The previously mentioned trace method is prefered for debugging because with this, one is now in the IO monad. Shawn
participants (5)
-
Ferenc Wagner
-
Hal Daume III
-
Jose Morais
-
Keith Wansbrough
-
Shawn P. Garbett