You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
911 B

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