In Haskell, why does this compile:
splice :: String -> String -> String
splice a b = a ++ b
main = print (splice "hi" "ya")
but this does not:
splice :: (String a) => a -> a -> a
splice a b = a ++ b
main = print (splice "hi" "ya")
>> Type constructor `String' used as a class
I would have thought these were the same thing. Is there a way to use the second style, which avoids repeating the type name 3 times?
The
=>syntax in types is for typeclasses.When you say
f :: (Something a) => a, you aren’t saying thatais aSomething, you’re saying that it is a type “in the group of”Somethingtypes.For example,
Numis a typeclass, which includes such types asIntandFloat.Still, there is no type
Num, so I can’t sayHowever, I could either say
or