Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (* Exercice 1 *)
- (* 1 *)
- type natural = Zero | S of natural ;;
- (* 2 *)
- let rec addition = fun n1 -> fun n2 ->
- match n1 with
- | Zero -> n2
- | (* ? *)
- addition (S(Zero)) (S(Zero)) ;;
- let rec multiplication = fun n1 n2 ->
- match n1 with
- | (* ? *)
- multiplication (S(S(Zero))) (S Zero);;
- (* 3 *)
- let rec factorial = fun n ->
- match n with
- | Zero -> (* ? *)
- | (* ? *)
- factorial (S (S Zero)) ;;
- factorial (S (S (S Zero))) ;;
- (* Exercice 2 *)
- let rec string_of_natural = fun m ->
- match m with
- | Zero -> "0"
- | (* ? *)
- string_of_natural (S (S Zero)) ;;
- let rec natural_of_string = fun s ->
- let lg = String.length s in
- if lg = 1 then Zero
- (* ? *) ;;
- natural_of_string "SS0" ;;
- let rec natural_of_int = fun n ->
- if n < 0 then failwith "négatif !" else
- match n with
- (* ? *);;
- natural_of_int 7 ;;
- (* A FAIRE : int_of_natural *)
- int_of_natural (S (S (S Zero))) ;;
- (* Exercice 3 *)
- type pile = natural list ;;
- let string_of_pile = fun (p:pile) ->
- (* Une fonction aux(iliaire) peut être utile *)
- match p with
- | [] -> (* ? *)
- | (* ? *)
- in aux 1 p "" ;;
- print_string (string_of_pile []) ;;
- let s= string_of_pile [natural_of_int 2; natural_of_string "SSSS0"; S(Zero)] ;;
- print_string s;;
- (* Exercice 4 *)
- type calculator_input =
- | Natural of natural
- | Addition
- | Multiplication
- | Factorial
- | Clear
- let calculator = fun p c ->
- match c with
- | Natural n -> n::p
- | Addition -> begin match p with
- [] -> failwith "operation incorrecte! "
- | (* ? *)
- | (* ? *)
- end
- | Multiplication -> begin (* ?*) end
- | Factorial -> begin (* ? *) end
- | Clear -> []
- exception Stop ;;
- let input_of_string = fun s ->
- if s = "" then raise Stop else
- (* Regarder le premier caractere est suffisant *)
- let c = s.[0] in
- match c with
- | '0' -> Natural (Zero)(* ? *)
- | (* ? *)
- | _ -> raise Stop ;;
- input_of_string "C" ;;
- input_of_string "SSS0" ;;
- input_of_string "+" ;;
- input_of_string "F" ;;
- (* Exercice 5 *)
- let rec top_level = fun (p:pile) ->
- (* ? *)
- let main() =
- print_string "Calculatrice de Peano";
- print_newline();
- top_level [] ;;
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement