lab 6 task 2

main
Artem-Darius Weber 6 months ago
parent d1ae3c3da0
commit 119b178360

@ -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

@ -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 ""
[<EntryPoint>]
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

@ -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,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
[<EntryPoint>]
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

@ -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>
Loading…
Cancel
Save