[Haskell-cafe] Pattern matching on numbers?

Ryan Ingram ryani.spam at gmail.com
Tue Nov 18 18:44:04 EST 2008


How does this work?

> fac n = case n of
>    0 -> 1
>    _ -> n * fac (n-1)

ghci> :t fac
fac :: (Num t) => t -> t

The first line of "fac" pattern matches on 0.  So how does this work
over any value of the Num typeclass?  I know that the "1" on the rhs
of fac are replaced with (fromInteger 1), but what about numeric
literals in patterns?  Does it turn into a call to (==)?

Should whatever technique is used be extended to other typeclasses too?

  -- ryan


More information about the Haskell-Cafe mailing list