[英]Why does a Haskell type "deriving Integral" need to be "deriving Enum"?
[英]Haskell. Why does read function does not work with custom data type though I used deriving?
讀取 function 無法與自定義數據類型一起正常工作。
data TwoInts = Int Int
deriving (Read, Show)
conv :: String -> TwoInts
conv s = read s
當我用 ghci ghci conv.hs
它加載正確,但是當我調用它conv "15 14"
我得到以下錯誤:
*** Exception: Prelude.read: no parse
如建議的那樣,您的數據類型:
data TwoInts = Int Int
deriving (Read, Show)
描述了一個名為Int
的構造函數,它接受類型Int
。 如果您要使用記錄,也許這更清楚:
data TwoInts = Int { int :: Int}
deriving (Read, Show)
評論中建議的解決方案是將您的構造函數命名為其他名稱,例如,
data TwoInts = TwoInts Int Int
deriving (Read, Show)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.