Reputation: 293
I am a newbie to Haskell and Iam trying to implement Set
ADT with one function named union
using Lists
.here below is the code :
import Data.List
data Set a = Set [a]
union' :: Set a -> Set a -> Set a
union' xs [] = xs
union' [] ys = ys
union' x:xs ys = | not $ x:xs `elem` ys = x:union' xs ys
| otherwise union xs ys
I think am doing something terribly wrong here in type allocation. while compiling it's showing an error as below
error: parse error on input `::'
union' :: Set a -> Set a -> Set a
pardon me for such silly mistakes but any help would be appreciated, Thank you
Upvotes: 0
Views: 149
Reputation: 567
Without the original code for examining, I can only leave a version for you to compare and may you find something that cause parsing error on '::'.
import Data.List
data Set a = Set [a]
union' :: Eq a => Set a -> Set a -> Set a
union' (Set xs) (Set []) = Set xs
union' (Set []) (Set ys) = Set ys
union' (Set (x:xs)) (Set ys)
| not $ x `elem` ys = cons (Set [x]) (union' (Set xs) (Set ys))
| otherwise = union' (Set xs) (Set ys)
cons (Set xs) (Set ys) = Set (xs ++ ys)
instance Show a => Show (Set a) where
show (Set xs) = show xs
The following is what I bumped into when I started to modify the source code:
You don't need to put '=' before guard '|'
You need equal space indentation for guard '|'
missing '=' for the second guard expression.
called union instead of union'
you are gonna need your datatype constructor 'Set' for every pattern matching. (or a wrapper function for unpacking Set constructor and a helper function passing two list. In this way, you may not need to make your own cons. I just think of that.)
Cons for Set instead of (:)
And a show instance for Set in order to show the result of union'.
Just trust your compiler and everything will be fine eventually! Good luck!
Upvotes: 1