Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (* inizia *)
- type direzione = Su | Giu | Destra | Sinistra
- type posizione = int * int * direzione
- type azione = Gira | Avanti of int
- (* gira : direzione -> direzione *)
- let gira = function
- Su -> Destra
- | Giu -> Sinistra
- | Destra -> Giu
- | Sinistra -> Su
- (* avanti : posizione -> int -> posizione *)
- let avanti (x,y,dir) n =
- match dir with
- Su -> (x,y+n,dir)
- | Giu -> (x,y-n,dir)
- | Destra -> (x+n,y,dir)
- | Sinistra -> (x-n,y,dir)
- (* sposta : posizione -> azione -> posizione *)
- let sposta (x,y,dir) act =
- match act with
- Gira -> (x,y,gira dir)
- (* le coordinate non cambiano,
- la direzione gira di 90 gradi in senso orario *)
- | Avanti n -> avanti (x,y,dir) n
- (* esegui: posizione -> azione list -> posizione *)
- let rec esegui p = function
- [] -> p
- | x::xs -> esegui (sposta p x) xs
- (* inizia *)
- type nat = Zero | Succ of nat
- (* somma : nat -> nat -> nat *)
- let rec somma n m =
- match n with
- Zero -> m
- | Succ k -> Succ(somma k m)
- (* prodotto : nat -> nat -> nat *)
- let rec prodotto n m =
- match m with
- Zero -> Zero
- | Succ Zero -> n
- | Succ k -> prodotto (somma n n) k
- (* inizia *)
- type chiave = Aperta | Chiusa
- type cassaforte = chiave list
- (* giraPrima: cassaforte -> cassaforte *)
- let giraPrima = function
- x::xs when x == Aperta -> Chiusa::xs
- | x::xs when x == Chiusa -> Aperta::xs
- | _ -> []
- (* giraDopoChiusa: cassaforte -> cassaforte *)
- let rec giraDopoChiusa = function
- x::xs when x == Aperta -> x::(giraDopoChiusa xs)
- | x::xs when x == Chiusa -> x::(giraPrima xs)
- | _ -> raise (Failure "Nessuna chiave Chiusa trovata.")
- (* successori: cassaforte -> cassaforte list *)
- let successori l = try [(giraPrima l); (giraDopoChiusa l)] with e -> [(giraPrima l)]
- (* inizia *)
- type 'a pattern = Jolly | Val of 'a
- let isConforme x y = x=y || y=Jolly;;
- let first (x, _) = x
- let second (_, y) = y
- let most_general_match l1 l2 =
- if List.length l1 = List.length l2
- then
- begin
- let result = List.partition (fun x -> List.exists (fun y -> x=y || y = Jolly) l1) l2
- in first(result) @ (List.filter (fun x -> x = Jolly) (second result))
- end
- else
- raise (Failure "Error. Lunghezze diverse.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement