Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
35
Task/Y-combinator/F-Sharp/y-combinator-1.fs
Normal file
35
Task/Y-combinator/F-Sharp/y-combinator-1.fs
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
type 'a mu = Roll of ('a mu -> 'a) // ' fixes ease syntax colouring confusion with
|
||||
|
||||
let unroll (Roll x) = x
|
||||
// val unroll : 'a mu -> ('a mu -> 'a)
|
||||
|
||||
// As with most of the strict (non-deferred or non-lazy) languages,
|
||||
// this is the Z-combinator with the additional 'a' parameter...
|
||||
let fix f = let g = fun x a -> f (unroll x x) a in g (Roll g)
|
||||
// val fix : (('a -> 'b) -> 'a -> 'b) -> 'a -> 'b = <fun>
|
||||
|
||||
// Although true to the factorial definition, the
|
||||
// recursive call is not in tail call position, so can't be optimized
|
||||
// and will overflow the call stack for the recursive calls for large ranges...
|
||||
//let fac = fix (fun f n -> if n < 2 then 1I else bigint n * f (n - 1))
|
||||
// val fac : (int -> BigInteger) = <fun>
|
||||
|
||||
// much better progressive calculation in tail call position...
|
||||
let fac = fix (fun f n i -> if i < 2 then n else f (bigint i * n) (i - 1)) <| 1I
|
||||
// val fac : (int -> BigInteger) = <fun>
|
||||
|
||||
// Although true to the definition of Fibonacci numbers,
|
||||
// this can't be tail call optimized and recursively repeats calculations
|
||||
// for a horrendously inefficient exponential performance fib function...
|
||||
// let fib = fix (fun fnc n -> if n < 2 then n else fnc (n - 1) + fnc (n - 2))
|
||||
// val fib : (int -> BigInteger) = <fun>
|
||||
|
||||
// much better progressive calculation in tail call position...
|
||||
let fib = fix (fun fnc f s i -> if i < 2 then f else fnc s (f + s) (i - 1)) 1I 1I
|
||||
// val fib : (int -> BigInteger) = <fun>
|
||||
|
||||
[<EntryPoint>]
|
||||
let main argv =
|
||||
fac 10 |> printfn "%A" // prints 3628800
|
||||
fib 10 |> printfn "%A" // prints 55
|
||||
0 // return an integer exit code
|
||||
40
Task/Y-combinator/F-Sharp/y-combinator-2.fs
Normal file
40
Task/Y-combinator/F-Sharp/y-combinator-2.fs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
// same as previous...
|
||||
type 'a mu = Roll of ('a mu -> 'a) // ' fixes ease syntax colouring confusion with
|
||||
|
||||
// same as previous...
|
||||
let unroll (Roll x) = x
|
||||
// val unroll : 'a mu -> ('a mu -> 'a)
|
||||
|
||||
// break race condition with some deferred execution - laziness...
|
||||
let fix f = let g = fun x -> f <| fun() -> (unroll x x) in g (Roll g)
|
||||
// val fix : ((unit -> 'a) -> 'a -> 'a) = <fun>
|
||||
|
||||
// same efficient version of factorial functionb with added deferred execution...
|
||||
let fac = fix (fun f n i -> if i < 2 then n else f () (bigint i * n) (i - 1)) <| 1I
|
||||
// val fac : (int -> BigInteger) = <fun>
|
||||
|
||||
// same efficient version of Fibonacci function with added deferred execution...
|
||||
let fib = fix (fun fnc f s i -> if i < 2 then f else fnc () s (f + s) (i - 1)) 1I 1I
|
||||
// val fib : (int -> BigInteger) = <fun>
|
||||
|
||||
// given the following definition for an infinite Co-Inductive Stream (CIS)...
|
||||
type CIS<'a> = CIS of 'a * (unit -> CIS<'a>) // ' fix formatting
|
||||
|
||||
// Using a double Y-Combinator recursion...
|
||||
// defines a continuous stream of Fibonacci numbers; there are other simpler ways,
|
||||
// this way implements recursion by using the Y-combinator, although it is
|
||||
// much slower than other ways due to the many additional function calls,
|
||||
// it demonstrates something that can't be done with the Z-combinator...
|
||||
let fibs() =
|
||||
let fbsgen = fix (fun fnc (CIS((f, s), rest)) ->
|
||||
CIS((s, f + s), fun() -> fnc () <| rest()))
|
||||
Seq.unfold (fun (CIS((v, _), rest)) -> Some(v, rest()))
|
||||
<| fix (fun cis -> fbsgen (CIS((1I, 0I), cis))) // cis is a lazy thunk!
|
||||
|
||||
[<EntryPoint>]
|
||||
let main argv =
|
||||
fac 10 |> printfn "%A" // prints 3628800
|
||||
fib 10 |> printfn "%A" // prints 55
|
||||
fibs() |> Seq.take 20 |> Seq.iter (printf "%A ")
|
||||
printfn ""
|
||||
0 // return an integer exit code
|
||||
4
Task/Y-combinator/F-Sharp/y-combinator-3.fs
Normal file
4
Task/Y-combinator/F-Sharp/y-combinator-3.fs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
let rec fix f = f <| fun() -> fix f
|
||||
// val fix : f:((unit -> 'a) -> 'a) -> 'a
|
||||
|
||||
// the application of this true Y-combinator is the same as for the above non function recursive version.
|
||||
Loading…
Add table
Add a link
Reference in a new issue