Skip to content

Commit

Permalink
s
Browse files Browse the repository at this point in the history
  • Loading branch information
freewizard20 committed Sep 5, 2018
1 parent 0117f18 commit 38e04c6
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 0 deletions.
5 changes: 5 additions & 0 deletions hw11.ml
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)
3 changes: 3 additions & 0 deletions hw12.ml
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)
15 changes: 15 additions & 0 deletions hw14.ml
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)
35 changes: 35 additions & 0 deletions hw22.ml
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')
48 changes: 48 additions & 0 deletions hw23.ml
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)

0 comments on commit 38e04c6

Please sign in to comment.