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
69 changes: 69 additions & 0 deletions F#/Homework1/Homework1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
module Homework1

/// <summary>
/// Calculates factorial of the given number
/// </summary>
/// <param name="num">Number to calculate factorial from</param>
let rec factorial num =
let rec factorialHelper acc number =
match number with
| 0u -> acc
| _ -> factorialHelper (acc * number) (number - 1u)
factorialHelper 1u num

/// <summary>
/// Calculates fibonacci number
/// </summary>
/// <param name="num">Index of fibonacci number(starting with 0)</param>
let rec fibonacci num =
let rec fibonacciHelper ind curr next =
match ind with
| 0u -> curr
| _ -> fibonacciHelper (ind - 1u) next (curr + next)
fibonacciHelper num 1 1

/// <summary>
/// Reverses the list
/// </summary>
/// <param name="list">List to reverse</param>
/// <returns>Reversed list</returns>
let rec reverseList list =
let rec reverseListHelper list tail =
match list with
| [] -> tail
| head :: t -> reverseListHelper t (head :: tail)
reverseListHelper list []

/// <summary>
/// Generates list of [2^n; 2^(n + 1); ...; 2^(n + m)]
/// </summary>
/// <param name="n">First power</param>
/// <param name="m">Number of elements</param>
/// <returns>None if given negative arguments, list otherwise</returns>
let rec genPowerList n m =
if n < 0 || m < 0 then None else
let rec genPowerListHelper num list curPower =
if curPower = n + m then
num :: list
else if curPower < n then
genPowerListHelper (num * 2) list (curPower + 1)
else
genPowerListHelper (num * 2) (num :: list) (curPower + 1)
Some(reverseList (genPowerListHelper 1 [] 0))

/// <summary>
/// Search for element in the list
/// </summary>
/// <param name="mainList">List to search</param>
/// <param name="elem">Element to search</param>
/// <returns>None if element not found and position otherwise</returns>
let rec findElement mainList elem =

Choose a reason for hiding this comment

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

rec тут излишне

let rec findElementHelper pos list =
match list with
| [] -> None
| head :: tail ->
if head = elem then
Some(pos)
else
findElementHelper (pos + 1) tail
findElementHelper 0 mainList
13 changes: 13 additions & 0 deletions F#/Homework1/Homework1.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

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

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

</Project>
11 changes: 10 additions & 1 deletion F#/forSpbu.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "07.03", "07.03\07.03.fsproj", "{0FD34DFA-792D-44F0-BD17-CE59DD81D040}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "07.03", "07.03", "{5CA9053B-DE9B-4824-9F19-AEB464E3B9D8}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Homework1", "Homework1\Homework1.fsproj", "{04B15EE4-079A-42ED-ACC8-E2DCD25281C6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -18,5 +20,12 @@ Global
{0FD34DFA-792D-44F0-BD17-CE59DD81D040}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0FD34DFA-792D-44F0-BD17-CE59DD81D040}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0FD34DFA-792D-44F0-BD17-CE59DD81D040}.Release|Any CPU.Build.0 = Release|Any CPU
{04B15EE4-079A-42ED-ACC8-E2DCD25281C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{04B15EE4-079A-42ED-ACC8-E2DCD25281C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{04B15EE4-079A-42ED-ACC8-E2DCD25281C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{04B15EE4-079A-42ED-ACC8-E2DCD25281C6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{04B15EE4-079A-42ED-ACC8-E2DCD25281C6} = {5CA9053B-DE9B-4824-9F19-AEB464E3B9D8}
EndGlobalSection
EndGlobal