diff --git a/lab6/Makefile b/lab6/Makefile index a624627..c72547a 100644 --- a/lab6/Makefile +++ b/lab6/Makefile @@ -1,4 +1,4 @@ -.PHONY: run build clean task1 build-task1 clean-task1 task2 build-task2 clean-task2 task3 build-task3 clean-task3 +.PHONY: run build clean task1 build-task1 clean-task1 task2 build-task2 clean-task2 task3 build-task3 clean-task3 task4 build-task4 clean-task4 # task1 run: @@ -41,3 +41,13 @@ build-task3: clean-task3: cd task3 && dotnet clean + +# task4 +task4: + cd task4 && dotnet run --project task4.fsproj + +build-task4: + cd task4 && dotnet build + +clean-task4: + cd task4 && dotnet clean diff --git a/lab6/task4/Program.fs b/lab6/task4/Program.fs new file mode 100644 index 0000000..5845fe8 --- /dev/null +++ b/lab6/task4/Program.fs @@ -0,0 +1,64 @@ +type ChurchList<'a> = ('a -> 'b -> 'b) -> 'b -> 'b + +let nil : ChurchList<'a> = fun _ z -> z + +let cons (x: 'a) (xs: ChurchList<'a>) : ChurchList<'a> = + fun f z -> f x (xs f z) + +let toList (churchList: ChurchList<'a>) : 'a list = + churchList (fun x acc -> x :: acc) [] + |> List.rev + +let ofList (lst: 'a list) : ChurchList<'a> = + List.foldBack cons lst nil + +let foldWithPredicate (f: 'a -> 'b -> 'b) (p: 'a -> bool) (acc: 'b) (churchList: ChurchList<'a>) : 'b = + let rec foldTailRec lst currentAcc = + match lst with + | [] -> currentAcc + | x :: xs -> + if p x then + foldTailRec xs (f x currentAcc) + else + foldTailRec xs currentAcc + + let regularList = toList churchList + foldTailRec regularList acc + +let findMin (churchList: ChurchList) : int option = + let regularList = toList churchList + match regularList with + | [] -> None + | x :: xs -> + let minValue = foldWithPredicate (fun x acc -> min x acc) (fun _ -> true) x churchList + Some minValue + +let sumEven (churchList: ChurchList) : int = + foldWithPredicate (+) (fun x -> x % 2 = 0) 0 churchList + +let countOdd (churchList: ChurchList) : int = + foldWithPredicate (fun _ acc -> acc + 1) (fun x -> x % 2 <> 0) 0 churchList + +[] +let main argv = + let testList = ofList [1; 2; 3; 4; 5; 6; 7; 8; 9; 10] + let emptyList = nil + + printfn "Тестовый список: [1; 2; 3; 4; 5; 6; 7; 8; 9; 10]" + + match findMin testList with + | Some min -> printfn "Минимальный элемент: %d" min + | None -> printfn "Список пуст" + + printfn "Сумма четных чисел: %d" (sumEven testList) + printfn "Количество нечетных чисел: %d" (countOdd testList) + + printfn "\nТест с пустым списком:" + match findMin emptyList with + | Some min -> printfn "Минимальный элемент: %d" min + | None -> printfn "Список пуст" + + printfn "Сумма четных чисел в пустом списке: %d" (sumEven emptyList) + printfn "Количество нечетных чисел в пустом списке: %d" (countOdd emptyList) + + 0 \ No newline at end of file diff --git a/lab6/task4/task4.fsproj b/lab6/task4/task4.fsproj new file mode 100644 index 0000000..1cddbd7 --- /dev/null +++ b/lab6/task4/task4.fsproj @@ -0,0 +1,8 @@ + + + + Exe + net7.0 + + + \ No newline at end of file