I had a datatype, example:
data MyData = Something1 String
and then I had a function
myFunction :: MyData -> String
myFunction x = x
within myFunction I want to refer to the characters ie [‘S’,’o’,’m’,’e’……’1′] which are in my data type MyData. However, I get the following error:
Couldn’t match expected type
[Char]'MyData’
with actual type
Expected type: String
Actual type: MyData
As far as I understand [Char] is the same as String, and I have declared ‘Something1’ as String, so it should work?
[Char]is the same asString, but neither is the same asMyData. To access the string stored within your data type, you’ll need to use pattern matching:This is because the
datakeyword makes a completely new data type. If you only wanted an alias, you could also use thetypekeyword: