Input syntax error (unexpected `= ')

I am actually new to Haskell. I wrote this code for Queue, but the last line always encounters this syntax error.

Syntax error in input (unexpected '='). 

I really can't understand what's wrong :(

module Queue where
data Queue a = Q[a] deriving Show

class QDS q a where
  pop :: q a -> (a, q a)
  push :: q a -> a -> q a
  lengthQS :: q a -> Int
  isEmpty :: q a -> Bool

instance QDS Queue a where
  pop (Q (x:xs)) = (x, (Q xs))
  push (Q x) a = (Q (x ++ [a])) 
  lengthQS (Q x) = length x
  isEmpty q = lengthQS q == 0  -- This line fails
+4
source share
1 answer

Solution without compiler extensions:

Remove the type parameter ain both the class and the instance:

class QDS q where
instance QDS Queue where

and it compiles fine without language extensions.

A Need for MultiParamTypeClasses

The reason the compiler wants MultiParamTypeClassesit is obvious: yours QDSprovides two type parameters. If you did not include this instance, MultiParamTypeClass is enough.

The need for flexible operations

instance QDS Queue Int where . - , , a , Int.

?

a. , Functor, Foldable ..

, : , . . , ( , haskell).

+1

Source: https://habr.com/ru/post/1584410/


All Articles