-
Notifications
You must be signed in to change notification settings - Fork 17
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
27ff11e
commit 799b3ee
Showing
3 changed files
with
117 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,58 @@ | ||
import OrderedMap | ||
|
||
let assert condition = | ||
if condition then () else exit 1 | ||
|
||
let rec eq_list lista_1 lista_2 compare = | ||
match (lista_1,lista_2) with | ||
| ([],[]) => True | ||
| (x::xs, y::ys) => if compare x y then eq_list xs ys compare else False | ||
| _ => False | ||
end | ||
|
||
let rec foldl func lista acc = | ||
match lista with | ||
| [] => acc | ||
| x :: xs => func x (foldl func xs acc) | ||
end | ||
|
||
let only_val lista = foldl (fn x acc => (snd x) :: acc) lista [] | ||
|
||
let lt (v1 : Int) (v2 : Int) = v1 < v2 | ||
|
||
let OrderedMap.Map {module IntMap} = OrderedMap.makeMap lt | ||
|
||
let x = IntMap.empty | ||
let _ = assert (x.isEmpty) | ||
|
||
|
||
let x = x.add 1 1 | ||
let _ = assert (x.isEmpty == False) | ||
|
||
let x = x.add 2 1 >. add 3 2 >. add 4 3 | ||
let _ = assert (eq_list (only_val x.toList) [1,1,2,3] (fn (x : Int) (y : Int) => x == y)) | ||
|
||
let x = x.update 2 5 | ||
let _ = assert (eq_list (only_val x.toList) [1,5,2,3] (fn (x : Int) (y : Int) => x == y)) | ||
|
||
let x = x.deleteElem 1 | ||
let _ = assert (eq_list (only_val x.toList) [5,2,3] (fn (x : Int) (y : Int) => x == y)) | ||
|
||
let y = IntMap.empty >. add 5 2 >. add 6 1 >. add 1 1 >. add 0 8 | ||
let x = x.join y | ||
let _ = assert (eq_list (only_val x.toList) [8,1,5,2,3,2,1] (fn (x : Int) (y : Int) => x == y)) | ||
|
||
let x = x.deleteMin | ||
let _ = assert (eq_list (only_val x.toList) [1,5,2,3,2,1] (fn (x : Int) (y : Int) => x == y)) | ||
|
||
let x = x.deleteMax | ||
let _ = assert (eq_list (only_val x.toList) [1,5,2,3,2] (fn (x : Int) (y : Int) => x == y)) | ||
|
||
let get_value val = | ||
match val with | ||
| Some (x,y) => y | ||
| _ => -1 | ||
end | ||
|
||
let y = x.find 3 | ||
let _ = assert ((fn (x : Int) (y : Int) => x == y) (get_value y) 2) |
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,36 @@ | ||
import OrderedMap | ||
|
||
let assert condition = | ||
if condition then () else exit 1 | ||
|
||
let rec eq_list lista_1 lista_2 compare = | ||
match (lista_1,lista_2) with | ||
| ([],[]) => True | ||
| (x::xs, y::ys) => if compare x y then eq_list xs ys compare else False | ||
| _ => False | ||
end | ||
|
||
let rec foldl func lista acc = | ||
match lista with | ||
| [] => acc | ||
| x :: xs => func x (foldl func xs acc) | ||
end | ||
|
||
let lt (v1 : Int) (v2 : Int) = v1 < v2 | ||
|
||
let OrderedMap.Set {module IntSet} = OrderedMap.makeSet lt | ||
|
||
let x = IntSet.empty | ||
let _ = assert x.isEmpty | ||
|
||
let x = x.insert 1 | ||
let _ = assert (x.isEmpty == False) | ||
|
||
let x = x.insert 2 >. insert 3 >. insert 2 | ||
let _ = assert (x.find 2 && x.find 1 && x.find 3 && x.find 4 == False) | ||
|
||
let x = 4.singleton | ||
let _ = assert ((x.find 2 && x.find 1 && x.find 3 && x.find 4 == False) == False) | ||
|
||
let x = x.deleteMax | ||
let _ = assert (x.isEmpty) |
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,23 @@ | ||
import Queue | ||
|
||
|
||
let assert condition = | ||
if condition then () else exit 1 | ||
|
||
let compare (x : Int) (y : Int) = x == y | ||
let get_val x = | ||
match x with | ||
| Some x => x | ||
| _ => -1 | ||
end | ||
|
||
let Queue.Queue {module IntQ} = Queue.makeQueue | ||
|
||
let x = IntQ.empty | ||
let _ = assert x.isEmpty | ||
let x = x.push 1 | ||
let _ = assert (x.isEmpty == False && compare (get_val x.head) 1) | ||
let x = x.pop | ||
let _ = assert x.isEmpty | ||
let x = x >. push 1 >. push 2 >. push 3 | ||
let _ = assert (x.isEmpty == False && compare (get_val x.head) 1 && compare (get_val (x.pop >. head)) 2 && compare (get_val (x.pop >. pop >. head)) 3) |