Last active
October 8, 2018 18:09
-
-
Save biboudis/b0fd567129c2e7dbf26f to your computer and use it in GitHub Desktop.
Staged power, with fixed, with staged fix.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* http://fssnip.net/sE in BER MetaOCaml *) | |
open Runcode;; | |
let rec fix : (('a -> 'b) -> ('a -> 'b)) -> 'a -> 'b = fun f x -> | |
f (fix f) x;; | |
let fix' : (('a -> 'b) code -> ('a -> 'b) code) -> ('a -> 'b) code = fun f -> | |
.< fun x -> let rec loop x = (fun f' -> .~(f .<f'>.) ) loop x in | |
loop x >.;; | |
let even n = (n mod 2) = 0;; | |
let square x = x * x;; | |
let rec power n x = | |
if n = 0 then 1 | |
else x * (power (n-1) x);; | |
let even n = (n mod 2) = 0;; | |
let square x = x * x;; | |
let power x f = fun n -> | |
if n = 0 then .<1>. | |
else .<.~x * .~(f (n-1))>. ;; | |
let powerS = (fix (power .<5>.)) 10;; | |
let ans = !. powerS;; | |
let power' : int code -> (int -> int) code -> (int -> int) code = fun x f -> | |
.< fun n -> | |
if n = 0 then 1 | |
else .~x * .~f (n-1) >.;; | |
let powerS' = fix' (power' .<5>.) ;; | |
let ans = !. powerS' 10;; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment