let list_of_string s = let rec iter i s = if i = (String.length s) then [] else s.[i] :: (iter (i + 1)) s in iter 0 s let is_digit c = c >= '0' && c <= '9' let is_upper c = c >= 'A' && c <= 'Z' let is_lower c = c >= 'a' && c <= 'z' let is_alpha c = (is_lower c) || (is_upper c) let is_alphanum c = (is_alpha c) || (is_digit c) let num s = let pos = ref 0 in let rec one (c : char) = if !pos >= Bytes.length s then false else if (is_digit c) then begin pos := !pos + 1; two s.[!pos] end else false and two (c : char) = if !pos >= Bytes.length s then true else if (is_digit c) then begin pos := !pos + 1; two s.[!pos] end else false in s, (one s.[0]) (* The following solution is based on DFA simulation. As the given FSA is an NFA, it had to be converted to a DFA which is as follows: 'a, 'b ----> \ / 'a \ / _ 1' ----> 2' ----> 3' ^ | | | |--------| 'a, 'b 'a -> alpha, 'b -> beta The above conversion has been done using the 'subset construction' method. *) let id s = let pos = ref 0 in let rec one (c : char) = if (is_alpha c) then begin if (!pos+1 == Bytes.length s) then true else begin pos := !pos + 1; two s.[!pos] end end else false and two (c : char) = if (is_alpha c || is_alphanum c) then begin if (!pos+1 == Bytes.length s) then true else begin pos := !pos + 1; two s.[!pos] end end else if c=='_' then begin if (!pos+1 == Bytes.length s) then false else begin pos := !pos + 1; three s.[!pos] end end else false and three (c : char) = if (is_alpha c || is_alphanum c) then begin if (!pos+1 == Bytes.length s) then true else begin pos := !pos + 1; two s.[!pos] end end else false in s, (one s.[0]) let test_nums () = let n = [ "a"; "123"; "2a3"; "Ab"] in let result = List.map num n in List.iter (fun (x, y) -> (Printf.printf "%s -> %b;\n" x y)) result let test_ids () = let n = [ "a"; "ab";"_abc";"ab_c";"a1_b";"1a"] in let result = List.map id n in List.iter (fun (x, y) -> (Printf.printf "%s -> %b;\n" x y)) result let _ = test_ids ()
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