Reputation: 1128
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
I'm new to data types in Haskell. I'm wondering why ListUnit works but I'm having problems with ListUnit2. I know I shouldn't have to break out all of ListUnits values like I do in the first instance. But when I don't, GHC that I'm not passing enough arguments. I don't see how what I'm doing is different than LYAH in the Shapes example.
In case anyone is wondering, this is for "99 Problems" #11. The data I want to model is [Multiple 4 'a',Single 'b',Multiple 2 'c', Multiple 2 'a',Single 'd',Multiple 4 'e']
.
Upvotes: 0
Views: 87
Reputation: 183211
The problem is that this:
decodeModified2 :: ListUnit2 -> Char
means "decodeModified2
takes a ListUnit2
(type) and returns a Char
", whereas this:
decodeModified2 _ _ ls = ls
means "decodeModified2
takes three arguments, discarding the first two and returning the third". These two statements are incompatible.
I think what you meant to write is:
decodeModified2 (ListUnit2 _ _ ls) = ls
which means "decodeModified2
takes a value constructed by ListUnit2
(function) with three arguments, discarding the first two and returning the third".
Does that make sense?
Edited to add after your comment below: And similarly, when you call decodeModified2
, you need to pass an instance of ListUnit2
(type). For example, you can write something like decodeModified2 (ListUnit2 3 's' 'x')
, using the value constructor ListUnit2
(function) to create an instance of ListUnit2
(type). (Or, more interestingly: you can create an instance of ListUnit2
(type) in one place and then call decodeModified2
in a different place, passing said instance.)
Upvotes: 4