Skip to content

Latest commit

 

History

History
61 lines (45 loc) · 1.91 KB

index.org

File metadata and controls

61 lines (45 loc) · 1.91 KB

Exercism.io → Haskell → Accumulate

Accumulate

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

Restrictions

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.

Source

Conversation with James Edward Gray II view source

Solution

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. 👍

Tests

runhaskell -Wall accumulate_test.hs
Cases: 6  Tried: 6  Errors: 0  Failures: 0