Avatar of agbell

agbell's solution

to List Ops in the Haskell Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Implement basic list operations.

In functional languages list operations like length, map, and reduce are very common. Implement a series of basic list operations, without using existing functions.

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!

Submitting Incomplete Solutions

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

Tests.hs

{-# LANGUAGE DeriveAnyClass #-}

import Control.Exception (Exception, throw, evaluate)
import Test.Hspec        (Spec, describe, it, shouldBe, shouldThrow)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)

import Prelude hiding
    ( (++)
    , concat
    , filter
    , foldr
    , length
    , map
    , reverse
    )

import ListOps
    ( (++)
    , concat
    , filter
    , foldl'
    , foldr
    , length
    , map
    , reverse
    )

data StrictException = StrictException deriving (Eq, Show, Exception)

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

specs :: Spec
specs = do

    let big = 100000 :: Int

    describe "length" $ do
      it "of empty list" $
        length ([] :: [Int]) `shouldBe` 0
      it "of non-empty list" $
        length [1 .. 4 :: Int] `shouldBe` 4
      -- Track-specific test
      it "of large list" $
        length [1 .. big :: Int] `shouldBe` big

    describe "reverse" $ do
      it "of empty list" $
        reverse ([] :: [Int]) `shouldBe` []
      it "of non-empty list" $
        reverse [1 .. 100 :: Int] `shouldBe` [100 , 99 .. 1]

    describe "map" $ do
      it "of empty list" $
        map (+1) ([] :: [Int]) `shouldBe` []
      it "of non-empty list" $
        map (+1) [1, 3 .. 7 :: Int] `shouldBe` [2, 4 .. 8]

    describe "filter" $ do
      it "of empty list" $
        filter undefined ([] :: [Int]) `shouldBe` []
      it "of normal list" $
        filter odd [1 .. 4 :: Int] `shouldBe` [1, 3]

    describe "foldl'" $ do
      it "of empty list" $
        foldl' (+) (0 :: Int) [] `shouldBe` 0
      it "of non-empty list" $
        foldl' (+) (-3) [1 .. 4 :: Int] `shouldBe` 7
      -- Track-specific test
      it "of huge list" $
        foldl' (+) 0 [1 .. big] `shouldBe` big * (big + 1) `div` 2
      it "with non-commutative function" $
        foldl' (-) 10 [1 .. 4 :: Int] `shouldBe` 0
      -- Track-specific test
      it "is not just foldr . flip" $
        foldl' (flip (:)) [] "asdf" `shouldBe` "fdsa"
      -- Track-specific test
      it "is accumulator-strict (use seq or BangPatterns)" $
        evaluate (foldl' (flip const) () [throw StrictException, ()])
        `shouldThrow` (== StrictException)

    describe "foldr" $ do
      it "of empty list" $
        foldr (*) (2 :: Int) [] `shouldBe` 2
      it "of non-empty list" $
        foldr (+) 5 [1 .. 4 :: Int] `shouldBe` 15
      it "with non-commutative function" $
        foldr div 5 [2, 5 :: Int] `shouldBe` 2
      -- Track-specific test
      it "as id" $
        foldr (:) [] [1 .. big] `shouldBe` [1 .. big]
      -- Track-specific test
      it "as append" $
        foldr (:) [100 .. big] [1 .. 99] `shouldBe` [1 .. big]

    describe "++" $ do
      it "of empty lists" $
        [] ++ ([] :: [Int]) `shouldBe` []
      it "of empty and non-empty lists" $
        [] ++ [1 .. 4 :: Int] `shouldBe` [1 .. 4]
      it "of non-empty and empty lists" $
        [1 .. 4 :: Int] ++ [] `shouldBe` [1 .. 4]
      it "of non-empty lists" $
        [1 .. 3] ++ [4, 5 :: Int] `shouldBe` [1 .. 5]
      -- Track-specific test
      it "of large lists" $
        [1 .. big `div` 2] ++ [1 + big `div` 2 .. big] `shouldBe` [1 .. big]

    describe "concat" $ do
      it "of no lists" $
        concat ([] :: [[Int]]) `shouldBe` []
      it "of list of lists" $
        concat [[1, 2], [3], [], [4, 5, 6 :: Int]] `shouldBe` [1 .. 6]
      -- Track-specific test
      it "of large list of small lists" $
        concat (map (:[]) [1 .. big]) `shouldBe` [1 .. big]
module ListOps
  ( length
  , reverse
  , map
  , filter
  , foldr
  , foldl'
  , (++)
  , concat
  ) where

import Prelude hiding
  ( length, reverse, map, filter, foldr, (++), concat )

foldl' :: (b -> a -> b) -> b -> [a] -> b
foldl' _ z [] = z
foldl' f z (x:xs) = z' `seq` foldl' f z' xs
  where z' = f z x

foldr :: (a -> b -> b) -> b -> [a] -> b
foldr _ b [] = b
foldr f z (x:xs) = f x z'
  where z' = foldr f z xs

length :: [a] -> Int
length = foldl' (flip (const succ)) 0

reverse :: [a] -> [a]
reverse = foldl' (flip (:)) []

map :: (a -> b) -> [a] -> [b]
map f = foldr go []
  where go a bs = f a : bs

filter :: (a -> Bool) -> [a] -> [a]
filter f = foldr (go f) []
    where
      go f1 y ys
        | f1 y = y : ys
        | otherwise = ys

(++) :: [a] -> [a] -> [a]
(++) xs ys = foldr (:) ys xs

concat :: [[a]] -> [a]
concat = foldr (++) []

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?