(lab 5) feat: add Lambda Traversal techniques

main
Artem-Darius Weber 3 weeks ago
parent 071cec924c
commit 939761bddb

@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Program.fs" />
</ItemGroup>
</Project>

@ -0,0 +1,35 @@
let rec traverseNumber' n f acc =
match n with
| 0 -> acc
| n -> traverseNumber' (n / 10) f (f acc (n % 10))
let traverseNumber n f init = traverseNumber' n f init
[<EntryPoint>]
let main argv =
System.Console.WriteLine("Введите число:")
let number = System.Console.ReadLine() |> int
let sumResult = traverseNumber number (fun x y -> x + y) 0
System.Console.WriteLine($"Сумма цифр (λx y x + y): {sumResult}")
let mulResult = traverseNumber number (fun x y -> x * y) 1
System.Console.WriteLine($"Произведение цифр (λx y x * y): {mulResult}")
let minResult = traverseNumber number (fun x y -> min x y) System.Int32.MaxValue
System.Console.WriteLine($"Минимальная цифра (λx y min x y): {minResult}")
let maxResult = traverseNumber number (fun x y -> max x y) 0
System.Console.WriteLine($"Максимальная цифра (λx y max x y): {maxResult}")
// Демонстрация других форм лямбда-выражений
let sumResult2 = traverseNumber number (fun acc digit -> acc + digit) 0
System.Console.WriteLine($"Сумма цифр (λacc digit acc + digit): {sumResult2}")
let mulResult2 = traverseNumber number (fun acc digit ->
match digit with
| 0 -> acc
| d -> acc * d) 1
System.Console.WriteLine($"Произведение ненулевых цифр (λacc digit match digit...): {mulResult2}")
0
Loading…
Cancel
Save