Learn You a Haskell discusses newtype
.
How does its signature of Pair b a
mean that the passed-in argument must be a tuple?
ghci> newtype Pair b a = Pair { getPair :: (a, b) }
ghci> let p = Pair (5, 10)
I'm confused how b a
indicates a tuple.
The reason you pass in a tuple is not in the type, but in its constructor:
Pair { getPair :: (a, b) }
This is using record syntax to define a Pair
constructor with a single field called getPair
, which contains a tuple. You could get a very similar effect by breaking it into two parts:
newtype Pair b a = Pair (a, b)
getPair (Pair (x, y)) = (x, y)
So the b a
does not force it to be a tuple; that's what the { getPair :: (a, b) }
does.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With