1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| module Environment =
struct
type 'a env =
| Leaf
| Node of 'a env * 'a * 'a env
exception
Empty
let empty =
Leaf
let rec push x = function
| Leaf -> Node(Leaf,x,Leaf)
| Node(l,y,r) -> Node(push y r,x,l)
let top = function
| Leaf -> raise Empty
| Node(_,x,_) -> x
let rec top_pop = function
| Leaf -> raise Empty
| Node(Leaf,x,_) -> x,Leaf
| Node(l,x,r) ->
let t,p = top_pop l
in x,Node(r,t,p)
let rec nth n = function
| Leaf -> raise Empty
| Node(l,x,r) ->
if n = 0 then x
else if n mod 2 = 0 then nth (n/2) l
else nth (n/2 - 1) r
end |
Partager