[Haskell-cafe] problems with trace

Henning Thielemann lemming at henning-thielemann.de
Fri May 17 21:43:10 UTC 2024


On Fri, 17 May 2024, George Colpitts wrote:

> After I load the following into ghci and call main I expect to see "norm" in the output 3 times as I have called it 3 times
> but it only shows once. Why?
> 
> traceBug.hs
> ---------------
> {-# OPTIONS_GHC -Wall #-}
> 
> import Debug.Trace (trace)
> 
> norm :: [Int] -> [Int]
> norm  = trace ("norm") id
> 
> main :: IO ()
> main =
>   do
>     print $ norm [2]
>     print $ norm [2]
>     print $ norm [3]
> ---------------------
> 
> ghci session
> 
> % ghci traceBug.hs
> Loaded package environment from /Users/gcolpitts/.ghc/x86_64-darwin-9.8.2/environments/default
> GHCi, version 9.8.2: https://www.haskell.org/ghc/  :? for help
> [1 of 2] Compiling Main             ( traceBug.hs, interpreted )
> Ok, one module loaded.
> ghci> main
> norm
> [2]
> [2]
> [3]


I think this is correct. 'norm' is evaluated only once, because it is the 
same variable (for a function) in every call. It would be different, if 
you would define:

   norm xs = trace "norm" xs


More information about the Haskell-Cafe mailing list