tyrion

esercizi.ml

Feb 16th, 2013
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 0.74 KB | None | 0 0
  1. type col = Rosso | Giallo | Verde | Blu
  2. type 'a col_assoc = (col * 'a list) list
  3. type 'a tree = Empty | Tr of 'a * 'a tree * 'a tree
  4.  
  5. let colore x lst = fst (List.find (fun (a,b) -> List.mem x b) lst)
  6.  
  7. let next = function
  8.     | Rosso | Giallo | Verde -> Blu
  9.     | Blu -> Rosso
  10.  
  11. let path_to k colori =
  12.     let rec aux c sol = function
  13.         | Empty -> raise Fail
  14.         | Tr(x,_,_) when (colore x colori)=c -> raise Fail
  15.         | Tr(x,Empty,Empty) -> if x=k then (List.rev (x::sol)) else raise Fail
  16.         | Tr(x,lt,rt) -> let c,s = (colore x colori),(x::sol) in
  17.                          try aux c s lt with _ -> aux c s rt
  18.     in function
  19.         | Tr(x,_,_) as t -> aux (next (colore x colori)) [] t
  20.         | Empty -> raise Fail
Advertisement
Add Comment
Please, Sign In to add comment