Hello I am trying to write a very simple function in Haskell. However I can’t get “ghci” to accept my code.
data Field = A1 Int deriving (Show)
data FieldList = FL [Field] | Name String deriving (Show)
t :: Field
t = A1 1
u :: Int -> FieldList
u 0 = FL []
u n = FL [t]:(u (n-1))
And the error I get is this:
test.hs:9:7:
Couldn't match expected type `FieldList' with actual type `[a0]'
In the expression: (FL [t]) : (u (n - 1))
In an equation for `u': u n = (FL [t]) : (u (n - 1))
Can someone point me in the right direction?
Thanks!
Looking at the last line:
u has the type
Int -> FieldList.nis anInt, so(n - 1)is also anInt.u (n-1)would therefor be aFieldList.Function application has a higher precedence than operators, so the above line is equivalent to:
FL [t]is aFieldList.However,
(:)has the typea -> [a] -> [a]. You can see the types don’t match, so that is what is causing the problem.What you probably want to do is build up the list of
Fields (having type[Field]), and then turning that into aFieldList. Here is some stub code: