From 38e04c68a5e527cdd5bdd25aebea1100655a01d7 Mon Sep 17 00:00:00 2001 From: kate43 Date: Wed, 5 Sep 2018 11:48:29 +0900 Subject: [PATCH] s --- hw11.ml | 5 +++++ hw12.ml | 3 +++ hw14.ml | 15 +++++++++++++++ hw22.ml | 35 +++++++++++++++++++++++++++++++++++ hw23.ml | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 106 insertions(+) create mode 100644 hw11.ml create mode 100644 hw12.ml create mode 100644 hw14.ml create mode 100644 hw22.ml create mode 100644 hw23.ml diff --git a/hw11.ml b/hw11.ml new file mode 100644 index 0000000..68e6350 --- /dev/null +++ b/hw11.ml @@ -0,0 +1,5 @@ +let rec merge (l1,l2) = + match (l1,l2) with + | ([], _) -> l2 + | (_, []) -> l1 + | (h1::t1, h2::t2) -> h1::h2::merge(t1,t2) diff --git a/hw12.ml b/hw12.ml new file mode 100644 index 0000000..876e463 --- /dev/null +++ b/hw12.ml @@ -0,0 +1,3 @@ +sigma(a,b,f) = + if a>b-1 then 0 + else f(b) + sigma(a,b-1,f) diff --git a/hw14.ml b/hw14.ml new file mode 100644 index 0000000..c7283d3 --- /dev/null +++ b/hw14.ml @@ -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) diff --git a/hw22.ml b/hw22.ml new file mode 100644 index 0000000..eace70c --- /dev/null +++ b/hw22.ml @@ -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') diff --git a/hw23.ml b/hw23.ml new file mode 100644 index 0000000..2dab3bb --- /dev/null +++ b/hw23.ml @@ -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)