Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions hw2/hw2.Test/FunctionsTest.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
module hw2.Test

open NUnit.Framework
open FsCheck
open FsUnit
open Program

[<Test>]
let countEvenNumbersCheckWithNumbers () =
let result = countEvenNumbersFilter [1; 2; 3; 4; 5; 6]

result |> should equal 3

[<Test>]
let countEvenNumbersCheckEqual () =
let check x =

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

было бы так же неплохо проверить на корректность одну из функций. А то окажется, что все функции эквивалентны, но некорректны.

let mapFilter = countEvenNumbersMap x = countEvenNumbersFilter x
let mapFold = countEvenNumbersMap x = countEvenNumbersFold x
let foldFilter = countEvenNumbersFold x = countEvenNumbersFilter x
match mapFilter, mapFold, foldFilter with
| true, true, true -> true
| _ -> false

Check.QuickThrowOnFailure check

[<Test>]
let funcToTreeWhenRightSubtreeIsNotEmpty () =
let tree = Tree.Tree(2, Tree.Tip(4), Tree.Tree(2, Tree.Tree(4, Tree.Tip(4), Tree.Tip(4)), Tree.Tip(4)))

let newTree = funcToTree tree (fun x -> x * 2)

newTree |> should equal (Tree.Tree(4, Tree.Tip(8), Tree.Tree(4, Tree.Tree(8, Tree.Tip(8), Tree.Tip(8)), Tree.Tip(8))))

[<Test>]
let funcToTreeTest () =
let tree = Tree.Tree(2, Tree.Tip(4), Tree.Tip(6))

let newTree = funcToTree tree (fun x -> x * 2)

newTree |> should equal (Tree(4, Tree.Tip(8), Tree.Tip(12)))

[<Test>]
let countTreeTestSum () =
let tree1 = TreeArithmetic.Node(Summation, Leaf 2, Node(Summation, Leaf 1, Leaf -1))

let result = count tree1

result |> should equal 2

[<Test>]
let countTreeTestSub () =
let tree1 = TreeArithmetic.Node(Subtracting, Leaf 15, Node(Subtracting, Leaf 10, Leaf -5))

let result = count tree1

result |> should equal 0

[<Test>]
let countTreeTestMult () =
let tree1 = TreeArithmetic.Node(Multiplication, Leaf -2, Node(Multiplication, Leaf 2, Leaf 4))

let result = count tree1

result |> should equal -16

[<Test>]
let countTreeTestDiv () =
let tree1 = TreeArithmetic.Node(Division, Leaf -10, Node(Division, Leaf 10, Leaf 5))

let result = count tree1

result |> should equal -5

[<Test>]
let countTreeTestWithAll () =
let tree1 = TreeArithmetic.Node(Division,
Node(Summation, Node(Subtracting, Leaf 35, Leaf -15), Leaf 50),
Node(Multiplication, Leaf 10, Leaf 5))

let result = count tree1

result |> should equal 2


[<Test>]
let primeNumbersTest () =
let first7PrimeNums = [ for i in 0 .. 7 -> Seq.item i (primeNumbers()) ]

first7PrimeNums |> should equal [ 2; 3; 5; 7; 11; 13; 17; 19]

27 changes: 27 additions & 0 deletions hw2/hw2.Test/hw2.Test.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>

<IsPackable>false</IsPackable>
<GenerateProgramFile>false</GenerateProgramFile>
</PropertyGroup>

<ItemGroup>
<Compile Include="FunctionsTest.fs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="FsCheck" Version="3.0.0-beta2" />
<PackageReference Include="FsUnit" Version="4.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="NUnit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\hw2\hw2.fsproj" />
</ItemGroup>

</Project>
22 changes: 22 additions & 0 deletions hw2/hw2.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "hw2", "hw2\hw2.fsproj", "{E29CE84F-A450-4B11-B9D1-195F5D0C46B6}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "hw2.Test", "hw2.Test\hw2.Test.fsproj", "{A878DF09-0A2D-4982-AECE-235EFEFEB545}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E29CE84F-A450-4B11-B9D1-195F5D0C46B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E29CE84F-A450-4B11-B9D1-195F5D0C46B6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E29CE84F-A450-4B11-B9D1-195F5D0C46B6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E29CE84F-A450-4B11-B9D1-195F5D0C46B6}.Release|Any CPU.Build.0 = Release|Any CPU
{A878DF09-0A2D-4982-AECE-235EFEFEB545}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A878DF09-0A2D-4982-AECE-235EFEFEB545}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A878DF09-0A2D-4982-AECE-235EFEFEB545}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A878DF09-0A2D-4982-AECE-235EFEFEB545}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
66 changes: 66 additions & 0 deletions hw2/hw2/Program.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
open System

// подсчитывает количество чётных чисел в списке c filter
let countEvenNumbersFilter list =
list |> List.filter (fun x -> x % 2 = 0) |> List.length

// подсчитывает количество чётных чисел в списке c map
let countEvenNumbersMap list =
list |> List.map (fun x -> (abs x + 1) % 2) |> List.sum

// подсчитывает количество чётных чисел в списке c fold
let countEvenNumbersFold list =
(0, list) ||> List.fold (fun acc x -> acc + (abs x + 1) % 2)

// бинарное дерево
type Tree<'a> =
| Tree of 'a * Tree<'a> * Tree<'a>
| Tip of 'a

// функция, которая применяет функцию к каждому элементу дерева
let rec funcToTree tree func =
match tree with
| Tip (value) -> Tip(func value)
| Tree (value, left, right) -> Tree(func value, funcToTree left func, funcToTree right func)

// все операции
type Operation =
| Summation
| Subtracting
| Multiplication
| Division

// арифметическое дерево
type TreeArithmetic<'t> =
| Leaf of 't
| Node of Operation * TreeArithmetic<'t> * TreeArithmetic<'t>

// функция, реализующая подсчет арифмитического дерева
let rec count tree =
match tree with
| Leaf x -> x
| Node(operation, left, right) ->
let left = count left
let right = count right
match operation with
| Summation -> left + right
| Subtracting -> left - right
| Multiplication -> left * right
| Division -> left / right

// выводит бесконечную последовательность простых чисел
let primeNumbers () =
let rec testOnPrime element list =
match list with
| [] -> true
| h :: tail ->
if element % h = 0 then false else testOnPrime element tail
let rec sequence element list =
seq {
if testOnPrime element list then
yield element
yield! sequence (element + 1) (element :: list)
else
yield! sequence (element + 1) list
}
sequence 2 []
13 changes: 13 additions & 0 deletions hw2/hw2/hw2.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<DockerDefaultTargetOS>Windows</DockerDefaultTargetOS>
</PropertyGroup>

<ItemGroup>
<Compile Include="Program.fs" />
</ItemGroup>

</Project>