læran91
læran91

Reputation: 293

error: parse error on input `::' in haskell?

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

Answers (1)

Johnny Liao
Johnny Liao

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:

  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!

Upvotes: 1

Related Questions