Skip to content

Commit

Permalink
Add accumulate in haskell
Browse files Browse the repository at this point in the history
  • Loading branch information
Sgoettschkes committed Oct 5, 2023
1 parent 23a5a6b commit d0a2589
Show file tree
Hide file tree
Showing 9 changed files with 249 additions and 0 deletions.
5 changes: 5 additions & 0 deletions haskell/accumulate/Accumulate.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Accumulate(accumulate) where

accumulate :: (t -> a) -> [t] -> [a]
accumulate _ [] = []
accumulate f (x:xs) = f x : accumulate f xs
87 changes: 87 additions & 0 deletions haskell/accumulate/HELP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Help

## Running the tests

To run the test suite, execute the following command:

```bash
stack test
```

#### If you get an error message like this...

```
No .cabal file found in directory
```

or

```
RedownloadInvalidResponse Request {
...
}
"/home/username/.stack/build-plan/lts-xx.yy.yaml" (Response {responseStatus = Status {statusCode = 404, statusMessage = "Not Found"},
```

You are probably running an old stack version and need
to upgrade it. Try running:

```bash
stack upgrade
```

Or see other options for upgrading at [Stack documentation](https://docs.haskellstack.org/en/stable/install_and_upgrade/#upgrade).

#### Otherwise, if you get an error message like this...

```
No compiler found, expected minor version match with...
Try running "stack setup" to install the correct GHC...
```

Just do as it says and it will download and install
the correct compiler version:

```bash
stack setup
```

If you want to play with your solution in GHCi, just run the command:

```bash
stack ghci
```

## Submitting your solution

You can submit your solution using the `exercism submit src/Accumulate.hs package.yaml` command.
This command will upload your solution to the Exercism website and print the solution page's URL.

It's possible to submit an incomplete solution which allows you to:

- See how others have completed the exercise
- Request help from a mentor

## Need to get help?

If you'd like help solving the exercise, check the following pages:

- The [Haskell track's documentation](https://exercism.org/docs/tracks/haskell)
- The [Haskell track's programming category on the forum](https://forum.exercism.org/c/programming/haskell)
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)

Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.

## Getting Started

Please refer to the [installation](https://exercism.io/tracks/haskell/installation)
and [learning](https://exercism.io/tracks/haskell/learning) help pages.

## Feedback, Issues, Pull Requests

The [exercism/haskell](https://github.com/exercism/haskell) repository on
GitHub is the home for all of the Haskell exercises.

If you have feedback about an exercise, or want to help implementing a new
one, head over there and create an issue. We'll do our best to help you!
48 changes: 48 additions & 0 deletions haskell/accumulate/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Accumulate

Welcome to Accumulate on Exercism's Haskell Track.
If you need help running the tests or submitting your code, check out `HELP.md`.

## Instructions

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.

Given the collection of numbers:

- 1, 2, 3, 4, 5

And the operation:

- square a number (`x => x * x`)

Your code should be able to produce the collection of squares:

- 1, 4, 9, 16, 25

Check out the test suite to see the expected function signature.

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

## Source

### Created by

- @etrepum

### Contributed to by

- @iHiD
- @kytrinyx
- @mttakai
- @petertseng
- @ppartarr
- @rbasso
- @sshine
- @tejasbubane

### Based on

Conversation with James Edward Gray II - https://twitter.com/jeg2
34 changes: 34 additions & 0 deletions haskell/accumulate/accumulate.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
cabal-version: 1.12

-- This file has been generated from package.yaml by hpack version 0.36.0.
--
-- see: https://github.com/sol/hpack

name: accumulate
version: 0.1.0.3
build-type: Simple

library
exposed-modules:
Accumulate
other-modules:
Paths_accumulate
hs-source-dirs:
src
ghc-options: -Wall
build-depends:
base
default-language: Haskell2010

test-suite test
type: exitcode-stdio-1.0
main-is: Tests.hs
other-modules:
Paths_accumulate
hs-source-dirs:
test
build-depends:
accumulate
, base
, hspec
default-language: Haskell2010
21 changes: 21 additions & 0 deletions haskell/accumulate/package.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: accumulate
version: 0.1.0.3

dependencies:
- base

library:
exposed-modules: Accumulate
source-dirs: src
ghc-options: -Wall
# dependencies:
# - foo # List here the packages you
# - bar # want to use in your solution.

tests:
test:
main: Tests.hs
source-dirs: test
dependencies:
- accumulate
- hspec
4 changes: 4 additions & 0 deletions haskell/accumulate/src/Accumulate.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Accumulate(accumulate) where
accumulate :: (t -> a) -> [t] -> [a]
accumulate _ [] = []
accumulate f (x:xs) = f x : accumulate f xs
1 change: 1 addition & 0 deletions haskell/accumulate/stack.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
resolver: lts-20.18
12 changes: 12 additions & 0 deletions haskell/accumulate/stack.yaml.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file was autogenerated by Stack.
# You should not edit this file by hand.
# For more information, please see the documentation at:
# https://docs.haskellstack.org/en/stable/lock_files

packages: []
snapshots:
- completed:
sha256: 9fa4bece7acfac1fc7930c5d6e24606004b09e80aa0e52e9f68b148201008db9
size: 649606
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/20/18.yaml
original: lts-20.18
37 changes: 37 additions & 0 deletions haskell/accumulate/test/Tests.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import Data.Char (toUpper)
import Test.Hspec (Spec, it, shouldBe)
import Test.Hspec.Runner (configFailFast, defaultConfig, hspecWith)

import Accumulate (accumulate)

main :: IO ()
main = hspecWith defaultConfig {configFailFast = True} specs

specs :: Spec
specs = do

let square x = x * x :: Int

it "empty accumulation" $
accumulate square []
`shouldBe` []

it "accumulate squares" $
accumulate square [1, 2, 3]
`shouldBe` [1, 4, 9]

it "accumulate upcases" $
accumulate (map toUpper) ["hello", "world"]
`shouldBe` ["HELLO", "WORLD"]

it "accumulate reversed strings" $
accumulate reverse ["the", "quick", "brown", "fox", "etc"]
`shouldBe` ["eht", "kciuq", "nworb", "xof", "cte"]

it "accumulate recursively" $
accumulate (\c -> accumulate ((c:) . show) ([1, 2, 3] :: [Int])) "abc"
`shouldBe` [["a1", "a2", "a3"], ["b1", "b2", "b3"], ["c1", "c2", "c3"]]

it "accumulate non-strict" $
take 1 (accumulate id ("nice work!" : error "accumulate should be even lazier, don't use reverse!"))
`shouldBe` ["nice work!"]

0 comments on commit d0a2589

Please sign in to comment.