Implement the `accumulate` operation, which, given a collection and an operation to perform on each element of the collection, returns a new collection containing the result of applying that operation to each element of the input collection.
For example, given the collection of numbers:
- 1, 2, 3, 4, 5
And the operation:
- square a number
Your code should be able to produce the collection of squares:
- 1, 4, 9, 16, 25
Keep your hands off that collect/map/fmap/whatchamacallit functionality provided by your standard library! Solve this one yourself using other basic tools instead.
Elixir specific: it’s perfectly fine to use Enum.reduce
or
Enumerable.reduce
.
Check out Exercism Help for instructions to get started writing Haskell.
Conversation with James Edward Gray II view source
module Accumulate where
\[ \text{accumulate}\ f\ \text{coll} = \left\{\ f(x)\ |\ x ∈ \text{coll}\ \right\} \]
accumulate :: (a -> b) -> [a] -> [b]
accumulate f coll = [(f x) | x <- coll]
Once again, Haskell syntax almost mirrors mathematical notation. 👍
runhaskell -Wall accumulate_test.hs
Cases: 6 Tried: 6 Errors: 0 Failures: 0