let get_7 list = "7";; let comp (a0 :: a1 :: _) = if a0 > a1 then "Bigger" else if a0 = a1 then "Equal" else "Smaller" ;; let median (a0 :: a1 :: a2 :: _) = let sorted = List.sort compare [a0; a1; a2] in List.nth sorted 1 |> string_of_int ;; let sum list = List.fold_left (+) 0 list;; let sum_even list = list |> List.filter (fun n -> n mod 2 = 0) |> sum ;; let string_of_array array = String.init (Array.length array) (Array.get array);; let make_string list = let to_letter i = ((i mod 26) + (int_of_char 'a')) |> char_of_int in list |> List.map to_letter |> Array.of_list |> string_of_array ;; module IntSet = Set.Make(Int);; let check_loop list = let aux dest visited current = if current < 0 || current > Array.length dest then "Out" else if current = Array.length - 1 then "Done" else if IntSet.mem current visited then "Cyclic" else let next_current = dest.(current) in let next_visited = IntSet.add current visited in aux dest next_visited next_current in aux (Array.of_list dest) IntSet.empty 0 ;; let main () = let n :: command :: _ = read_line () |> String.split_on_char ' ' |> List.map int_of_string in let list = read_line () |> String.split_on_char ' ' |> List.map int_of_string in let output = match command with | 1 -> get_7 list | 2 -> comp list | 3 -> median list | 4 -> sum list | 5 -> sum_even list | 6 -> make_string list | 7 -> check_loop list | _ -> failwith "bad" in print_string output ;; main ()
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