3

I am trying to make my own "Set" datatype 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 element to my set?

Here is 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)
2
  • About the error itself: Set is not a type, Set e is a type. You forgot the e in the signature of insert. Commented May 30, 2015 at 16:16
  • empty should be written empty (Set s) = null s. if boolean then True else False is exactly the same as just boolean. Commented May 30, 2015 at 19:37

1 Answer 1

3

You have to implement insert like as follows. Note that your type signature isn't correct. You cannot insert Int to a set of type Set e (you can only insert Int to a set of type Set Int).

insert :: Set e -> e -> Set e
insert (Set s) value = Set $ value : s

Note that the above insert doesn't take into account of duplicate elements(Hint: use nub for eliminating that).

Sign up to request clarification or add additional context in comments.

2 Comments

I get this: Couldn't match type [e0]' with Set e0' Expected type: [Set e0] Actual type: [e] In the second argument of (:)', namely s' In the expression: Set value : s In an equation for `insert': insert (Set s) value = Set value : s
@gravysam I think you are missing $according to the error report ? Also see this: ideone.com/3LpTPq

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.