-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0117f18
commit 38e04c6
Showing
5 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
let rec merge (l1,l2) = | ||
match (l1,l2) with | ||
| ([], _) -> l2 | ||
| (_, []) -> l1 | ||
| (h1::t1, h2::t2) -> h1::h2::merge(t1,t2) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
sigma(a,b,f) = | ||
if a>b-1 then 0 | ||
else f(b) + sigma(a,b-1,f) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
t = ZERO | SUCC of nat | ||
|
||
let rec natadd (a,b) = | ||
match(a,b) with | ||
| (ZERO,ZERO) -> ZERO | ||
| (ZERO, b) -> b | ||
| (a, ZERO) -> a | ||
| (SUCC a', SUCC b') -> SUCC(natadd(a,b')) | ||
|
||
let rec natmul(a,b)= | ||
match(a,b) with | ||
| (ZERO,ZERO)->ZERO | ||
| (ZERO,b)->ZERO | ||
| (a,ZERO)->ZERO | ||
| (SUCC a', SUCC b') -> natadd(natmul(a,b'),a) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
world*) | ||
|
||
type crazy2 = NIL | ZERO of crazy2 | ONE of crazy2 | MONE of crazy2 | ||
|
||
let is_even n = (n mod 2 = 0) | ||
|
||
let pow base exponent = | ||
if exponent <0 then invalid_arg "exponent cannot be negative" else | ||
let rec aux accumulator base = function | ||
| 0 -> accumulator | ||
| 1 -> base * accumulator | ||
| e when is_even e -> aux accumulator (base * base) (e/2) | ||
| e-> aux(base * accumulator)(base*base)((e-1)/2) in | ||
aux 1 base exponent | ||
(* | ||
let _ = print_endline(string_of_int(pow 2 5)) | ||
*) | ||
let rec find_depth l = | ||
match l with | ||
| NIL -> 0 | ||
| MONE(l') -> 1 + find_depth l' | ||
| ONE(l') -> 1+ find_depth l' | ||
| ZERO(l') -> 1+find_depth l' | ||
(* | ||
let _ = print_endline(string_of_int(find_depth(ZERO(ONE(MONE NIL))))) | ||
*) | ||
|
||
let rec crazy2val x = | ||
let sam = find_depth x in | ||
match x with | ||
| NIL -> 0 | ||
| MONE(x') -> (pow 2 (sam-1))*(-1)+crazy2val(x') | ||
| ONE(x') -> (pow 2 (sam-1))*1 + crazy2val(x') | ||
| ZERO(x') -> crazy2val(x') |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
|
||
|
||
type crazy2 = NIL | ZERO of crazy2 | ONE of crazy2 | MONE of crazy2 | ||
|
||
let is_even n = (n mod 2 = 0) | ||
|
||
let pow base exponent = | ||
if exponent <0 then invalid_arg "exponent cannot be negative" else | ||
let rec aux accumulator base = function | ||
| 0 -> accumulator | ||
| 1 -> base * accumulator | ||
| e when is_even e -> aux accumulator (base * base) (e/2) | ||
| e-> aux(base * accumulator)(base*base)((e-1)/2) in | ||
aux 1 base exponent | ||
(* | ||
let _ = print_endline(string_of_int(pow 2 5)) | ||
*) | ||
let rec find_depth l = | ||
match l with | ||
| NIL -> 0 | ||
| MONE(l') -> 1 + find_depth l' | ||
| ONE(l') -> 1+ find_depth l' | ||
| ZERO(l') -> 1+find_depth l' | ||
(* | ||
let _ = print_endline(string_of_int(find_depth(ZERO(ONE(MONE NIL))))) | ||
*) | ||
|
||
let rec crazy2val x = | ||
let sam = find_depth x in | ||
match x with | ||
| NIL -> 0 | ||
| MONE(x') -> (pow 2 (sam-1))*(-1)+crazy2val(x') | ||
| ONE(x') -> (pow 2 (sam-1))*1 + crazy2val(x') | ||
| ZERO(x') -> crazy2val(x') | ||
(* | ||
let _ = print_endline(string_of_int(crazy2val(MONE(ONE(MONE(ZERO(NIL)))))))*) | ||
|
||
let rec decode (x, l) = | ||
if (x = 0) then l else | ||
if (x mod 2 = 0) then decode ((x/2), ZERO(l)) | ||
else decode ((x/2), ONE(l)) | ||
|
||
|
||
let crazy2add (x,y) = | ||
let sam = crazy2val x in | ||
let sam2 = crazy2val y in | ||
let sam3 = sam+sam2 in | ||
decode (sam3, NIL) |