From f05a39da06010bd10a845fec22b3ab6b8df0010c Mon Sep 17 00:00:00 2001 From: Arity-T Date: Fri, 29 Nov 2024 19:33:07 +0300 Subject: [PATCH] filterByPredicate --- lab4/app/Main.hs | 7 ++++++- lab4/src/Lib.hs | 12 +++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lab4/app/Main.hs b/lab4/app/Main.hs index 33000ba..dd64d38 100644 --- a/lab4/app/Main.hs +++ b/lab4/app/Main.hs @@ -6,4 +6,9 @@ main :: IO () main = do putStrLn $ "Примеры работы `isCongruent`" putStrLn $ "isCongruent 10 12 2: " ++ show (isCongruent 10 12 2) - putStrLn $ "isCongruent 10 11 2: " ++ show (isCongruent 10 11 2) \ No newline at end of file + putStrLn $ "isCongruent 10 11 2: " ++ show (isCongruent 10 11 2) + + putStrLn $ "\nПример работы `filterByPredicate`" + let predicate x = x > 5 + putStrLn $ "filterByPredicate (>5) [1, 6, 3, 7, 2]: " + ++ show (filterByPredicate predicate [1 :: Int, 6, 3, 7, 2]) \ No newline at end of file diff --git a/lab4/src/Lib.hs b/lab4/src/Lib.hs index 382853f..5c91057 100644 --- a/lab4/src/Lib.hs +++ b/lab4/src/Lib.hs @@ -1,6 +1,16 @@ module Lib - ( isCongruent + ( isCongruent, + filterByPredicate ) where isCongruent :: Int -> Int -> Int -> Bool isCongruent a b d = a `mod` d == b `mod` d + + +filterByPredicate :: (a -> Bool) -> [a] -> [a] +filterByPredicate _ [] = [] +filterByPredicate predicate (x:xs) + | predicate x = x : filteredTail + | otherwise = filteredTail + where + filteredTail = filterByPredicate predicate xs \ No newline at end of file