Avatar of agbell

agbell's solution

to Strain in the Haskell Track

Published at Jul 13 2018 · 0 comments
Test suite

Implement the keep and discard operation on collections. Given a collection and a predicate on the collection's elements, keep returns a new collection containing those elements where the predicate is true, while discard returns a new collection containing those elements where the predicate is false.

For example, given the collection of numbers:

  • 1, 2, 3, 4, 5

And the predicate:

  • is the number even?

Then your keep operation should produce:

  • 2, 4

While your discard operation should produce:

  • 1, 3, 5

Note that the union of keep and discard is all the elements.

The functions may be called keep and discard, or they may need different names in order to not clash with existing functions or concepts in your language.


Keep your hands off that filter/reject/whatchamacallit functionality provided by your standard library! Solve this one yourself using other basic tools instead.

Getting Started

For installation and learning resources, refer to the exercism help page.

Running the tests

To run the test suite, execute the following command:

stack test

If you get an error message like this...

No .cabal file found in directory

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

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:

stack setup

Running GHCi

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

stack ghci

Feedback, Issues, Pull Requests

The 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!


Conversation with James Edward Gray II https://twitter.com/jeg2

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


import Test.Hspec        (Spec, it, shouldBe)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)

import Data.List (isPrefixOf)

import Strain (discard, keep)

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

specs :: Spec
specs = do

    it "empty keep" $
        keep (<10) [] `shouldBe` ([] :: [Int])

    it "keep everything" $
        keep (<10) [1, 2, 3] `shouldBe` [1, 2, 3 :: Int]

    it "keep first and last" $
        keep odd [1, 2, 3] `shouldBe` [1, 3 :: Int]

    it "keep nothing" $
        keep even [1, 3, 5, 7] `shouldBe` ([] :: [Int])

    it "keep neither first nor last" $
        keep even [1, 2, 3] `shouldBe` [2 :: Int]

    it "keep strings" $
        keep ("z" `isPrefixOf`)
        ["apple", "zebra", "banana", "zombies", "cherimoya", "zealot"]
        ["zebra", "zombies", "zealot"]

    it "empty discard" $
        discard (< 10) [] `shouldBe` ([] :: [Int])

    it "discard everything" $
        discard (< 10) [1, 2, 3] `shouldBe` ([] :: [Int])

    it "discard first and last" $
        discard odd [1, 2, 3] `shouldBe` [2 :: Int]

    it "discard nothing" $
        discard even [1, 3, 5, 7] `shouldBe` [1, 3, 5, 7 :: Int]

    it "discard neither first nor last" $
        discard even [1, 2, 3] `shouldBe` [1, 3 :: Int]

    it "discard strings" $
        discard ("z" `isPrefixOf`)
        ["apple", "zebra", "banana", "zombies", "cherimoya", "zealot"]
        ["apple", "banana", "cherimoya"]

    it "keep non-strict" $
        (take 1 . keep (const True))
        ("yes" : error "keep should be lazier - don't look at list elements you don't need!")

    it "discard non-strict" $
        (take 1 . discard (const False))
        ("yes" : error "discard should be lazier - don't look at list elements you don't need!")
module Strain (keep, discard) where

keep, discard :: ( a -> Bool) -> [a] -> [a]
keep f = foldr (prependIf f) []
discard f = foldr (prependIf (not . f)) []

prependIf :: (a -> Bool) -> a -> [a] -> [a]
prependIf f a as
  | f a = a:as
  | otherwise = as

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?