From e3e3dc1ffd5dc8111dd348a945ce3be73217ae0b Mon Sep 17 00:00:00 2001 From: Tony Morris Date: Tue, 10 Sep 2013 15:57:53 +1000 Subject: [PATCH] fix tests --- src/Core.hs | 2 -- src/Monad/Monad.hs | 6 +++--- src/Structure/List.hs | 16 ++++++++++++++++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/Core.hs b/src/Core.hs index 569954c2c..a0d9ef785 100644 --- a/src/Core.hs +++ b/src/Core.hs @@ -39,7 +39,6 @@ module Core( , getChar , concatMap , length - , product , replicate , foldr , (++) @@ -86,7 +85,6 @@ import Prelude( , getChar , concatMap , length - , product , replicate , foldr , (++) diff --git a/src/Monad/Monad.hs b/src/Monad/Monad.hs index 05b6fa48b..4a854a9ab 100644 --- a/src/Monad/Monad.hs +++ b/src/Monad/Monad.hs @@ -187,7 +187,7 @@ apply = -- >>> lift2 (+) Empty (Full 8) -- Empty -- --- >>> lift2 (+) length sum [4,5,6] +-- >>> lift2 (+) len sum (listh [4,5,6]) -- 18 lift2 :: Monad m => @@ -222,7 +222,7 @@ lift2 = -- >>> lift3 (\a b c -> a + b + c) Empty Empty (Full 9) -- Empty -- --- >>> lift3 (\a b c -> a + b + c) length sum product [4,5,6] +-- >>> lift3 (\a b c -> a + b + c) len sum product (listh [4,5,6]) -- 138 lift3 :: Monad m => @@ -258,7 +258,7 @@ lift3 = -- >>> lift4 (\a b c d -> a + b + c + d) Empty Empty (Full 9) (Full 10) -- Empty -- --- >>> lift4 (\a b c d -> a + b + c + d) length sum product (sum . filter even) [4,5,6] +-- >>> lift4 (\a b c d -> a + b + c + d) len sum product (sum . filter even) (listh [4,5,6]) -- 148 lift4 :: Monad m => diff --git a/src/Structure/List.hs b/src/Structure/List.hs index d735c2e51..9dd8567f1 100644 --- a/src/Structure/List.hs +++ b/src/Structure/List.hs @@ -67,6 +67,19 @@ headOr :: headOr = error "todo" +-- | The product of the elements of a list. +-- +-- >>> product (1 :. 2 :. 3 :. Nil) +-- 6 +-- +-- >>> product (1 :. 2 :. 3 :. 4 :. Nil) +-- 24 +product :: + List Int + -> Int +product = + foldLeft (*) 1 + -- Exercise 2 -- Relative Difficulty: 2 -- Correctness: 2.5 marks @@ -79,6 +92,9 @@ headOr = -- >>> sum (1 :. 2 :. 3 :. Nil) -- 6 -- +-- >>> sum (1 :. 2 :. 3 :. 4 :. Nil) +-- 10 +-- -- prop> foldLeft (-) (sum x) x == 0 sum :: List Int