diff --git a/F#/Homework1/Homework1.fs b/F#/Homework1/Homework1.fs new file mode 100644 index 0000000..fc8ae3b --- /dev/null +++ b/F#/Homework1/Homework1.fs @@ -0,0 +1,69 @@ +module Homework1 + +/// +/// Calculates factorial of the given number +/// +/// Number to calculate factorial from +let rec factorial num = + let rec factorialHelper acc number = + match number with + | 0u -> acc + | _ -> factorialHelper (acc * number) (number - 1u) + factorialHelper 1u num + +/// +/// Calculates fibonacci number +/// +/// Index of fibonacci number(starting with 0) +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 + +/// +/// Reverses the list +/// +/// List to reverse +/// Reversed list +let rec reverseList list = + let rec reverseListHelper list tail = + match list with + | [] -> tail + | head :: t -> reverseListHelper t (head :: tail) + reverseListHelper list [] + +/// +/// Generates list of [2^n; 2^(n + 1); ...; 2^(n + m)] +/// +/// First power +/// Number of elements +/// None if given negative arguments, list otherwise +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)) + +/// +/// Search for element in the list +/// +/// List to search +/// Element to search +/// None if element not found and position otherwise +let rec findElement mainList elem = + 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 \ No newline at end of file diff --git a/F#/Homework1/Homework1.fsproj b/F#/Homework1/Homework1.fsproj new file mode 100644 index 0000000..386f953 --- /dev/null +++ b/F#/Homework1/Homework1.fsproj @@ -0,0 +1,13 @@ + + + + net8.0 + true + Factorial + + + + + + + diff --git a/F#/forSpbu.sln b/F#/forSpbu.sln index 9a517dd..da4e1ca 100644 --- a/F#/forSpbu.sln +++ b/F#/forSpbu.sln @@ -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 @@ -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