I'm a Haskell beginner and while building a simple project I came across this odd problem.
I have a list of functions e.g.
[f1, f2, f3 ... fn]
Which doesn't work because they don't have same type. Some have Ct -> String
, Ct -> Double
and Ct -> Maybe Double
What I wanted to do was apply the functions applicatively like this
[f1, f2, f3 ... fn] <*> [v1, v2, v3 ... vn]
where all v
s have same type Ct
The problem can be easily solved by just using list comprehension with tuples like
[(f1 a, f2 a, f3 a ... fn a) | a <- [v1, v2, v3 ... vn]]
which would look something like this
[ (f1 v1, f2 v1, f3 v1, f4, v1 ... fn v1)
, (f2 v1, f2 v2, f3 v2, f4 v2 ... fn v2)
, ...
, (fn v1, fn v2, fn v3, fn v4 ... fn vn)
]
but n
is quite a large number where you have to define a separate instance of show to say print them on terminal, which is one of the thing I want to do.
I know a few ways of overcoming this problem, one is by using tuples but dividing results into smaller chunks but I was wondering, is there a way of defining a datatype that can encapsulate multiple datatypes and still retain 'idiomaticity' and readable code of applicative style?