21 Commits

Author SHA1 Message Date
58aefa43c5 Генерация фраз 2024-12-09 19:00:08 +03:00
93bb0f247c N-граммы в действии 2024-12-09 16:48:54 +03:00
5700d5892a Добавил текст 2024-12-09 16:20:16 +03:00
f92d91571a Убрал пустые предложения 2024-12-09 14:47:22 +03:00
1c7c0a0fd6 Кириллица и show 2024-12-09 14:43:29 +03:00
de9d8681f3 splitText 2024-12-09 13:22:08 +03:00
92e1cb96ea Добавил текст задания 2024-12-09 03:58:59 +03:00
5921d5f9d4 Создал проект для второй части 2024-12-09 03:07:02 +03:00
65bb51146d Ошибка на русском 2024-12-09 02:47:38 +03:00
d89e7f09e7 Избавился от do 2024-12-09 02:31:17 +03:00
cf58fc9634 Ограничил доступ к вспомогательным функциям 2024-12-08 20:02:18 +03:00
3692629264 Первая версия Main, но пока с do-нотацией 2024-12-08 20:00:12 +03:00
1a45ec2330 Финальная функция для обработки строк 2024-12-08 19:45:21 +03:00
70d1742368 Что-то похожее на правду 2024-12-08 19:17:29 +03:00
c66352d500 Текст задания для части 1 2024-12-08 17:30:18 +03:00
917341a8c8 Код из лекции 2024-12-08 17:14:37 +03:00
2867c80305 Проект для первой части 2024-12-08 14:00:03 +03:00
a5b624a333 Добавил в отчёт примеры функций непроходящих тесты 2024-12-06 18:59:49 +03:00
4bcec8060d Неправильный вариант был 2024-12-06 18:48:23 +03:00
4982ace7a6 Добавил коменты 2024-12-05 19:20:41 +03:00
14d853c878 Забыл исправить опечатку 2024-12-05 18:13:01 +03:00
31 changed files with 847 additions and 2 deletions

4
coursework/part1/.gitignore vendored Normal file
View File

@@ -0,0 +1,4 @@
.stack-work/
*~
!task_part1.txt
!expressions.txt

View File

@@ -0,0 +1,11 @@
# Changelog for `part1`
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to the
[Haskell Package Versioning Policy](https://pvp.haskell.org/).
## Unreleased
## 0.1.0.0 - YYYY-MM-DD

26
coursework/part1/LICENSE Normal file
View File

@@ -0,0 +1,26 @@
Copyright 2024 Author name here
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@@ -0,0 +1 @@
# part1

View File

@@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain

View File

@@ -0,0 +1,12 @@
module Main (main) where
import Lib
main :: IO ()
main =
putStrLn "Введите имя файла:" >>
getLine >>= \fileName ->
readFile fileName >>= \content ->
let expressions = lines content in
putStrLn $ concatMap processExpression expressions

View File

@@ -0,0 +1,6 @@
100 * 100
40 + 30
50 / 2
5 / 2
62 - 32
78 - 500

View File

@@ -0,0 +1,59 @@
name: part1
version: 0.1.0.0
github: "githubuser/part1"
license: BSD-3-Clause
author: "Author name here"
maintainer: "example@example.com"
copyright: "2024 Author name here"
extra-source-files:
- README.md
- CHANGELOG.md
# Metadata used when publishing your package
# synopsis: Short description of your package
# category: Web
# To avoid duplicated efforts in documentation and dealing with the
# complications of embedding Haddock markup inside cabal files, it is
# common to point users to the README.md file.
description: Please see the README on GitHub at <https://github.com/githubuser/part1#readme>
dependencies:
- base >= 4.7 && < 5
ghc-options:
- -Wall
- -Wcompat
- -Widentities
- -Wincomplete-record-updates
- -Wincomplete-uni-patterns
- -Wmissing-export-lists
- -Wmissing-home-modules
- -Wpartial-fields
- -Wredundant-constraints
library:
source-dirs: src
executables:
part1-exe:
main: Main.hs
source-dirs: app
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- part1
tests:
part1-test:
main: Spec.hs
source-dirs: test
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- part1

View File

@@ -0,0 +1,67 @@
cabal-version: 2.2
-- This file has been generated from package.yaml by hpack version 0.37.0.
--
-- see: https://github.com/sol/hpack
name: part1
version: 0.1.0.0
description: Please see the README on GitHub at <https://github.com/githubuser/part1#readme>
homepage: https://github.com/githubuser/part1#readme
bug-reports: https://github.com/githubuser/part1/issues
author: Author name here
maintainer: example@example.com
copyright: 2024 Author name here
license: BSD-3-Clause
license-file: LICENSE
build-type: Simple
extra-source-files:
README.md
CHANGELOG.md
source-repository head
type: git
location: https://github.com/githubuser/part1
library
exposed-modules:
Lib
other-modules:
Paths_part1
autogen-modules:
Paths_part1
hs-source-dirs:
src
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints
build-depends:
base >=4.7 && <5
default-language: Haskell2010
executable part1-exe
main-is: Main.hs
other-modules:
Paths_part1
autogen-modules:
Paths_part1
hs-source-dirs:
app
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
, part1
default-language: Haskell2010
test-suite part1-test
type: exitcode-stdio-1.0
main-is: Spec.hs
other-modules:
Paths_part1
autogen-modules:
Paths_part1
hs-source-dirs:
test
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
, part1
default-language: Haskell2010

104
coursework/part1/src/Lib.hs Normal file
View File

@@ -0,0 +1,104 @@
{-# LANGUAGE InstanceSigs #-}
module Lib (
processExpression
) where
import Control.Applicative (Alternative(..))
import Data.Char (digitToInt, isDigit)
newtype Parser tok a =
Parser { runParser :: [tok] -> Maybe ([tok], a)}
instance Functor (Parser tok) where
fmap :: (a -> b) -> Parser tok a -> Parser tok b
fmap g (Parser p) = Parser f where
f xs = case p xs of
Nothing -> Nothing
Just (cs, c) -> Just (cs, g c)
instance Applicative (Parser tok) where
pure :: a -> Parser tok a
pure x = Parser $ \toks -> Just (toks, x)
(<*>) :: Parser tok (a -> b) -> Parser tok a -> Parser tok b
Parser u <*> Parser v = Parser f where
f xs = case u xs of
Nothing -> Nothing
Just (xs', g) -> case v xs' of
Nothing -> Nothing
Just (xs'', x) -> Just (xs'', g x)
instance Alternative (Parser tok) where
empty :: Parser tok a
empty = Parser $ \_ -> Nothing
(<|>) :: Parser tok a -> Parser tok a -> Parser tok a
Parser u <|> Parser v = Parser f where
f xs = case u xs of
Nothing -> v xs
z -> z
satisfy :: (tok -> Bool) -> Parser tok tok
satisfy pr = Parser f where
f (c:cs) | pr c = Just (cs, c)
f _ = Nothing
char :: Char -> Parser Char Char
char c = satisfy (== c)
digit :: Parser Char Char
digit = satisfy isDigit
skipSpaces :: Parser Char String
skipSpaces = many (char ' ')
number :: Parser Char Int
number = skipSpaces *> (strToInt <$> some digit)
where
strToInt = foldl (\acc x -> acc * 10 + digitToInt x) 0
data Operation = Add | Sub | Mul | Div deriving Show
operationToString :: Operation -> String
operationToString op = case op of
Add -> "+"
Sub -> "-"
Mul -> "*"
Div -> "/"
operationToOperator :: Operation -> (Int -> Int -> Int)
operationToOperator op = case op of
Add -> (+)
Sub -> (-)
Mul -> (*)
Div -> div
operation :: Parser Char Operation
operation = skipSpaces *> (
char '+' *> pure Add <|>
char '-' *> pure Sub <|>
char '*' *> pure Mul <|>
char '/' *> pure Div
)
expression :: Parser Char (Int, Operation, Int)
expression = (,,) <$> number <*> operation <*> number <* skipSpaces
calculateExpression :: (Int, Operation, Int) -> Int
calculateExpression (a, op, b) = (operationToOperator op) a b
processExpression :: String -> String
processExpression s = case runParser expression s of
Nothing -> err
Just (cs, (a, op, b)) -> case cs of
[] ->
show a ++ " " ++
operationToString op ++ " " ++
show b ++ " = " ++
show (calculateExpression (a, op, b)) ++ "\n"
_ -> err
where
err = error $ "Не удалось прочитать выражение: \"" ++ s ++ "\""

View File

@@ -0,0 +1,67 @@
# This file was automatically generated by 'stack init'
#
# Some commonly used options have been documented as comments in this file.
# For advanced use and comprehensive documentation of the format, please see:
# https://docs.haskellstack.org/en/stable/yaml_configuration/
# A 'specific' Stackage snapshot or a compiler version.
# A snapshot resolver dictates the compiler version and the set of packages
# to be used for project dependencies. For example:
#
# snapshot: lts-22.28
# snapshot: nightly-2024-07-05
# snapshot: ghc-9.6.6
#
# The location of a snapshot can be provided as a file or url. Stack assumes
# a snapshot provided as a file might change, whereas a url resource does not.
#
# snapshot: ./custom-snapshot.yaml
# snapshot: https://example.com/snapshots/2024-01-01.yaml
snapshot:
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/22/43.yaml
# User packages to be built.
# Various formats can be used as shown in the example below.
#
# packages:
# - some-directory
# - https://example.com/foo/bar/baz-0.0.2.tar.gz
# subdirs:
# - auto-update
# - wai
packages:
- .
# Dependency packages to be pulled from upstream that are not in the snapshot.
# These entries can reference officially published versions as well as
# forks / in-progress versions pinned to a git hash. For example:
#
# extra-deps:
# - acme-missiles-0.3
# - git: https://github.com/commercialhaskell/stack.git
# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
#
# extra-deps: []
# Override default flag values for project packages and extra-deps
# flags: {}
# Extra package databases containing global packages
# extra-package-dbs: []
# Control whether we use the GHC we find on the path
# system-ghc: true
#
# Require a specific version of Stack, using version ranges
# require-stack-version: -any # Default
# require-stack-version: ">=3.1"
#
# Override the architecture used by Stack, especially useful on Windows
# arch: i386
# arch: x86_64
#
# Extra directories used by Stack for building
# extra-include-dirs: [/path/to/dir]
# extra-lib-dirs: [/path/to/dir]
#
# Allow a newer minor version of GHC than the snapshot specifies
# compiler-check: newer-minor

View File

@@ -0,0 +1,13 @@
# This file was autogenerated by Stack.
# You should not edit this file by hand.
# For more information, please see the documentation at:
# https://docs.haskellstack.org/en/stable/lock_files
packages: []
snapshots:
- completed:
sha256: 08bd13ce621b41a8f5e51456b38d5b46d7783ce114a50ab604d6bbab0d002146
size: 720271
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/22/43.yaml
original:
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/22/43.yaml

View File

@@ -0,0 +1,10 @@
Часть 1
Написать синтаксический анализатор (парсер), разбирающий строки, прочитанные
из текстового файла .txt. Файл должен содержать значения и бинарные операции.
Значения: целые числа в десятичной системе счисления
Бинарные операции: сложение, вычитание, умножение, деление
Пример строки в файле: 234 * 43. Вычислить проанализированное выражение, вывести
его результат вычисления на экран. Пользователь вводит название файла.

View File

@@ -0,0 +1,2 @@
main :: IO ()
main = putStrLn "Test suite not yet implemented"

4
coursework/part2/.gitignore vendored Normal file
View File

@@ -0,0 +1,4 @@
.stack-work/
*~
!task_part2.txt
!text.txt

View File

@@ -0,0 +1,11 @@
# Changelog for `part2`
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to the
[Haskell Package Versioning Policy](https://pvp.haskell.org/).
## Unreleased
## 0.1.0.0 - YYYY-MM-DD

26
coursework/part2/LICENSE Normal file
View File

@@ -0,0 +1,26 @@
Copyright 2024 Author name here
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@@ -0,0 +1 @@
# part2

View File

@@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain

View File

@@ -0,0 +1,15 @@
module Main (main) where
import Lib
main :: IO ()
main =
putStrLn "Введите имя файла:" >>
getLine >>= \fileName ->
readFile fileName >>= \content ->
let sentences = splitText content in
let dict = buildDictionary sentences in
saveDictionary "dict.txt" dict >>
putStrLn "Введите слово или пару слов:" >>
getLine >>= \input ->
processInput dict input

View File

@@ -0,0 +1,62 @@
name: part2
version: 0.1.0.0
github: "githubuser/part2"
license: BSD-3-Clause
author: "Author name here"
maintainer: "example@example.com"
copyright: "2024 Author name here"
extra-source-files:
- README.md
- CHANGELOG.md
# Metadata used when publishing your package
# synopsis: Short description of your package
# category: Web
# To avoid duplicated efforts in documentation and dealing with the
# complications of embedding Haddock markup inside cabal files, it is
# common to point users to the README.md file.
description: Please see the README on GitHub at <https://github.com/githubuser/part2#readme>
dependencies:
- base >= 4.7 && < 5
- containers
- random
- unescaping-print
ghc-options:
- -Wall
- -Wcompat
- -Widentities
- -Wincomplete-record-updates
- -Wincomplete-uni-patterns
- -Wmissing-export-lists
- -Wmissing-home-modules
- -Wpartial-fields
- -Wredundant-constraints
library:
source-dirs: src
executables:
part2-exe:
main: Main.hs
source-dirs: app
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- part2
tests:
part2-test:
main: Spec.hs
source-dirs: test
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- part2

View File

@@ -0,0 +1,76 @@
cabal-version: 2.2
-- This file has been generated from package.yaml by hpack version 0.37.0.
--
-- see: https://github.com/sol/hpack
name: part2
version: 0.1.0.0
description: Please see the README on GitHub at <https://github.com/githubuser/part2#readme>
homepage: https://github.com/githubuser/part2#readme
bug-reports: https://github.com/githubuser/part2/issues
author: Author name here
maintainer: example@example.com
copyright: 2024 Author name here
license: BSD-3-Clause
license-file: LICENSE
build-type: Simple
extra-source-files:
README.md
CHANGELOG.md
source-repository head
type: git
location: https://github.com/githubuser/part2
library
exposed-modules:
Lib
other-modules:
Paths_part2
autogen-modules:
Paths_part2
hs-source-dirs:
src
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints
build-depends:
base >=4.7 && <5
, containers
, random
, unescaping-print
default-language: Haskell2010
executable part2-exe
main-is: Main.hs
other-modules:
Paths_part2
autogen-modules:
Paths_part2
hs-source-dirs:
app
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
, containers
, part2
, random
, unescaping-print
default-language: Haskell2010
test-suite part2-test
type: exitcode-stdio-1.0
main-is: Spec.hs
other-modules:
Paths_part2
autogen-modules:
Paths_part2
hs-source-dirs:
test
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
, containers
, part2
, random
, unescaping-print
default-language: Haskell2010

View File

@@ -0,0 +1,73 @@
module Lib where
import Data.Char (isLetter, toLower)
import Data.Map (Map)
import qualified Data.Map as Map
import Data.List (nub, tails)
import System.IO
import System.Random (StdGen, randomR, newStdGen) -- mkStdGen
import UnescapingPrint (ushow)
splitText :: String -> [[String]]
splitText text = filter (not . null) $ map (processSentence . words) (splitSentences text)
where
splitSentences :: String -> [String]
splitSentences [] = []
splitSentences s =
let (sentence, rest) = break isSeparator s
rest' = dropWhile isSeparator rest
in if null sentence
then splitSentences rest'
else sentence : splitSentences rest'
isSeparator :: Char -> Bool
isSeparator c = c `elem` ".!?;:()"
processSentence :: [String] -> [String]
processSentence = filter (not . null) . map cleanWord
cleanWord :: String -> String
cleanWord = map toLower . filter isLetter
buildDictionary :: [[String]] -> Map String [String]
buildDictionary sentences =
let bigrams = [ (w1, w2) | s <- sentences, (w1:w2:_) <- tails s ]
trigrams = [ (w1, w2, w3) | s <- sentences, (w1:w2:w3:_) <- tails s ]
singleKeys = foldr (\(w1, w2) acc -> Map.insertWith (++) w1 [w2] acc) Map.empty bigrams
singleKeys' = foldr (\(w1, w2, w3) acc -> Map.insertWith (++) w1 [w2 ++ " " ++ w3] acc) singleKeys trigrams
doubleKeys = foldr (\(w1, w2, w3) acc -> Map.insertWith (++) (w1 ++ " " ++ w2) [w3] acc) Map.empty trigrams
combined = Map.unionWith (++) singleKeys' doubleKeys
in Map.map nub combined
saveDictionary :: FilePath -> Map String [String] -> IO ()
saveDictionary filePath dict = withFile filePath WriteMode $ \h ->
mapM_ (\(k,v) -> hPutStrLn h $ ushow k ++ ": " ++ ushow v) (Map.toList dict)
generatePhrase :: Map String [String] -> String -> StdGen -> [String]
generatePhrase dict start initGenState =
-- let (len, initGenState') = (2 :: Int, initGenState)
let (len, initGenState') = randomR (2,15 :: Int) initGenState
in reverse $ gp start [] len initGenState'
where
gp :: String -> [String] -> Int -> StdGen -> [String]
gp key acc n genState
| n <= 0 = acc
| otherwise =
case Map.lookup key dict of
Nothing -> acc
Just [] -> acc
Just vals ->
let (i, newGenState) = randomR (0, length vals - 1) genState
next = vals !! i
in
gp next (next:acc) (n - length (words next)) newGenState
processInput :: Map String [String] -> String -> IO ()
processInput dict input =
if Map.member input dict then
-- let gen = mkStdGen 42 in
newStdGen >>= \gen ->
putStrLn $ unwords $ generatePhrase dict input gen
else
putStrLn "Нет в словаре"

View File

@@ -0,0 +1,69 @@
# This file was automatically generated by 'stack init'
#
# Some commonly used options have been documented as comments in this file.
# For advanced use and comprehensive documentation of the format, please see:
# https://docs.haskellstack.org/en/stable/yaml_configuration/
# A 'specific' Stackage snapshot or a compiler version.
# A snapshot resolver dictates the compiler version and the set of packages
# to be used for project dependencies. For example:
#
# snapshot: lts-22.28
# snapshot: nightly-2024-07-05
# snapshot: ghc-9.6.6
#
# The location of a snapshot can be provided as a file or url. Stack assumes
# a snapshot provided as a file might change, whereas a url resource does not.
#
# snapshot: ./custom-snapshot.yaml
# snapshot: https://example.com/snapshots/2024-01-01.yaml
snapshot:
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/22/43.yaml
# User packages to be built.
# Various formats can be used as shown in the example below.
#
# packages:
# - some-directory
# - https://example.com/foo/bar/baz-0.0.2.tar.gz
# subdirs:
# - auto-update
# - wai
packages:
- .
# Dependency packages to be pulled from upstream that are not in the snapshot.
# These entries can reference officially published versions as well as
# forks / in-progress versions pinned to a git hash. For example:
#
# extra-deps:
# - acme-missiles-0.3
# - git: https://github.com/commercialhaskell/stack.git
# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
#
extra-deps:
- unescaping-print-0.1@sha256:12774feb474d20ac3041d577b6307730c7376127f59bc79612d599a56d5009d8,1397
allow-newer: true
# Override default flag values for project packages and extra-deps
# flags: {}
# Extra package databases containing global packages
# extra-package-dbs: []
# Control whether we use the GHC we find on the path
# system-ghc: true
#
# Require a specific version of Stack, using version ranges
# require-stack-version: -any # Default
# require-stack-version: ">=3.1"
#
# Override the architecture used by Stack, especially useful on Windows
# arch: i386
# arch: x86_64
#
# Extra directories used by Stack for building
# extra-include-dirs: [/path/to/dir]
# extra-lib-dirs: [/path/to/dir]
#
# Allow a newer minor version of GHC than the snapshot specifies
# compiler-check: newer-minor

View File

@@ -0,0 +1,20 @@
# This file was autogenerated by Stack.
# You should not edit this file by hand.
# For more information, please see the documentation at:
# https://docs.haskellstack.org/en/stable/lock_files
packages:
- completed:
hackage: unescaping-print-0.1@sha256:12774feb474d20ac3041d577b6307730c7376127f59bc79612d599a56d5009d8,1397
pantry-tree:
sha256: b32edaf8cdef31ef02a0363e629ddf4949a6490777a6a797617f5dd376a46fce
size: 371
original:
hackage: unescaping-print-0.1@sha256:12774feb474d20ac3041d577b6307730c7376127f59bc79612d599a56d5009d8,1397
snapshots:
- completed:
sha256: 08bd13ce621b41a8f5e51456b38d5b46d7783ce114a50ab604d6bbab0d002146
size: 720271
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/22/43.yaml
original:
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/22/43.yaml

View File

@@ -0,0 +1,56 @@
Часть 2
Написать синтаксический анализатор разбирающий текст и генератор
продолжения текста по введенному слову. Для этого выполнить следующие
задачи:
1. Прочитать текст из файла, указываемого пользователем. Синтаксически
проанализировать текст согласно правилам: слова состоят только из букв;
предложения состоят только из слов и разделены символами: .!?;:().
Разбить текст на предложения. Удалить все символы пунктуации и цифры
из слов и предложений.
2. Составить модель N-грамм. Использовать модель биграмм и триграмм.
По списку предложений составить словарь. Ключами являются: одно слово,
либо пара слов. Значениями в словаре является список всех уникальных
возможных продолжений триграммы (т.е. список пар слов или одиночных
слов). Словарь сохранить в файл .txt.
Пример текста и словаря на его основе:
Текст: [a b, c d e! b c d? e b c# a ^d. a f; f.]
Словарь:
[
"a": ["b", "d", "f", "b c"];
"b": ["c", "c d"];
"c": ["d", "a", "d e", "a d"];
"d": ["e"];
"e": ["b", "b c"];
"a b" : ["c"];
"b c" : ["d", "a"];
"c d": ["e"];
"e b" : ["c"];
"c a": ["d"];
"f": []
]
3. Реализовать взаимодействие с пользователем. Пользователь вводит одно
слово или пару слов. Программа возвращает строку случайной длины в
диапазоне от 2 до 15 слов, если задаваемого пользователем слова нет в
ключах словаря, выдавать соответствующее сообщение. Фраза составляется
путем добавления случайного слова (или пары) из списка значений текущего
слова-ключа (или пары-ключа), до тех пор, пока либо не будет сформировано
предложение нужной длины, либо не будет достигнут ключ, у которого нет
значений.
4. Организовать диалог двух моделей N-грамм созданных на двух разных
текстах. Тексты для второй модели выбрать самостоятельно. Пользователь
задает начальное слово (или пару) и глубину М сообщений, которыми
обмениваются модели. Ответ модели основывается на последнем слове из
предложения оппонента (если последнее слово отсутствует в словаре, то
предпоследнее и т.д. пока не будет найдено подходящее слово или не
закончится предложение оппонента)
Автор текстов:
Чехов, Антон Павлович

View File

@@ -0,0 +1,2 @@
main :: IO ()
main = putStrLn "Test suite not yet implemented"

17
coursework/part2/text.txt Normal file
View File

@@ -0,0 +1,17 @@
<EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E2A8AB><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>: <20><><EFBFBD><EFBFBD><><E2AEAB><EFBFBD><EFBFBD>, <20><><EFBFBD><><E2AEAD><EFBFBD>. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ⮫쪮 <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20> <20><><EFBFBD><EFBFBD> <20><><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD>, <20><><EFBFBD><E1ADA8><EFBFBD><EFBFBD>, <20><><EFBFBD><><E1AFA5><EFBFBD> <20><>譨. <20><><20><> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><20><>-<2D>'<27><EFBFBD><E0A0AD><EFBFBD>. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> ⮫쪮 <20><><EFBFBD> <20><><20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>祭 祬<><E7A5AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, 㧫<><E3A7AB><EFBFBD> <20> <20><><EFBFBD><EFBFBD><E2AEAD><EFBFBD><EFBFBD>. <20><><20><> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><E7A8AD> <20> <20><><EFBFBD><E4A5A9><EFBFBD> <20><>饩. <20><>-<2D><> <20><><EFBFBD><><E1AFA8> <20><EFBFBD><EBA3AB><EFBFBD><EBA2A0> <20><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E0AEA4><EFBFBD> - <20><><EFBFBD> <20><><EFBFBD><EFBFBD>, <20> <20><><EFBFBD><E1AEAA> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E0A5AD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> - <20><><EFBFBD> <20><><EFBFBD>.
- <20><><EFBFBD><EFBFBD><EFBFBD>਩! - <20><><EFBFBD><E1AAAB><EFBFBD><EFBFBD><EFBFBD><><E2AEAB><EFBFBD><EFBFBD>, 㢨<><E3A2A8><EFBFBD><><E2AEAD><EFBFBD><EFBFBD>.- <20><> <20><> <20><><EFBFBD>? <20><><EFBFBD><EFBFBD><EFBFBD><20><><EFBFBD>! <20><><EFBFBD><EFBFBD><20><><EFBFBD>, ᪮<><20><><EFBFBD>!
- <20><><EFBFBD><EFBFBD>誨! - <20><><EFBFBD><E3ACA8><EFBFBD><><E2AEAD><EFBFBD>.- <20><><EFBFBD><EFBFBD>! <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>⢠! <20><><EFBFBD><20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>?
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><EFBFBD><E0AEA5><EFBFBD><20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EBA7A0><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><E0A5AC><EFBFBD> <20><><EFBFBD><EFBFBD> <20><> <20><><20><><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><>. <20><><EFBFBD> <20><20><><EFBFBD><EFBFBD><20><EFBFBD><E8A5AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
- <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD>! - <20><>砫 ⮭<><E2AEAD><EFBFBD> <20><><20><><EFBFBD><EFBFBD><EBA7A0><EFBFBD>.- <20><><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>! <20><><EFBFBD> <20><><EFBFBD><EFBFBD>ਧ! <20><>, <20><> <20><><EFBFBD><EFBFBD><20><> <20><> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>襭쪮! <20><><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><E1A0A2>, <20><><EFBFBD> <20> <20><><EFBFBD>! <20><><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><E8AEAD> <20><><E9A5A3><EFBFBD>! <20><> <20><>, <20><><EFBFBD><E1AFAE>! <20><>, <20><><EFBFBD> <20><> <20><>? <20><><EFBFBD><EFBFBD><EFBFBD>? <20><><EFBFBD><EFBFBD><EFBFBD>? <20><20><><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>... <20><><EFBFBD> <20><><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD>, <20><EFBFBD>, <20><EFBFBD><E0AEA6><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><E6A5AD><EFBFBD>... <20><><EFBFBD><EFBFBD><EFBFBD><E0A0AD>... <20> <20><><EFBFBD> <20><><EFBFBD> <20><><EFBFBD>, <20><><EFBFBD><E4A0AD><EFBFBD>, <20><EFBFBD><E7A5AD> III <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>. <20><><EFBFBD>, <20><><EFBFBD>, <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>⢠! <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><EFBFBD><E7A8AB><EFBFBD>!
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><><E8A0AF>.
- <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><EFBFBD><E7A8AB><EFBFBD>! - <20><EFBFBD><E0AEA4><EFBFBD><EFBFBD><EFBFBD><><E2AEAD><EFBFBD>.- <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><> <20><EFBFBD><E0A0A7><EFBFBD><EFBFBD>? <20><><EFBFBD><EFBFBD> <20><EFBFBD><E0A0A7><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><20><> <20><>, <20><><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><EFBFBD><E0AEA6>, <20> <20><><EFBFBD><EFBFBD> <20><EFBFBD><E4A8A0><20><> <20><>, <20><><EFBFBD> <20><><EFA1A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><EFBFBD>. <20><>-<2D><>... <20><><EFBFBD><20>뫨! <20><> <20><><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD>! <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>... <20> <20><><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD>, <20><EFBFBD><E0AEA6><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><E6A5AD><EFBFBD>... <20><><EFBFBD><EFBFBD><EFBFBD><E0A0AD>.
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><E2A0AB> <20><><><E1AFA8> <20><><EFBFBD><EFBFBD>.
- <20><>, <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD>? - <20><><EFBFBD><EFBFBD>ᨫ ⮫<><E2AEAB><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E0A6A5><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><> <20><>㣠.- <20><><EFBFBD><E3A6A8> <20><><EFBFBD>? <20><><EFBFBD><EFBFBD><EFBFBD><E3A6A8><EFBFBD>?
- <20><><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD>! <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>஬ 㦥 <20><><EFBFBD><20><><EFBFBD> <20> <20><EFBFBD><EFBFBD><E1ABA0> <20><><EFBFBD><EFBFBD>. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD>宥... <20><>, <20><> <20><><EFBFBD> <20> <20><><EFBFBD>! <20><><EFBFBD><EFBFBD> <20><EFBFBD> <20><><EFBFBD><20><><EFBFBD><EFBFBD>, <20> <20><><EFBFBD><EFBFBD><EFBFBD><E1A8A3><EFBFBD> <20><EFBFBD><20><> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>. <20><EFBFBD><E2ABA8><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><E1A8A3><EFBFBD>! <20><> <20><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD> <20><EFBFBD><E0AEA4>. <20><20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD>, ⮬<>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD>. <20><EFBFBD><E0AEA1><EFBFBD><EFBFBD><EFA5AC> <20><><EFBFBD>-<2D><><EFBFBD>. <20><><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E2A0AC><EFBFBD><EFBFBD>, <20><><E2A5AF><EFBFBD> <20><20><><EFBFBD><E0A5A2><EFBFBD><EFBFBD> <20><EFBFBD><E2AEAB><EFBFBD>砫쭨<E7A0AB><ECADA8><EFBFBD> <20><><> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>... <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><E3A6A8>. <20><>, <20> <20><> <20><><EFBFBD>? <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, 㦥 <20><><EFBFBD><EFBFBD><EFBFBD>? <20>?
- <20><><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,- ᪠<><E1AAA0><EFBFBD><><E2AEAB><EFBFBD><EFBFBD>.- <20><20><><><E2A0A9><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><E3A6A8><EFBFBD>... <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>.
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><><E1AAAE> <20><><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><E0A8A2><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>砩襩 <20><><EFBFBD><EBA1AA>; <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD> <20><> <20><><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EBAFA0><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>. <20><><EFBFBD> <20><> <20><EFBFBD><EAA5A6><EFBFBD><EFBFBD>, ᣮࡨ<E1A3AE><E0A1A8><EFBFBD>, <20><EFBFBD><E3A7A8><EFBFBD>... <20><><EFBFBD><><E7A5AC><EFBFBD><EFBFBD><EFBFBD>, 㧫<> <20> <20><><EFBFBD><EFBFBD><E2AEAD> <20><EFBFBD><EAA5A6><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E9A8AB><EFBFBD>... <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E0AEA4> <20><><EFBFBD><EFBFBD> <20><20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>; <20><><EFBFBD><E4A0AD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><E2A5A3><EFBFBD> <20><><EFBFBD> <20><EFBFBD><E3A3AE><EFBFBD><><E1A2AE><EFBFBD> <20><EFBFBD><E3ADA4><EFBFBD>...
- <20>, <20><><EFBFBD><EFBFBD> <20><EFBFBD><E0A5A2><EFBFBD><EFBFBD><E2A5AB>⢮... <20><EFBFBD> <20><><EFBFBD><EFBFBD>⭮-<2D>! <20><><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><><E1AAA0><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><20> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><20><><E2A0AA> <20><><EFBFBD><EFBFBD><ECACAE>-<2D>! <20><>-<2D><>-<2D>.
- <20><>, <20><><EFBFBD><EFBFBD><EFBFBD>! - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E9A8AB><><E2AEAB><EFBFBD><EFBFBD>.- <20><><EFBFBD><> <20><><EFBFBD><EFBFBD> ⮭? <20><> <20><><E2AEA1> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>⢠ - <20> <20><> <20><><EFBFBD> <20><><EFBFBD><><E7A8AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E2A0AD>!
- <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>... <20><><EFBFBD> <20><>-<2D>...- <20><><EFBFBD><EFBFBD><EFBFBD><E5A8AA><><E2AEAD><EFBFBD>, <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><EFBFBD><EAA5A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.- <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E2A8A2> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><EFBFBD><E0A5A2><EFBFBD><EFBFBD><E2A5AB>⢠... <20><EFBFBD> <20><><EFBFBD> <20><> <20><><EFBFBD><EFBFBD>⥫쭮<E2A5AB> <20><><EFBFBD><EFBFBD><EFBFBD>... <20><><EFBFBD> <20><><EFBFBD>, <20><><EFBFBD><EFBFBD> <20><EFBFBD><E0A5A2><EFBFBD><EFBFBD><E2A5AB>⢮, <20><><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><E4A0AD><EFBFBD>... <20><><EFBFBD><EFBFBD> <20><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><E0A0AD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><E0A0A7>...
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><20><20><><EFBFBD><EFBFBD><E0A0A7><EFBFBD> <20><><EFBFBD>-<2D><>, <20><> <20><> <20><><EFBFBD><EFBFBD> <20><><E2AEAD><EFBFBD><EFBFBD> <20><20><><EFBFBD><EFBFBD><EFBFBD> <20>⮫쪮 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, ᫠<><E1ABA0><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD>⥫쭮<E2A5AB> <20><><EFBFBD><E1ABAE>, <20><><EFBFBD><><E2A0A9><EFBFBD><EFBFBD><><EFBFBD><E2ADA8> <20><><EFBFBD><EFBFBD><E8ADA8>. <20><> <20><EFBFBD><E2A2A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><><E2AEAD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><E9A0AD> <20><><EFBFBD><EFBFBD>.
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><20><EFBFBD><E3ABAE><20> <20><><EFBFBD><EFBFBD><EFBFBD><E5A8AA>, <20><><EFBFBD> <20><><EFBFBD>: <<<3C><>-<2D><>-<2D><>>>. <20><><EFBFBD><EFBFBD> <20><>롭㫠<EBA1AD><E3ABA0>. <20><><EFBFBD><E4A0AD><EFBFBD> <20><><EFBFBD><E0AAAD> <20><><EFBFBD><EFBFBD><EFBFBD> <20> <20><EFBFBD><E0AEAD> <20><><EFBFBD><E0A0A6>. <20><><EFBFBD> <20><20><20><><EFBFBD><EFBFBD><20><EFBFBD><E8A5AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.

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