Reputation: 109
I am trying to construct a new value of type MyData
, which is a personal data type I defined, but I am getting an error. MyData
has a constructor List
which receives a list of MyData
values. I am trying to extract two elements of MyData
from the List
type and create a new one using the Two And
constructor. The parser works perfectly fine, I checked.
data MyData = Var String | Con Bool | Two TwoOp MyData MyData | List [MyData]
deriving Show
data Final= F MyData MyData deriving Show
data TwoOp = And | Or deriving Show
add_and (Right (F (List e1) (List e2))) i1 i2 = do
a<-e1!!i1
b<-e1!!i2
return (Two And a b)
val (Right (F e1 e2))=add_and (Right (F e1 e2)) 0 1
parse_value string_expression= val (parse getfinal "" string_expression)
If something is not clear, please ask. I have spent many hours trying to solve it, but don't know how.
Here is the error I am getting:
Couldn't match type `MyData' with `m MyData'
Expected type: [m MyData]
Actual type: [MyData]
Relevant bindings include
add_and :: Either t Final -> Int -> Int -> m MyData
(bound at D:\PF\final.hs:53:1)
In the first argument of `(!!)', namely `e1'
In a stmt of a 'do' block: a <- e1 !! i1
D:\PF\final.hs:55:5:
Couldn't match type `MyData' with `m MyData'
Expected type: [m MyData]
Actual type: [MyData]
Relevant bindings include
add_and :: Either t Final -> Int -> Int -> MyData
(bound at D:\PF\final.hs:53:1)
In the first argument of `(!!)', namely `e1'
In a stmt of a 'do' block: b <- e1 !! i2
Failed, modules loaded: none.
Upvotes: 0
Views: 157
Reputation: 52029
You don't want to use <-
to extract values from the list.
In this case you just want to use pattern matching:
add_and :: Final -> MyData
add_and (F (List (a:_)) (List (b:_))) = Two And a b
add_and _ = error "I don't know what to do here."
list1 :: MyData
list1 = List [ Var "abc" ]
list2 :: MyData
list2 = List [ Con False ]
final :: Final
final = F list1 list2
main = print $ add_and final
When run in ghci:
*Main> main
Two And (Var "abc") (Con False)
Upvotes: 3