Haskell. Why does read function does not work with custom data type though I used deriving?
Read function does not work properly with custom data type.
data TwoInts = Int Int
deriving (Read, Show)
conv :: String -> TwoInts
conv s = read s
When i load this function with ghci
ghci conv.hs
It loads properly, but when i call it
conv "15 14"
i am getting following error:
*** Exception: Prelude.read: no parse
As suggested, your data type:
data TwoInts = Int Int
deriving (Read, Show)
describes a constructor named Int
that accepts a type Int
. If you were to use records, maybe this is more clear:
data TwoInts = Int { int :: Int}
deriving (Read, Show)
The solution as suggested in the comments is to name your constructor something else, e.g.,
data TwoInts = TwoInts Int Int
deriving (Read, Show)