Skip to main content
edited body
Source Link
leventov
  • 446
  • 3
  • 8

First, your function is concatMap from Prelude.

More idiomatic ways to write it:

myFold :: (a -> [b]) -> [a] -> [b]
myFold = concat . map
-- or
myFold f = foldlfoldr (\acc\x xacc -> (f x) ++ acc) []

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

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

First, your function is concatMap from Prelude.

More idiomatic ways to write it:

myFold :: (a -> [b]) -> [a] -> [b]
myFold = concat . map
-- or
myFold f = foldl (\acc x -> (f x) ++ acc) []

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

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

First, your function is concatMap from Prelude.

More idiomatic ways to write it:

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

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

  1. Always compile at least with -O2
  2. Lists are slow. ++ operation especially. Consider Data.Sequence or even vectors.
  3. Avoid lazyness in your types
  4. Provide all small functions with INLINE pragma.
  5. And more...
Source Link
leventov
  • 446
  • 3
  • 8

First, your function is concatMap from Prelude.

More idiomatic ways to write it:

myFold :: (a -> [b]) -> [a] -> [b]
myFold = concat . map
-- or
myFold f = foldl (\acc x -> (f x) ++ acc) []

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

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