New to Haskell so sorry if this is very basic
This example is taken from "Real World Haskell" -
ghci> :type fst
fst :: (a, b) -> a
They show the type of the fst
function and then follow it with this paragraph...
"The result type of fst
is a
. We've already mentioned that parametric polymorphism makes the real type inaccessible: fst
doesn't have enough information to construct a value of type a
, nor can it turn an a
into a b
. So the only possible valid behaviour (omitting infinite loops or crashes) it can have is to return the first element of the pair."
I feel like I am missing the fundamental point of the paragraph, and perhaps something important about Haskell. Why couldn't the fst
function return type b
? Why couldn't it take the tuple as a param, but simply return an Int
( or any other type that is NOT a
)? I don't understand why it MUST return type a
?
Thanks
See Question&Answers more detail:os