module Coord = struct (** Une coordonnée, représentée par un x et un y *) type t = { x : float; y : float; } (** [to_string c] représente une coordonnée [c] sous forme de chaîne de caractère, par exemple [(0.12, 1.34)] *) let to_string (coord : t) : string = Printf.sprintf "{x = %.2f; y = %.2f}" coord.x coord.y end module Line = struct (** Une ligne partant d'une origine jusqu'à une destination *) type t = { origin : Coord.t; destination : Coord.t; } (** [to_string l] représente une ligne [l] sous forme de chaîne de caractère, par exemple: [(1.00, 1.00) -> (2.00, 2.00)] *) let to_string (line : t) : string = Printf.sprintf "{origin=%s; destination=%s}" (Coord.to_string line.origin) (Coord.to_string line.destination) end module Canvas = struct (** Un canevas utilisé pour dessiner un ensemble de lignes à l'écran. Cette structune n'est là que pour visualiser le résultat d'un L-system *) type t = { topleft: Coord.t; (** La coordonnée supérieure gauche de l'écran *) bottomright: Coord.t; (** La coordonnée inférieure droite de l'écran *) lines: Line.t list; (** Les lignes à dessiner *) } end module Atom = struct type t = string list;; let rec to_string a = match a with | [x] -> x | tete :: queue -> tete ^ " " ^ to_string queue | _ -> failwith "Cela ne se passe jamais!";; end;; module System = struct type t = { iterations : int ; angle : float; init : Atom.t; regles : (string * Atom.t) list };; let regle_of_string line = let morceaux = String.split_on_char ' ' line in ( List.hd morceaux , List.tl (List.tl morceaux ) );; let of_string s = let lines = String.split_on_char '\n' s in let strit = List.hd lines in let iteration = int_of_string ( List.nth (String.split_on_char ' ' strit) 2 ) in let strangle = List.nth lines 1 in let angle = float_of_string ( List.nth (String.split_on_char ' ' strangle) 2 ) in let strInit = List.nth lines 2 in let atom = List.tl (List.tl (String.split_on_char ' ' strInit)) in let regles = List.map regle_of_string ( List.tl ( List.tl( List.tl lines ) ) ) in { iterations = iteration; angle = angle; init = atom; regles = regles } ;; let remplace regles lettre = let opt = List.find_opt (fun regle -> fst regle = lettre ) regles in match opt with | None -> [lettre] | Some v -> snd v ;; let rec applyOnce regles atom = match atom with | [] -> [] | tete :: queue -> remplace regles tete @ applyOnce regles queue;; let rec applyRec regles atom iteration = if iteration = 0 then atom else applyRec regles (applyOnce regles atom) (iteration - 1);; let apply ?iterations lsystem = applyRec lsystem.regles lsystem.init (Option.value ~default:lsystem.iterations iterations);; let draw ?iterations ?angle lsystem = failwith "Not yet implemented!" end;; print_string "Hello, World!"; print_string ( Atom.to_string ["Hello "; "Eyana!"] ); let regle = System.regle_of_string "F : F F - F - F - F - F - F + F" in print_string (fst regle); print_string "\n"; print_int (List.length (snd regle)); print_string "\n"; let lsystem = System.of_string "iterations = 4\nangle = 90\ninit = F - F - F - F\nF : F F - F - F - F - F - F + F" in print_int lsystem.iterations; print_string "\n"; print_float lsystem.angle; print_string "\n"; print_int (List.length lsystem.init); print_string "\n"; print_int (List.length lsystem.regles); print_string "\n"; let newatom = System.remplace lsystem.regles "F" in print_string (Atom.to_string newatom); print_string "\n"; let newatom2 = System.applyOnce lsystem.regles ["F";"-";"F"] in print_string (Atom.to_string newatom2); print_string "\n"; let newatom3 = System.applyRec lsystem.regles ["F";"-";"F"] 2 in print_string (Atom.to_string newatom3); print_string "\n"; let newatom5 = System.apply lsystem ~iterations:0 in print_string (Atom.to_string newatom5); print_string "\n"; print_int lsystem.iterations; print_string "\n"; let newatom4 = System.apply lsystem in print_string (Atom.to_string newatom4); print_string "\nAll worked fine!";
Write, Run & Share OCaml code online using OneCompiler's OCaml online compiler for free. It's one of the robust, feature-rich online compilers for OCaml language, running on the latest version 4. Getting started with the OneCompiler's OCaml compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as OCaml
. OneCompiler also has reference programs, where you can look for the sampleprograms and start coding.
OCaml is general purpose programming language with more importance to safety and expressiveness. With it's advanced type system, it helps to catch the mistakes in an efficient way. Hence this is used to develop applications/environments where a single mistake can cost millions and speed matters. It has good community support and rich set of development tools and libraries.
Classification | Data types |
---|---|
Basic data types | integers, floating point numbers, booleans, characters, strings |
Sophisticated data types | tuples, arrays, lists, sets, hash tables, queues, stacks, data streams |
OCaml allows users to define new data types.
Variable is a name given to the storage area in order to manipulate them in our programs.
let varible-names = value
If is performed when you need to choose expression based on a boolean-condition.
if boolean-condition then (* code if condition is true *)
if boolean-condition then (* code if condition is true*) else (* code if condition is false*)
While is used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while boolean-condition do
(* code *)
done
For loop is used to iterate a set of statements for specific number of items.
for var = start-value to end-value do
(* code *)
done
for var = start-value downto end-value do
(* code *)
done