I'm trying to write the code for Haskell concatmap without using the ++
operator where
concatMap :: (a -> [b]) -> [a] -> [b]
and producing the same result of
concatMap f = foldr ((++) . f) []
I'm quite new to Haskell and this was just an exercise I found. Actually, I do not even know if this can be done.
The ConcatMap creates an inner observable, subscribes to it, and emits its value as observable. It emits the value in the order in which it creates the observable. The Following example shows the difference between ConcatMap & Map. The Map operator below maps the value coming from the source observable to a new value by multiplying it by 2.
Working of map in Haskell is as follows: Whenever we want to apply a function on each element of a given list and produce a new list consisting of the updated elements, then we make use of a function called map () function in Haskell.
The Angular ConcatMap maps each value from the source observable into an inner observable, subscribes to it, and then starts emitting the values from it replacing the original value. It creates a new inner observable for every value it receives from the Source.
What does concatMap do? I know what concat and map do. Is it just both of them put together or is it a completely different function? Show activity on this post. Yes, the concatMap function is just concat and map put together. Hence the name. Putting functions together simply means composing them:
Here's a way that makes the state of the computation explicit:
concatMap :: (a -> [b]) -> [a] -> [b]
concatMap f = go []
where
-- We have b values; use one.
go (b:bs) as = b : go bs as
-- No bs left; get some more.
go [] (a:as) = go (f a) as
-- Nothing left; we're done.
go [] [] = []
This maintains the current list of b
s, filling it up whenever it's empty.
This might be cheating, but how about:
myConcatMap f s = concat (map f s)
The concat
function uses some sort of ++
in its source code, so that is why you might not like it. You can try to use an alternative concat
that does list comprehensions, it has a more "from scratch" feeling.
myconcat ll = [y | x <- ll, y <- x]
You can use the fact that foldr (:)
= flip (++)
concatMap f = foldr (flip (foldr (:)) . f) []
Or pointfree:
concatMap = flip foldr [] . (flip (foldr (:)) .)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With