Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. (* inizia *)
  2. type direzione = Su | Giu | Destra | Sinistra
  3. type posizione = int * int * direzione
  4. type azione = Gira | Avanti of int
  5.  
  6. (* gira : direzione -> direzione *)
  7. let gira = function
  8. Su -> Destra
  9. | Giu -> Sinistra
  10. | Destra -> Giu
  11. | Sinistra -> Su
  12.  
  13. (* avanti : posizione -> int -> posizione *)
  14. let avanti (x,y,dir) n =
  15. match dir with
  16. Su -> (x,y+n,dir)
  17. | Giu -> (x,y-n,dir)
  18. | Destra -> (x+n,y,dir)
  19. | Sinistra -> (x-n,y,dir)
  20.  
  21. (* sposta : posizione -> azione -> posizione *)
  22. let sposta (x,y,dir) act =
  23. match act with
  24. Gira -> (x,y,gira dir)
  25. (* le coordinate non cambiano,
  26. la direzione gira di 90 gradi in senso orario *)
  27. | Avanti n -> avanti (x,y,dir) n
  28.  
  29. (* esegui: posizione -> azione list -> posizione *)
  30. let rec esegui p = function
  31. [] -> p
  32. | x::xs -> esegui (sposta p x) xs
  33.  
  34.  
  35.  
  36.  
  37. (* inizia *)
  38. type nat = Zero | Succ of nat
  39.  
  40. (* somma : nat -> nat -> nat *)
  41. let rec somma n m =
  42. match n with
  43. Zero -> m
  44. | Succ k -> Succ(somma k m)
  45.  
  46. (* prodotto : nat -> nat -> nat *)
  47. let rec prodotto n m =
  48. match m with
  49. Zero -> Zero
  50. | Succ Zero -> n
  51. | Succ k -> prodotto (somma n n) k
  52.  
  53.  
  54. (* inizia *)
  55. type chiave = Aperta | Chiusa
  56. type cassaforte = chiave list
  57.  
  58. (* giraPrima: cassaforte -> cassaforte *)
  59. let giraPrima = function
  60. x::xs when x == Aperta -> Chiusa::xs
  61. | x::xs when x == Chiusa -> Aperta::xs
  62. | _ -> []
  63.  
  64. (* giraDopoChiusa: cassaforte -> cassaforte *)
  65. let rec giraDopoChiusa = function
  66. x::xs when x == Aperta -> x::(giraDopoChiusa xs)
  67. | x::xs when x == Chiusa -> x::(giraPrima xs)
  68. | _ -> raise (Failure "Nessuna chiave Chiusa trovata.")
  69.  
  70. (* successori: cassaforte -> cassaforte list *)
  71. let successori l = try [(giraPrima l); (giraDopoChiusa l)] with e -> [(giraPrima l)]
  72.  
  73.  
  74.  
  75. (* inizia *)
  76. type 'a pattern = Jolly | Val of 'a
  77.  
  78. let isConforme x y = x=y || y=Jolly;;
  79.  
  80. let first (x, _) = x
  81. let second (_, y) = y
  82.  
  83. let most_general_match l1 l2 =
  84. if List.length l1 = List.length l2
  85. then
  86. begin
  87. let result = List.partition (fun x -> List.exists (fun y -> x=y || y = Jolly) l1) l2
  88. in first(result) @ (List.filter (fun x -> x = Jolly) (second result))
  89. end
  90. else
  91. raise (Failure "Error. Lunghezze diverse.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement