From 119b17836046bf33f948522eb529647ca7d6b7e9 Mon Sep 17 00:00:00 2001 From: Artem-Darius Weber Date: Fri, 18 Apr 2025 06:11:57 +0300 Subject: [PATCH] lab 6 task 2 --- lab6/Makefile | 22 +++++++++++- lab6/task2/Program.fs | 39 ++++++++++++++++++++ lab6/task2/task2.fsproj | 12 +++++++ lab6/task3/Program.fs | 80 +++++++++++++++++++++++++++++++++++++++++ lab6/task3/task3.fsproj | 12 +++++++ 5 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 lab6/task2/Program.fs create mode 100644 lab6/task2/task2.fsproj create mode 100644 lab6/task3/Program.fs create mode 100644 lab6/task3/task3.fsproj diff --git a/lab6/Makefile b/lab6/Makefile index 6d2309e..a624627 100644 --- a/lab6/Makefile +++ b/lab6/Makefile @@ -1,4 +1,4 @@ -.PHONY: run build clean task1 build-task1 clean-task1 +.PHONY: run build clean task1 build-task1 clean-task1 task2 build-task2 clean-task2 task3 build-task3 clean-task3 # task1 run: @@ -21,3 +21,23 @@ build-task1: clean-task1: cd task1 && dotnet clean + +# task2 +task2: + cd task2 && dotnet run --project task2.fsproj + +build-task2: + cd task2 && dotnet build + +clean-task2: + cd task2 && dotnet clean + +# task3 +task3: + cd task3 && dotnet run --project task3.fsproj + +build-task3: + cd task3 && dotnet build + +clean-task3: + cd task3 && dotnet clean diff --git a/lab6/task2/Program.fs b/lab6/task2/Program.fs new file mode 100644 index 0000000..12d64fc --- /dev/null +++ b/lab6/task2/Program.fs @@ -0,0 +1,39 @@ +module ChurchPrint + +open System + +let nil (c:'a->'b->'b) (n:'b) : 'b = n + +let cons (x:'a) (xs:('a->'b->'b)->'b->'b) : ('a->'b->'b)->'b->'b = + fun c n -> c x (xs c n) + +let fold (xs:('a->'b->'b)->'b->'b) (c:'a->'b->'b) (n:'b) : 'b = + xs c n + +let toList (xs:('a->'b->'b)->'b->'b) : 'a list = + fold xs (fun x acc -> x :: acc) [] + +let printChurchTR (show:'a -> string) (xs:('a->'b->'b)->'b->'b) : unit = + let rec loop l = + match l with + | [] -> () + | h :: t -> + printf "%s" (show h) + if t <> [] then printf " " + loop t + xs |> toList |> loop + printfn "" + +[] +let main _ = + let churchList = cons 1 (cons 2 (cons 3 nil)) + + printf "Числа: " + printChurchTR string churchList + + let stringList = cons "Hello" (cons "World" (cons "F#" nil)) + + printf "Строки: " + printChurchTR id stringList + + 0 diff --git a/lab6/task2/task2.fsproj b/lab6/task2/task2.fsproj new file mode 100644 index 0000000..878876c --- /dev/null +++ b/lab6/task2/task2.fsproj @@ -0,0 +1,12 @@ + + + + Exe + net7.0 + + + + + + + \ No newline at end of file diff --git a/lab6/task3/Program.fs b/lab6/task3/Program.fs new file mode 100644 index 0000000..c37b5b1 --- /dev/null +++ b/lab6/task3/Program.fs @@ -0,0 +1,80 @@ +module ChurchFold + +open System + + +let nil (c:'a->'b->'b) (n:'b) : 'b = n + +let cons (x:'a) (xs:('a->'b->'b)->'b->'b) : ('a->'b->'b)->'b->'b = + fun c n -> c x (xs c n) + + +let toList (xs:('a->'b->'b)->'b->'b) : 'a list = + xs (fun x acc -> x :: acc) [] + + +let ofList (ls:'a list) : (('a->'b->'b)->'b->'b) = + List.foldBack (fun x acc -> cons x acc) ls nil + + +let foldWithPredicate (churchList:(int->int list->int list)->int list->int list) (f:int->int->int) (p:int->bool) (acc:int) : int = + + let regularList = toList churchList + + + let rec foldTailRec lst accumulator = + match lst with + | [] -> accumulator + | head :: tail -> + if p head then + + foldTailRec tail (f accumulator head) + else + + foldTailRec tail accumulator + + foldTailRec regularList acc + +[] +let main _ = + + let numbers = ofList [1; 2; 3; 4; 5] + + + let add x y = x + y + + + let isEven x = x % 2 = 0 + + + let greaterThan2 x = x > 2 + + printfn "Исходный список: [1; 2; 3; 4; 5]" + printfn "" + + + let sumEven = foldWithPredicate numbers add isEven 0 + printfn "Сумма четных чисел (предикат isEven): %d" sumEven + + + let sumGreater2 = foldWithPredicate numbers add greaterThan2 0 + printfn "Сумма чисел больше 2 (предикат > 2): %d" sumGreater2 + + + let multiply x y = x * y + let productEven = foldWithPredicate numbers multiply isEven 1 + printfn "Произведение четных чисел: %d" productEven + + printfn "" + printfn "Демонстрация работы с различными предикатами:" + + + let isOdd x = x % 2 = 1 + let sumOdd = foldWithPredicate numbers add isOdd 0 + printfn "Сумма нечетных чисел: %d" sumOdd + + let isPositive x = x > 0 + let countPositive = foldWithPredicate numbers (fun acc _ -> acc + 1) isPositive 0 + printfn "Количество положительных чисел: %d" countPositive + + 0 \ No newline at end of file diff --git a/lab6/task3/task3.fsproj b/lab6/task3/task3.fsproj new file mode 100644 index 0000000..878876c --- /dev/null +++ b/lab6/task3/task3.fsproj @@ -0,0 +1,12 @@ + + + + Exe + net7.0 + + + + + + + \ No newline at end of file