Wednesday, April 01, 2026

OCaml Language

Microsoft DotNet F# language is derived from 

OCaml - Wikipedia

OCaml (/ˈkæməl/ oh-KAM-əl, formerly Objective Caml) is a general-purposehigh-levelmulti-paradigm programming language which extends the Caml dialect of ML with object-oriented features. OCaml was created in 1996

used in some CS courses and textbooks, like this one
Competitive Programming CP4


//ocaml.org/manual/5.4/coreexamples

A Tour of OCaml · OCaml Documentation

Example: bubble sort

(* Function to swap two elements in an array *)

let exchange i j v =
  let aux = v.(i) in
  v.(i) <- v.(j);
  v.(j) <- aux

(* Function to perform a single pass of the bubble sort *)
let one_pass_vect fin v =
  for j = 1 to fin do
    if v.(j - 1) > v.(j) then
      exchange (j - 1) j v
  done

(* The main bubble sort function for arrays *)
let bubble_sort_vect v =
  for i = Array.length v - 1 downto 0 do
    one_pass_vect i v
  done

(* Example Usage: *)
let my_array = [|18; 3; 1; 5; 2|] in
bubble_sort_vect my_array;
(* my_array is now sorted in-place: [|1; 2; 3; 5; 18|] *)

No comments:

Post a Comment