[英]Confusion about custom data types and Haskell
data Quant = Single | Multiple deriving (Show, Read, Eq)
data ListUnit = ListUnit {
quant :: Quant,
num :: Int,
letter :: Char
} deriving (Show, Read, Eq)
data ListUnit2 = ListUnit2 Quant Int Char deriving (Show, Read, Eq)
decodeModified :: Quant -> Int -> Char -> Char
decodeModified _ _ ls = ls
decodeModified2 :: ListUnit2 -> Char
decodeModified2 _ _ ls = ls
我是Haskell中數據類型的新手。 我想知道為什么ListUnit可以工作,但ListUnit2卻有問題。 我知道我不必像在第一個實例中那樣打破所有ListUnits值。 但是,如果我不這樣做,GHC表示我沒有傳遞足夠的論點。 在Shapes示例中,我看不到我在做什么與LYAH有什么不同。
如果有人想知道,這是針對“ 99問題”#11的。 我要建模的數據是[Multiple 4 'a',Single 'b',Multiple 2 'c', Multiple 2 'a',Single 'd',Multiple 4 'e']
。
問題是:
decodeModified2 :: ListUnit2 -> Char
and returns a Char
", whereas this: 意思是“ decodeModified2
采用一個ListUnit2
並返回一個Char
”,而這是:
decodeModified2 _ _ ls = ls
意思是“ decodeModified2
接受三個參數,丟棄前兩個參數並返回第三個參數”。 這兩個語句不兼容。
我認為您的意思是:
decodeModified2 (ListUnit2 _ _ ls) = ls
with three arguments, discarding the first two and returning the third". 這意味着“ decodeModified2
采用由ListUnit2
構造的具有三個參數的值,丟棄前兩個參數並返回第三個參數”。
那有意義嗎?
. 編輯后在下面的注釋后添加 :同樣,當您調用 decodeModified2
,您需要傳遞ListUnit2
的實例。 to create an instance of ListUnit2
. 例如,您可以使用值構造函數ListUnit2
創建ListUnit2
的實例,編寫類似decodeModified2 (ListUnit2 3 's' 'x')
。 in one place and then call decodeModified2
in a different place, passing said instance.) (或者,更有趣的是:您可以在一個地方創建ListUnit2
的實例,然后在另一個地方調用decodeModified2
,並傳遞該實例。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.