putStr

Keith Wansbrough Keith.Wansbrough at cl.cam.ac.uk
Tue Oct 14 13:24:39 EDT 2003


> Hal Daume III <hdaume at ISI.EDU> 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-)


More information about the Haskell-Cafe mailing list