OCaml 2 Basics
OCaml 2 Basics
Index
1. Recursion functions
a. Recursion (Definition, need of recursion, weaknesses, example)
b. Tail recursion (difference and advantages)
c. Mutual recursion (advantages)
2. User Defined data types
a. Variants
b. Records
c. type nats = Zero | Succ of nats
d. type num = A of int | B of float
3. ADTs
a. Algebraic Data type (Syntax, Definition, Examples, Need and advantages)
4. Functions for ADTs
a. Add function for nats
b. Convert function for nats (nats ->int)
c. Creating nats values (int ->. nats)
5. Reading inputs from the user
a. Read_int()
b. Raed_line()
6. Conditions (If-then-else)
a. Simple if else
b. Chained if else
c. Nested if else
7. Pattern Matching
a. Syntax
b. Advantages over if else
c. Advance pattern matching
8. Accessing Lists (using recursion)
a. Making list using recursion (make_list “hi” 3 -> [“hi”; “hi”; “hi”])
b. Count the number of elements
c. Take out 1st element
d. Take out last element
e. Take out both first and last element
9. Functions for lists (What they do, how to make it using recursion, how to use it)
a. List.map
b. List.nth
c. List.fold
d. List.filter
10. Higher order functions
a. Creating functions that take functions as input
b. Creating functions that return functions as output
11. Problems to test your skills
a. Basic problems
b. Medium problems
c. Hard problems
Basics of OCaml
1. Recursion Functions
• Definition: A function that calls itself within its definition to solve a problem by breaking it
into smaller sub-problems.
• Need: Useful for tasks like tree traversal, factorial calculation, and solving problems on lists.
• Weaknesses:
Example:
Example:
if n = 0 then acc
(* Usage: factorial_tail 5 1 *)
Example:
and is_odd n =
b. Records
• Syntax: Combines types using | for sum types and records for product types.
• Examples:
match n1 with
| Zero -> n2
| Zero -> 0
a. read_int()
b. read_line()
6. Conditions (If-Then-Else)
a. Simple If-Else
b. Chained If-Else
let classify n =
else "Zero";;
c. Nested If-Else
let nested_check n =
if n > 0 then
7. Pattern Matching
a. Syntax
| _ -> default_result;;
| [] -> 0
| [x] -> x
a. List.map
| [] -> []
b. List.nth
c. List.fold
| [] -> acc
d. List.filter
| [] -> []
a. Basic Problems
b. Medium Problems
c. Hard Problems
Would you like a deeper dive into any specific topic or problem?
------The End----
a. Basic Problems
if n = 0 then acc
(* Usage: factorial_tail 5 1 *)
| [] -> true
| _ -> false;;
| [] -> acc
in aux [] lst;;
| [] -> 0
| x :: xs -> (if x = elem then 1 else 0) + count_occurrences elem xs;;
| [] -> []
b. Medium Problems
type bst =
| Empty
if x = v then true
| [], l | l, [] -> l
| l, 0 -> x :: l
| [] -> [[]]
| x :: xs ->
List.concat (
);;
if n = 0 then 0
else if n = 1 then 1
if n = 0 then []
c. Hard Problems
type btree =
| Empty
| Empty -> []
| Node (v, left, right) -> inorder left @ [v] @ inorder right;;
else
@ [(source, target)]
type expr =
| Num of int
match e with
| Num n -> n
| [] -> []
| pivot :: rest ->
type expr =
| Num of int
match e with
| Num n -> n
Would you like further explanation or test cases for any specific problem?