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
29 changes: 29 additions & 0 deletions F#/TreeMap.Test/TreeMap.Test.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>

<IsPackable>false</IsPackable>
<GenerateProgramFile>true</GenerateProgramFile>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

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

</ItemGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="FsUnit" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
</ItemGroup>

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

</Project>
12 changes: 12 additions & 0 deletions F#/TreeMap.Test/TreeMapTest.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module TreeMap.Test

open NUnit.Framework
open FsUnit

[<Test>]
let LeafMapTest () =
Leaf 1 |> treeMap ((+) 1) |> should equal (Leaf 2)

[<Test>]
let TreeMapTest () =
Node(2, Leaf 1, Node(3, Leaf 4, Leaf 5)) |> treeMap ((+) 1) |> should equal (Node(3, Leaf 2, Node(4, Leaf 5, Leaf 6)))
10 changes: 10 additions & 0 deletions F#/TreeMap/TreeMap.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module TreeMap

type BinTree<'T> =
| Node of 'T * BinTree<'T> * BinTree<'T>
| Leaf of 'T

let rec treeMap func tree =
match tree with
| Node (value, lhs, rhs) -> Node (func value, treeMap func lhs, treeMap func rhs)
| Leaf value -> Leaf (func value)

Choose a reason for hiding this comment

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

Я бы сказал, что нужны комментарии. Ещё будь я Косарев, я бы сказал, что нужна хвостовая рекурсия, так что treeMap можно расписать в CPS с continuation-ом для левого поддерева, который вызывает continuation для правого. Но это одна из первых домашек, так что пусть будет зачтена.

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

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

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

</Project>
12 changes: 12 additions & 0 deletions F#/forSpbu.sln
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "07.03", "07.03", "{5CA9053B
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Homework1", "Homework1\Homework1.fsproj", "{04B15EE4-079A-42ED-ACC8-E2DCD25281C6}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "TreeMap", "TreeMap\TreeMap.fsproj", "{68775386-8212-46CE-BA41-10BC58B4DEA2}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "TreeMap.Test", "TreeMap.Test\TreeMap.Test.fsproj", "{91C86FB1-2E6A-4E53-9E04-AE05DAC8517C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -58,6 +62,14 @@ Global
{89A935E8-B5F3-435D-ACC3-A99DD7C66178}.Debug|Any CPU.Build.0 = Debug|Any CPU
{89A935E8-B5F3-435D-ACC3-A99DD7C66178}.Release|Any CPU.ActiveCfg = Release|Any CPU
{89A935E8-B5F3-435D-ACC3-A99DD7C66178}.Release|Any CPU.Build.0 = Release|Any CPU
{68775386-8212-46CE-BA41-10BC58B4DEA2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{68775386-8212-46CE-BA41-10BC58B4DEA2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{68775386-8212-46CE-BA41-10BC58B4DEA2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{68775386-8212-46CE-BA41-10BC58B4DEA2}.Release|Any CPU.Build.0 = Release|Any CPU
{91C86FB1-2E6A-4E53-9E04-AE05DAC8517C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{91C86FB1-2E6A-4E53-9E04-AE05DAC8517C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{91C86FB1-2E6A-4E53-9E04-AE05DAC8517C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{91C86FB1-2E6A-4E53-9E04-AE05DAC8517C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{7937CDA8-8285-4E23-AD1A-FC0F04FEEFE6} = {91E3BDA2-0836-46C2-95F0-02513FD7F13F}
Expand Down