Search code examples
haskellghci

What happens with a specific function name 'it'?


I was just wrestling with a Haskell error for an hour, and then traced it to the use of a specific function name it. Can someone explain why function names fu and it behave differently in the transcript below? Specifically, why does the type of it change when it is called?

λ> fu x = x + 1
λ> :t fu
fu :: Num a => a -> a
λ> fu 1
2
λ> :t fu
fu :: Num a => a -> a
λ> it x = x + 1
λ> :t it
it :: Num a => a -> a
λ> it 1
2
λ> :t it
it :: Num a => a

When ghci is started fresh, it reports that it is not in scope, so I do not think it should have any special meaning by default.


Solution

  • In GHCi, it is always bound to the last evaluated expression, and shadows its previous value:

    Prelude> 2 + 3
    5
    Prelude> it
    5
    Prelude> 3 * 4
    12
    Prelude> it
    12
    

    In the beginning of the GHCi session, it's not defined.