0
votes

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

1
What's the full error message? - melpomene
Your type declaration is for a function called union' (union prime), but your function is called union - it might not be related, but you might want to clean it up anyway. - Wander Nauta
notElem is a thing, btw. - melpomene
This error sounds like the way it is. parse error on input `::'. I tried your code. The error I got is at the part of "= |" "|". So maybe your error is about space indentation. So make sure everything is aligned and nested well. - Johnny Liao
And make sure you don't use tabs. Haskell indentation depends on space. en.wikibooks.org/wiki/Haskell/Indentation - Johnny Liao

1 Answers

1
votes

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:

  1. You don't need to put '=' before guard '|'

  2. You need equal space indentation for guard '|'

  3. missing '=' for the second guard expression.

  4. called union instead of union'

  5. 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.)

  6. Cons for Set instead of (:)

  7. 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!