问题
So I put this statement on my ghci
jkl x f y = f (map (+y) x)
And I got this out put back.
jkl :: Num b => [b] -> ([b] -> t) -> b -> t
But I'm confused when I read it. From my understanding jkl is type num that takes [b], [b] and t, and b. in the end it will output t. is that the right way to read it?
回答1:
jkl :: Num b => [b] -> ([b] -> t) -> b -> t
Whoever calls jkl
has to
- choose types
b
andt
- guarantee that
b
was chosen among numeric types (theNum b
constraint) - pass a
[b]
(list ofb
) as first argument - pass a function
[b] -> t
as second argument (i.e. taking[b]
and returning at
) - pass a
b
as third argument - receive
t
back as a final result
回答2:
Whatever is between the double colon ::
and the fat arrow =>
is called constraints. In this case, you have one constraint: Num b
. This constraint demands that, whatever type b
turns out to be, it must be an instance of type class Num
.
After the fat arrow, you get types of function parameters, and at the very end type of its result.
Parameters:
[b]
- a list of values of typeb
, whatever that is.[b] -> t
- a function that takes a list ofb
and produces a singlet
.b
- a single value of typeb
.
And finally, function result is t
.
来源:https://stackoverflow.com/questions/47803288/how-to-read-haskell-type-signature