Haskell Use multiple parameters correctly
I'm trying to create my own Set data type, and when I try to declare an insert function, the compiler complains about too few arguments:
quantities.hs:12:27:
Expecting one more argument to `Set'
In the type signature for `insert': insert :: Set e -> Int -> Set
How can I correctly define the insert function to add a new item to my set?
Here's what I have so far:
data Set e = Set [e]
mySet :: Set Int
mySet = Set [1, 2, 3, 4, 5]
setLength :: Set e -> Int
setLength (Set s) = length s
empty :: Set e -> Bool
empty (Set s) = if null s then True else False
insert :: Set e -> Int -> Set
insert set value = set : value
main = do print(insert mySet 1)
source to share
You should implement insert
as follows. Please note that your type signature is incorrect. You cannot paste Int
into a typeset Set e
(you can only paste Int
into a typeset Set Int
).
insert :: Set e -> e -> Set e
insert (Set s) value = Set $ value : s
Note that the above insert
does not account for duplicate items (hint: use nub
to fix this).
source to share