diff --git a/lab 5/ConditionalTraversal/ConditionalTraversal.fsproj b/lab 5/ConditionalTraversal/ConditionalTraversal.fsproj new file mode 100755 index 0000000..299cf40 --- /dev/null +++ b/lab 5/ConditionalTraversal/ConditionalTraversal.fsproj @@ -0,0 +1,12 @@ + + + + Exe + net7.0 + + + + + + + diff --git a/lab 5/ConditionalTraversal/Program.fs b/lab 5/ConditionalTraversal/Program.fs new file mode 100755 index 0000000..e738ced --- /dev/null +++ b/lab 5/ConditionalTraversal/Program.fs @@ -0,0 +1,46 @@ +// For more information see https://aka.ms/fsharp-console-apps +printfn "Hello from F#" + +// f - operator +// init - initial value +// predicate - filter condition +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 + + // Сумма четных цифр + let isEven x = x % 2 = 0 + let sumEven = traverseNumberWithCondition number (fun x y -> x + y) 0 isEven + System.Console.WriteLine($"Сумма четных цифр: {sumEven}") + + // Произведение нечетных цифр + let isOdd x = x % 2 <> 0 + let mulOdd = traverseNumberWithCondition number (fun x y -> x * y) 1 isOdd + System.Console.WriteLine($"Произведение нечетных цифр: {mulOdd}") + + // Максимальная цифра, большая 5 + let isGreaterThan5 x = x > 5 + let maxGreater5 = traverseNumberWithCondition number (fun x y -> max x y) 0 isGreaterThan5 + System.Console.WriteLine($"Максимальная цифра > 5: {maxGreater5}") + + // Минимальная цифра из цифр, делящихся на 3 + let isDivisibleBy3 x = x > 0 && x % 3 = 0 + let minDivBy3 = traverseNumberWithCondition number (fun x y -> min x y) System.Int32.MaxValue isDivisibleBy3 + System.Console.WriteLine($"Минимальная цифра, делящаяся на 3: {minDivBy3}") + + 0