mirror of
https://github.com/iritmaximus/haskell.git
synced 2026-10-12 19:13:39 +03:00
Set 4b Ex 7
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user