Skip to content

Commit

Permalink
add a test that will fail given a strict implementation of accumulate
Browse files Browse the repository at this point in the history
  • Loading branch information
etrepum committed Feb 4, 2015
1 parent 093a179 commit d238a33
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
5 changes: 5 additions & 0 deletions accumulate/accumulate_test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ accumulateTests =
, testCase "accumulate recursively" $
[["a1", "a2", "a3"], ["b1", "b2", "b3"], ["c1", "c2", "c3"]] @=?
accumulate (\c -> accumulate ((c:) . show) ([1, 2, 3] :: [Int])) "abc"
, testCase "accumulate non-strict" $
["nice work!"] @=?
take 1 (accumulate id
("nice work!" :
error "accumulate should be even lazier, don't use reverse!"))
]
17 changes: 11 additions & 6 deletions accumulate/example.hs
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
module Accumulate (accumulate) where

accumulate :: (a -> b) -> [a] -> [b]
accumulate = map
{-
accumulate _ [] = []
accumulate f (x:xs) = f x : accumulate f xs

-- Some other definitions that are easily possible:
{-
accumulate = fmap
-- Some other reasonable definitions:
accumulate f = foldr ((:) . f) []
accumulate f xs = [f x | x <- xs]
accumulate f (x:xs) = f x : accumulate xs
accumulate _ [] = []
-- Commonly submitted inefficient solution (we test for this now):
accumulate f xs = accumulate' []
where
accumulate' acc [] = acc
accumulate' acc (x:xs) = accumulate' (f x : acc) xs
-}

0 comments on commit d238a33

Please sign in to comment.