I’m a novice with no functional programming experience in the past (yet, quite a bit of procedural/imperative programming experience). I’m having a bit of trouble with understanding how declaring your own datatype works.
For instance, say I declared a datatype:
data SomeThing = Int [Int]
how would you write a Haskell function which consumes a someData and produces a someData; only the produced data’s Int is the sum of all the elements in the consumed data’s [Int], and where the consumed value’s [Int] has had every element multiplied by 2 in the produced [Int].
This is obviously possible, but I have not found any answers that made sense to me after a web search.
First of all, you have a mistake in your data type declaration. From your question, you want a data type which contains an
Intand a list ofInt, but you’re missing a data constructor1. This is a label used when pattern matching, or when constructing new values of your data type.It’s common to name the constructor the same thing as the data type itself when there’s only one, but I’ve given them separate names here to avoid confusion.
Now it’s easy to write your function using pattern matching and this data constructor.
1Or rather, you have a data constructor called
Int, which is obviously not what you meant.