From 18dc0554c0458e2cccb377739a51750b04396804 Mon Sep 17 00:00:00 2001 From: Artem-Darius Weber Date: Thu, 17 Apr 2025 13:32:09 +0300 Subject: [PATCH] (lab 5) feat: add practical Number Examples --- lab 5/NumberExamples/NumberExamples.fsproj | 12 +++++++ lab 5/NumberExamples/Program.fs | 40 ++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100755 lab 5/NumberExamples/NumberExamples.fsproj create mode 100755 lab 5/NumberExamples/Program.fs diff --git a/lab 5/NumberExamples/NumberExamples.fsproj b/lab 5/NumberExamples/NumberExamples.fsproj new file mode 100755 index 0000000..299cf40 --- /dev/null +++ b/lab 5/NumberExamples/NumberExamples.fsproj @@ -0,0 +1,12 @@ + + + + Exe + net7.0 + + + + + + + diff --git a/lab 5/NumberExamples/Program.fs b/lab 5/NumberExamples/Program.fs new file mode 100755 index 0000000..f7dc2fa --- /dev/null +++ b/lab 5/NumberExamples/Program.fs @@ -0,0 +1,40 @@ +let rec traverseNumberWithCondition' n f init predicate = + match n with + | 0 -> init + | n -> + let digit = n % 10 + let newAcc = + match predicate digit with + | true -> f init digit + | false -> init + traverseNumberWithCondition' (n / 10) f newAcc predicate + +let traverseNumberWithCondition n f init predicate = + traverseNumberWithCondition' n f init predicate + +[] +let main argv = + System.Console.WriteLine("Введите число:") + let number = System.Console.ReadLine() |> int + + // Пример 1: Количество цифр, больших 3 + let isGreaterThan3 x = x > 3 + let countGreaterThan3 = traverseNumberWithCondition number (fun acc _ -> acc + 1) 0 isGreaterThan3 + System.Console.WriteLine($"Количество цифр больше 3: {countGreaterThan3}") + + // Пример 2: Сумма цифр, кратных 3 + let isDivisibleBy3 x = x > 0 && x % 3 = 0 + let sumDivisibleBy3 = traverseNumberWithCondition number (fun acc x -> acc + x) 0 isDivisibleBy3 + System.Console.WriteLine($"Сумма цифр, кратных 3: {sumDivisibleBy3}") + + // Пример 3: Произведение цифр в диапазоне [3,7] + let isInRange3to7 x = x >= 3 && x <= 7 + let mulInRange = traverseNumberWithCondition number (fun acc x -> acc * x) 1 isInRange3to7 + System.Console.WriteLine($"Произведение цифр в диапазоне [3,7]: {mulInRange}") + + // Демонстрация композиции условий + let isEvenAndGreaterThan3 x = x % 2 = 0 && x > 3 + let sumEvenGreaterThan3 = traverseNumberWithCondition number (fun acc x -> acc + x) 0 isEvenAndGreaterThan3 + System.Console.WriteLine($"Сумма четных цифр, больших 3: {sumEvenGreaterThan3}") + + 0 \ No newline at end of file