Search code examples
functionhaskelltypesoperation

haskell create new operator, and control error


I have the following structure:

Terra [['0','1','0','1'],['0','1','0','1'],['1','0','G','1']]

and de function:

esTerra:: Taulell -> (Int,Int) -> Bool
esTerra t (u,d) = 
    case t!!!u!!!d of
        Left e -> False
        Right p -> True


(!!!) :: [a] -> Int -> Either Bool a 
xs !!! n | n < 0 = Left False   -- error Exception: Prelude.!!:...
[] !!! _ = Left False           -- error Exception: Prelude.!!:...
(x:_) !!! 0 = Right x
(_:xs) !!! n = xs!!!(n-1)

the function !!! is equal the operation !! but when you have to return an error message returns False

but return error:

Couldn't match expected type ‘[a0]’
            with actual type ‘Either Bool [Char]’
In the first argument of ‘(!!!)’, namely ‘t !!! u’
In the expression: t !!! u !!! d
In the expression:
  case t !!! u !!! d of {
    Left e -> False
    Right p -> True }

Because?

Thank's


Solution

  • I don't know what Taulell is, let's guess Taulell = [[a]] for some a.

    We have

    t :: [[a]]  -- Taulell
    u :: Int
    d :: Int
    

    hence

    t !!! u :: Either Bool [a]
    

    Then we write

    (t !!! u) !!! d
    

    but here the leftmost argument is not a list, it's an Either Bool [a]. Hence a type error arises.

    Instead, we could try, e.g.

    case t !!! u of
       Left b -> ...
       Right l -> case l !!! d of
                     Left c  -> ...
                     Rigth w -> ...