问题
In Haskell, I can make Haskell value from a string with read
.
Prelude> read "1" + 3
4
I can use fst
to get the first element
Prelude> fst (1,2)
1
However, I get an error when I combine read
and fst
to get the first element:
Prelude> fst (read "(1,2)")
<interactive>:20:6:
Could not deduce (Read b0) arising from a use of ‘read’
from the context (Read a)
bound by the inferred type of it :: Read a => a
at <interactive>:20:1-18
The type variable ‘b0’ is ambiguous
What's the problem?
回答1:
As read
is a polymorphic function, the read "3" + 4
works because the compiler know you want a Num
because you applied +
to read "3"
, If the compiler can't figure out what you want, you have to specify type of read
, like this:
Prelude> let rd = read :: String->(Int,Int)
Prelude> rd "(1,2)"
回答2:
First, let's see the type signature of read
:
Prelude> :t read
read :: Read a => String -> a
As you can see, its return value must have a type of the Read
typeclass. However, if we don't use the value the complier won't know what type it is.
In such cases, we can add ::
to specify its type.
Prelude> read "5"
*** Exception: Prelude.read: no parse
Prelude> read "5" :: Int
5
Similarly,
Prelude> read "(1, 2)"
*** Exception: Prelude.read: no parse
Prelude> read "(1, 2)" :: (Int, Int)
(1,2)
Prelude> fst (read "(1,2)" :: (Int, Int))
1
The compiler can deduce the types most of the time, but when it meets read "5"
, it may get confused about whether the type should be Int
or Float
or anything else. Only after computing, Haskell can know its type. On the other hand, Haskell has static types, so it has to know all type before compiling.
来源:https://stackoverflow.com/questions/31888700/why-cant-read-deduce-the-correct-type