Implement a fold-like Function in Haskell Let's say I need to write a function that applies a function to each item in a List, and then appends it to an accumulator. When the List is empty, return the accumulator.

go :: [a] -&gt; (a -&gt; [b]) -&gt; [b] -&gt; [b]
go [] f acc = acc
go (x:xs) f acc = go xs f (acc ++ f x)

myFold :: [a] -&gt; (a -&gt; [b]) -&gt; [b]
myFold as f = go as f []


I used the go function so that I wouldn't force the myFold caller to have to provide an empty [b] type. Also, I used the go function to achieve tail recursion. 

In Scala, I would've put @annotation.tailrec to make the compilation fail if the compiler could not perform tail-call optimization.

Is the above code idiomatic in Haskell?
First, your function is concatMap from Prelude.

More idiomatic ways to write it:

myFold :: (a -&gt; [b]) -&gt; [a] -&gt; [b]
myFold = concat . map
-- or
myFold f = foldr (\x acc -&gt; (f x) ++ acc) []


If efficiency is a concern, it's a completely different question. Recommended steps in priority order:


Always compile at least with -O2
Lists are slow. ++ operation especially. Consider Data.Sequence or even vectors.
Avoid lazyness in your types
Provide all small functions with INLINE pragma.
And more...