type 'a stream = Cons of 'a * 'a stream | Nil

exception Subscript
exception Empty

let head (s: 'a stream) : 'a =
  match s with
  | Cons (hd, _) -> hd
  | Nil -> raise Empty

let tail (s: 'a stream) : 'a stream =
  match s with
  | Cons (_, tl) -> tl
  | Nil -> raise Empty

let null (s: 'a stream) : bool =
  match s with
  | Nil -> true
  | _ -> false

let rec take (n: int) (s: 'a stream) : 'a list =
  match n with
  | n when n > 0 ->
    let h = head s in
    let t = tail s in
    h :: take (n - 1) t
  | _ -> []

let rec nth (n: int) (s: 'a stream) : 'a =
  match n with
  | n when n > 0 -> nth (n - 1) (tail s)
  | 0 -> head s
  | _ -> raise Subscript

let rec map (f: 'a -> 'b) (s: 'a stream) : 'b stream =
  fun () -> Cons (f (head s), map f (tail s))

let rec filter (s: 'a stream) (f: 'a -> bool) : 'a stream =
  if f (head s) then
    fun () -> Cons (head s, filter (tail s) f)
  else
    filter (tail s) f

let rec sieve (s: int stream) : int stream =
  fun () ->
    let h = head s in
    Cons (h, sieve (filter (tail s) (fun x -> x mod h <> 0)))

let rec fromn (n: int) : int stream =
  fun () -> Cons (n, fromn (n + 1))

let rec fib n m : int stream =
  fun () -> Cons (n, fib m (n + m))

let even (x: int) : bool = x mod 2 = 0

let odd (x: int) : bool = x mod 2 <> 0

let squares : int stream =
  map (fun x -> x * x) (fromn 1)

let fibs : int stream =
  let rec fib_aux a b : int stream =
    fun () -> Cons (a, fib_aux b (a + b))
  in
  fib_aux 0 1

let evenFibs : int stream =
  filter fibs even

let oddFibs : int stream =
  filter fibs odd

let is_prime (n: int) : bool =
  let rec is_divisible k =
    k * k <= n && (n mod k = 0 || is_divisible (k + 1))
  in
  n > 1 && not (is_divisible 2)

let primes : int stream =
  filter (fromn 2) is_prime

let zip (s1: 'a stream) (s2: 'b stream) (f: 'a -> 'b -> 'c) : ('c, 'a, 'b) stream =
  fun () ->
    let h1 = head s1 in
    let h2 = head s2 in
    Cons (f h1 h2, zip (tail s
 

OCaml Online Compiler

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.

About OCaml

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.

key features

  • Strongly typed functional language
  • Easy to learn
  • Very powerful type system
  • Automatic memory management
  • There is a seperate compilation of standalone applications.
  • Ocaml compiler can also produce machine codes
  • Multiple inheritance and parametric classes etc can be expressed in a simpler way
  • User can define algebraic data types

Data types

ClassificationData types
Basic data typesintegers, floating point numbers, booleans, characters, strings
Sophisticated data typestuples, arrays, lists, sets, hash tables, queues, stacks, data streams

Note:

OCaml allows users to define new data types.

Variables

Variable is a name given to the storage area in order to manipulate them in our programs.

let varible-names = value

Loops

1. If:

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*)

2. While:

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

3. For:

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