Skip to content

Instantly share code, notes, and snippets.

@einblicker
einblicker / gist:1156445
Created August 19, 2011 09:31
CoPL problem 51
|- let rec fib = fun n -> if n < 3 then 1 else fib (n - 1) + fib (n - 2) in
fib 5
evalto 5 by E-LetRec {
fib = ()[rec fib = fun n -> if n < 3 then 1 else fib (n - 1) + fib (n - 2)] |-
fib 5
evalto 5 by E-AppRec {
fib = ()[rec fib = fun n -> if n < 3 then 1 else fib (n - 1) + fib (n - 2)] |-
fib
evalto ()[rec fib = fun n -> if n < 3 then 1 else fib (n - 1) + fib (n - 2)]
@einblicker
einblicker / gist:1156613
Created August 19, 2011 11:29
CoPL problem 52
|- let rec sum = fun f -> fun n ->
if n < 1 then 0 else f n + sum f (n - 1) in
sum (fun x -> x * x) 2
evalto 5
by E-LetRec {
sum = ()[rec sum = fun f -> fun n ->
if n < 1 then 0 else f n + sum f (n - 1)] |-
((sum (fun x -> x * x)) 2)
evalto 5
by E-App {
@einblicker
einblicker / gist:1156724
Created August 19, 2011 12:49
CoPL problem 53
|- let fact = fun self -> fun n ->
if n < 2 then 1 else n * self self (n - 1) in
fact fact 3
evalto 6
by E-Let {
|- fun self -> fun n ->
if n < 2 then 1 else n * self self (n - 1)
evalto ()[fun self -> fun n ->
if n < 2 then 1 else n * self self (n - 1)]
by E-Fun {};
@einblicker
einblicker / gist:1157236
Created August 19, 2011 16:18
CoPL problem 5X
|- let rec . = fun . ->
if #1 < 2 then 1 else #1 * #2 (#1 - 1) in #1 3
evalto 6 by E-LetRec {
()[rec . = fun . ->
if #1 < 2 then 1 else #1 * #2 (#1 - 1)] |- #1 3 evalto 6 by E-AppRec {
()[rec . = fun . ->
if #1 < 2 then 1 else #1 * #2 (#1 - 1)] |- #1
evalto ()[rec . = fun . ->
if #1 < 2 then 1 else #1 * #2 (#1 - 1)] by E-Var {};
()[rec . = fun . ->
@einblicker
einblicker / gist:1157616
Created August 19, 2011 18:33
CoPL problem 71
|- let f = fun x -> match x with [] -> 0 | a :: b -> a in
f (4::[]) + f [] + f (1 :: 2 :: 3 :: [])
evalto 5 by E-Let {
|- fun x -> match x with [] -> 0 | a :: b -> a
evalto ()[fun x -> match x with [] -> 0 | a :: b -> a]
by E-Fun {};
f = ()[fun x -> match x with [] -> 0 | a :: b -> a] |-
((f (4::[])) + (f [])) + (f (1 :: 2 :: 3 :: []))
evalto 5 by E-Plus {
@einblicker
einblicker / gist:1158390
Created August 20, 2011 00:26
CoPL problem 72
|- let rec f = fun x -> if x < 1 then [] else x :: f (x - 1) in
f 3 evalto 3 :: 2 :: 1 :: []
by E-LetRec {
f = ()[rec f = fun x -> if x < 1 then [] else x :: f (x - 1)] |-
f 3 evalto 3 :: 2 :: 1 :: []
by E-AppRec {
f = ()[rec f = fun x -> if x < 1 then [] else x :: f (x - 1)] |-
f evalto ()[rec f = fun x -> if x < 1 then [] else x :: f (x - 1)]
by E-Var {};
@einblicker
einblicker / gist:1158417
Created August 20, 2011 00:46
CoPL problem 73
|- let rec length = fun l -> match l with [] -> 0 | x :: y -> 1 + length y in
length (1 :: 2 :: 3 :: []) evalto 3
by E-LetRec {
length = ()[rec length = fun l -> match l with [] -> 0 | x :: y -> 1 + length y] |-
length (1 :: 2 :: 3 :: []) evalto 3
by E-AppRec {
length = ()[rec length = fun l -> match l with [] -> 0 | x :: y -> 1 + length y] |-
length evalto ()[rec length = fun l -> match l with [] -> 0 | x :: y -> 1 + length y]
by E-Var {};
@einblicker
einblicker / gist:1158451
Created August 20, 2011 01:24
CoPL problem 74
|- let rec length = fun l -> match l with [] -> 0 | x :: y -> 1 + length y in
length ((1 :: 2 :: []) :: (3 :: 4 :: 5 :: []) :: []) evalto 2
by E-LetRec {
length = ()[rec length = fun l -> match l with [] -> 0 | x :: y -> 1 + length y] |-
length ((1 :: 2 :: []) :: (3 :: 4 :: 5 :: []) :: []) evalto 2
by E-AppRec {
length = ()[rec length = fun l -> match l with [] -> 0 | x :: y -> 1 + length y] |-
length evalto ()[rec length = fun l -> match l with [] -> 0 | x :: y -> 1 + length y]
by E-Var {};
@einblicker
einblicker / gist:1158965
Created August 20, 2011 11:04
CoPL problem 76
|- let rec apply = fun l -> fun x ->
match l with [] -> x | f :: l -> f (apply l x) in
apply ((fun x -> x * x) :: (fun y -> y + 3) :: []) 4
evalto 49
by E-LetRec {
apply = ()[rec apply = fun l -> fun x ->
match l with [] -> x | f :: l -> f (apply l x)] |-
(apply ((fun x -> x * x) :: (fun y -> y + 3) :: [])) 4
evalto 49
by E-App {
@einblicker
einblicker / gist:1159025
Created August 20, 2011 12:18
CoPL problem 77
|- let rec apply = fun l -> fun x ->
match l with [] -> x | f :: l -> apply l (f x) in
apply ((fun x -> x * x) :: (fun y -> y + 3) :: []) 4
evalto 19
by E-LetRec {
apply = ()[rec apply = fun l -> fun x ->
match l with [] -> x | f :: l -> apply l (f x)] |-
apply ((fun x -> x * x) :: (fun y -> y + 3) :: []) 4
evalto 19
by E-App {