I am quite new to Haskell, so apologies if there is any incorrect or confusing syntax. I have massively simplified what I am trying to do to make it more understandable.
To start, I have two user defined types:
data Foo = A String | B Int | C
type Bar = (Int, String, [Int])
I am trying to write a function such that:
myfunc :: Foo -> Bar -> Bar
--if Foo is A s,
-- increment intA
-- append s to stringA
-- return new Bar
myfunc (A s) intA stringA listA = (intA 1) stringA s listA
--if Foo is B i,
-- if listA[1]<listA[0]
-- increment intA by i
-- increment intA
-- return new Bar
-- else
-- increment intA
-- return new Bar
myfunc (B i) intA stringA (x:y:xs) = if y<x then ((intA i) 1 stringA xs) else ((intA 1) stringA xs)
--if Foo is C,
-- increment intA
-- add listA[0], listA[1]
-- prepend to listA
-- return new Bar
myfunc (C) intA stringA (top:second:xs) = (intA 1) stringA top second:xs
So, there is a different definition of myfunc for each possible value of Foo.
I then want to access the values in the second parameter, Bar, in order to return an 'updated' Bar, updated in different ways depending on the Foo used.
I am currently encountering an error on the myfunc (B i) version of myfunc:
Couldn't match type ‘(Int, String, [Int])’ with ‘[Bar]’
Expected type: [Bar]
Actual type: Bar
Which I interpret as the compiler expecting a list of Bar
, which I don't understand.
CodePudding user response:
A Bar
value is a single tuple, not 3 separate values. Match an existing value, and create a new value for return, just as you would any other 3-tuple.
myfunc (A s) (intA, stringA, listA) = ((intA 1), stringA s, listA)
-- etc