4 Commits

3 changed files with 29 additions and 2 deletions

View File

@@ -127,7 +127,7 @@
\large{Отчет по лабораторной работе №4}\\ \large{Отчет по лабораторной работе №4}\\
\large{по дисциплине}\\ \large{по дисциплине}\\
\large{<<Функциональное программирование>>}\\ \large{<<Функциональное программирование>>}\\
\large{Вариант 20}\\ \large{Вариант 12}\\
\hfill \break \hfill \break
% \hfill \break % \hfill \break
@@ -256,6 +256,27 @@ propFilterByPredicateAlwaysTrue xs =
filterByPredicate (\_ -> True) xs == xs filterByPredicate (\_ -> True) xs == xs
\end{lstlisting} \end{lstlisting}
В листинге~\ref{lst:broken1} представлена реализация функции \texttt{filterByPredicate}, которая не пройдёт ни один из перечисленных тестов. А в листинге~\ref{lst:broken2} представлена реализация, которая не пройдёт два из трёх тестов, первый и третий.
\begin{lstlisting}[caption={Пример реализации функции filterByPredicate, которая не пройдёт ни один тест.}, label={lst:broken1}]
filterByPredicate :: (a -> Bool) -> [a] -> [a]
filterByPredicate _ [] = []
filterByPredicate predicate (x:xs)
| not $ predicate x = x : x : filteredTail
| otherwise = x : x : filteredTail
where
filteredTail = filterByPredicate predicate xs
\end{lstlisting}
\begin{lstlisting}[caption={Пример реализации функции filterByPredicate, которая не пройдёт первый и третий тесты.}, label={lst:broken2}]
filterByPredicate :: (a -> Bool) -> [a] -> [a]
filterByPredicate _ [] = []
filterByPredicate predicate (x:xs)
| not $ predicate x = x : filteredTail
| otherwise = filteredTail
where
filteredTail = filterByPredicate predicate xs
\end{lstlisting}
\subsection{Запуск тестов} \subsection{Запуск тестов}

View File

@@ -4,7 +4,7 @@ module Lib
) where ) where
isCongruent :: Int -> Int -> Int -> Bool isCongruent :: Int -> Int -> Int -> Bool
isCongruent a b d = a `mod` d + 1 == b `mod` d isCongruent a b d = a `mod` d == b `mod` d
filterByPredicate :: (a -> Bool) -> [a] -> [a] filterByPredicate :: (a -> Bool) -> [a] -> [a]

View File

@@ -1,27 +1,33 @@
import Test.QuickCheck import Test.QuickCheck
import Lib import Lib
-- Если два числа равны по модулю, то их разность делится на модуль
propCongruentDifference :: Int -> Int -> Int -> Property propCongruentDifference :: Int -> Int -> Int -> Property
propCongruentDifference a b m = propCongruentDifference a b m =
m /= 0 ==> isCongruent a b m == ((a - b) `mod` m == 0) m /= 0 ==> isCongruent a b m == ((a - b) `mod` m == 0)
-- Равенство по модулю является симметричным
propCongruentSymmetric :: Int -> Int -> Int -> Property propCongruentSymmetric :: Int -> Int -> Int -> Property
propCongruentSymmetric a b m = propCongruentSymmetric a b m =
m /= 0 ==> isCongruent a b m == isCongruent b a m m /= 0 ==> isCongruent a b m == isCongruent b a m
-- Если одно число равно другому, то они равны по любому модулю
propCongruentEqualNumbers :: Int -> Int -> Property propCongruentEqualNumbers :: Int -> Int -> Property
propCongruentEqualNumbers a m = propCongruentEqualNumbers a m =
m /= 0 ==> isCongruent a a m == True m /= 0 ==> isCongruent a a m == True
-- Все элементы результата удовлетворяют предикату
propFilterByPredicateSatisfiesPredicate :: Fun Int Bool -> [Int] -> Bool propFilterByPredicateSatisfiesPredicate :: Fun Int Bool -> [Int] -> Bool
propFilterByPredicateSatisfiesPredicate (Fun _ predicate) xs = propFilterByPredicateSatisfiesPredicate (Fun _ predicate) xs =
all predicate (filterByPredicate predicate xs) all predicate (filterByPredicate predicate xs)
-- Длина результата не превышает длину исходного списка
propFilterByPredicateLength :: Fun Int Bool -> [Int] -> Bool propFilterByPredicateLength :: Fun Int Bool -> [Int] -> Bool
propFilterByPredicateLength (Fun _ predicate) xs = propFilterByPredicateLength (Fun _ predicate) xs =
length (filterByPredicate predicate xs) <= length xs length (filterByPredicate predicate xs) <= length xs
-- Если предикат всегда возвращает True, то результат совпадает с исходным списком
propFilterByPredicateAlwaysTrue :: [Int] -> Bool propFilterByPredicateAlwaysTrue :: [Int] -> Bool
propFilterByPredicateAlwaysTrue xs = propFilterByPredicateAlwaysTrue xs =
filterByPredicate (\_ -> True) xs == xs filterByPredicate (\_ -> True) xs == xs