Set 4b Ex 7

This commit is contained in:
Joel Kaasinen
2020-05-06 10:31:31 +03:00
parent cbeb4af8e4
commit d4a859a731
2 changed files with 23 additions and 1 deletions
+16
View File
@@ -97,3 +97,19 @@ myConcat xs = foldr concatHelper concatStart xs
concatStart = todo
concatHelper = todo
------------------------------------------------------------------------------
-- Ex 7: get all occurrences of the largest number in a list with a
-- fold. Implement largestHelper so that the given definition of largest works.
--
-- This is tricky.
--
-- Examples:
-- largest [] ==> []
-- largest [1,3,2] ==> [3]
-- largest [1,3,2,3] ==> [3,3]
largest :: [Int] -> [Int]
largest xs = foldr largestHelper [] xs
largestHelper = todo
+7 -1
View File
@@ -18,7 +18,8 @@ tests = [(1,"countNothings",[ex1_countNothings])
,(3,"myHead",[ex3_myHead_empty, ex3_myHead_full])
,(4,"myLast",[ex4_myLast_empty, ex4_myLast_full])
,(5,"sumAndLength",[ex5_sumAndLength])
,(6,"myConcat",[ex6_myConcat])]
,(6,"myConcat",[ex6_myConcat])
,(7,"largest",[ex7_largest])]
ex1_countNothings = property $ do
justs <- fmap (map Just) $ listOf (choose (0::Int,10))
@@ -63,3 +64,8 @@ ex6_myConcat =
forAll_ $ \(xs :: [[Int]]) ->
counterexample ("xs = "++show xs) $
$(testing' [|foldr concatHelper concatStart xs|]) (?==concat xs)
ex7_largest =
forAll_ $ \(NonEmpty (xs :: [Int])) ->
counterexample ("xs = "++show xs) $
$(testing' [|foldr largestHelper [] xs|]) (?==filter (==maximum xs) xs)