For example take a function that returns a list:
multAddFunc :: Integer -> Integer -> [Integer]
multAddFunc a b = [a*b, a+b]
*Main> multAddFunc 2 3
[6,5]
Now lets say we had two lists of numbers [1,2,3] [4,5,6] And we wanted to write a function that processed them:
*Main> [ multAddFunc x y | x<-[1,2,3], y<-[4,5,6] ]
[[4,5],[5,6],[6,7],[8,6],[10,7],[12,8],[12,7],[15,8],[18,9]]
This just returned a list of lists, observe
*Main> :t [ multAddFunc x y | x<-[1,2,3], y<-[4,5,6] ]
[[4,5],[5,6],[6,7],[8,6],[10,7],[12,8],[12,7],[15,8],[18,9]]
But what if we wanted a flattened list? For this we would have to concatenate the lists together using the ++ operator
*Main> :t (++)
(++) :: [a] -> [a] -> [a]
listMath :: [Integer] -> [Integer] -> [Integer]
listMath [] [] = []
listMath (x:xs) (y:ys) = (multAddFunc x y) ++ (listMath xs ys)
*Main> listMath [1,2,3] [4,5,6]
[4,5,10,7,18,9]
And there we have it a flattened list
No comments:
Post a Comment