mirror of
https://github.com/Fechin/reference.git
synced 2026-11-20 21:23:11 +03:00
2 files changed
+465
No files matched your search
@@ -0,0 +1,464 @@
|
||||
---
|
||||
title: OCaml
|
||||
date: 2024-08-06 00:00:00
|
||||
background: bg-[#ec670f]
|
||||
tags:
|
||||
- scientific
|
||||
- functional
|
||||
categories:
|
||||
- Programming
|
||||
intro: |
|
||||
The [OCaml](https://www.ocaml.org/) cheat sheet is a one-page reference sheet for the OCaml programming language.
|
||||
plugins:
|
||||
- copyCode
|
||||
---
|
||||
|
||||
## Getting Started
|
||||
|
||||
### hello.ml
|
||||
|
||||
```ml
|
||||
# let () =
|
||||
let message = "Hello, World!" in
|
||||
Printf.printf "%s\n" message
|
||||
```
|
||||
|
||||
#### Compile and Run
|
||||
|
||||
```bash
|
||||
$ ocamlc -o hello.byte hello.ml
|
||||
$ ./hello.byte
|
||||
```
|
||||
|
||||
#### Build and Run with Dune
|
||||
|
||||
```bash
|
||||
$ dune build hello.exe
|
||||
$ _build/default/hello.exe
|
||||
```
|
||||
|
||||
See the [dune](https://dune.build/) documentation for more info.
|
||||
|
||||
### Imports
|
||||
|
||||
Install modules with opam
|
||||
|
||||
```bash
|
||||
$ opam install hex
|
||||
```
|
||||
|
||||
#### Global Open
|
||||
|
||||
```ml
|
||||
# open Hex
|
||||
```
|
||||
|
||||
#### Local Open
|
||||
|
||||
```ml
|
||||
# Hex.of_string "hex string"
|
||||
|
||||
# let open Hex in
|
||||
of_string "to hex"
|
||||
|
||||
```
|
||||
|
||||
### Comments
|
||||
|
||||
#### Line & Block Comments
|
||||
|
||||
```ml
|
||||
(* A single line comment *)
|
||||
|
||||
(* A multi-line comment
|
||||
* where we want to explain
|
||||
* something complex *)
|
||||
|
||||
(* Outer comment
|
||||
(* Nested comment *)
|
||||
End of outer comment *)
|
||||
```
|
||||
|
||||
#### Documentation Comments
|
||||
|
||||
```ml
|
||||
val sum : int -> int -> int
|
||||
(** [sum x y] returns the sum
|
||||
of two integers [x] and [y] *)
|
||||
```
|
||||
|
||||
## Data Types
|
||||
|
||||
### Predefined Types
|
||||
|
||||
#### Unit
|
||||
|
||||
```ml
|
||||
# () (* eqivalent to void *)
|
||||
- : unit = ()
|
||||
```
|
||||
|
||||
#### Basic Types
|
||||
|
||||
```ml
|
||||
# 5 (* int *)
|
||||
# 3.14 (* float *)
|
||||
|
||||
# true (* bool *)
|
||||
# false
|
||||
|
||||
# 'a' (* char *)
|
||||
# "a string" (* string *)
|
||||
|
||||
# String.to_bytes "hello" (* bytes *)
|
||||
- : bytes = Bytes.of_string "hello"
|
||||
|
||||
# (3, 5) (* tuple *)
|
||||
|
||||
# ref 0 (* reference *)
|
||||
```
|
||||
|
||||
#### Options & Results
|
||||
|
||||
```ml
|
||||
# Some 42
|
||||
- : int option = Some 42
|
||||
|
||||
# Ok 42
|
||||
|
||||
# Error "404"
|
||||
```
|
||||
|
||||
### Arrays & Lists
|
||||
|
||||
#### Arrays
|
||||
|
||||
```ml
|
||||
# [|0; 1; 2; 3|]
|
||||
|
||||
# [|'u'; 's'; 'c'|].(1) (* array access *)
|
||||
- char = 's'
|
||||
```
|
||||
|
||||
Arrays are mutable
|
||||
|
||||
```ml
|
||||
# let scores = [|97; 85; 99|]
|
||||
# scores.(2) <- 89 (* update an element *)
|
||||
- unit = ()
|
||||
|
||||
# scores
|
||||
- : int array = [|97; 85; 89|]
|
||||
```
|
||||
|
||||
#### Lists
|
||||
|
||||
```ml
|
||||
# [1; 2; 3]
|
||||
- : int list = [1; 2; 3;]
|
||||
|
||||
# ["a"; "str"; "lst"]
|
||||
- : string list = ["a"; "str"; "lst"]
|
||||
```
|
||||
|
||||
Lists are immutable
|
||||
|
||||
```ml
|
||||
# let lst = [1; 2; 3]
|
||||
|
||||
# let new_lst = lst :: 4 (* append to a new list *)
|
||||
- : int list = [1; 2; 3; 4]
|
||||
```
|
||||
|
||||
### User-Defined Types
|
||||
|
||||
#### Records
|
||||
|
||||
Bundle related data
|
||||
|
||||
```ml
|
||||
# type person = {
|
||||
name: string;
|
||||
age: int
|
||||
}
|
||||
|
||||
# let zeno = {name = "Zeno"; age = 30};;
|
||||
val zeno : person = {name = "Zeno"; age = 30}
|
||||
```
|
||||
|
||||
#### Variants
|
||||
|
||||
Several different, but related types
|
||||
|
||||
```ml
|
||||
type shape =
|
||||
| Circle of float
|
||||
| Rectangle of float * float
|
||||
|
||||
let my_shape = Circle 5.0
|
||||
- : shape = Circle 5.
|
||||
```
|
||||
|
||||
#### Aliases
|
||||
|
||||
Provide meaningful name
|
||||
to complex or commonly used types
|
||||
|
||||
```ml
|
||||
# type point = float * float
|
||||
|
||||
# let origin: point = (0.0, 0.0)
|
||||
val origin : point = (0., 0.)
|
||||
```
|
||||
|
||||
## Functions
|
||||
|
||||
### Functions
|
||||
|
||||
#### Single parameter
|
||||
|
||||
```ml
|
||||
# let add_one x =
|
||||
let result = x + 1 in
|
||||
result
|
||||
|
||||
# add_one 1
|
||||
- : int = 2
|
||||
```
|
||||
|
||||
#### Multiple parameters
|
||||
|
||||
```ml
|
||||
# let sum x y =
|
||||
let result = x + y in
|
||||
result
|
||||
|
||||
# sum 1 2
|
||||
- : int = 3
|
||||
```
|
||||
|
||||
#### Tuple parameter
|
||||
|
||||
```ml
|
||||
# let str_concat (x, y) =
|
||||
x ^ " " ^ y
|
||||
|
||||
# str_concat ("Hello", "OCaml")
|
||||
- : string = "Hello Ocaml"
|
||||
```
|
||||
|
||||
### Recursive Functions
|
||||
|
||||
#### rec
|
||||
|
||||
All recusive functions use the rec keyword
|
||||
|
||||
```ml
|
||||
# let rec factorial n =
|
||||
if n < 1 then 1 else n * factorial (n - 1)
|
||||
```
|
||||
|
||||
The above is not very efficient and can cause stackoverflow.
|
||||
|
||||
#### Tail Recursion
|
||||
|
||||
Makes use of a helper function and the acc argument.
|
||||
|
||||
```ml
|
||||
# let rec factorial_helper n acc =
|
||||
if n = 0 then acc
|
||||
else factorial_helper (n - 1) (n * acc)
|
||||
```
|
||||
|
||||
Notice the last call is the recursive function.
|
||||
|
||||
```ml
|
||||
# let factorial n = factorial_helper n 1
|
||||
```
|
||||
|
||||
### Chaining
|
||||
|
||||
#### Application Operator
|
||||
|
||||
Read from right to left the first operation is `sum 2 3`
|
||||
|
||||
```ml
|
||||
(* find log(2 + 3) *)
|
||||
# log @@ float_of_int @@ sum 2 3
|
||||
- : float = 1.609...
|
||||
```
|
||||
|
||||
#### Pipeline
|
||||
|
||||
```ml
|
||||
(* find log((x + y)!) *)
|
||||
# sum 2 3
|
||||
|> factorial
|
||||
|> float_of_int
|
||||
|> log
|
||||
- : float = 4.787...
|
||||
```
|
||||
|
||||
`|>` takes the output of the function and passes it as input to the next function in the pipeline
|
||||
|
||||
## Control Flow
|
||||
|
||||
### If Statements
|
||||
|
||||
#### If Statement
|
||||
|
||||
```ml
|
||||
# let is_pos x =
|
||||
if x > 0 then "positive" else "negative"
|
||||
```
|
||||
|
||||
#### If else if
|
||||
|
||||
```ml
|
||||
# let f x =
|
||||
if x > 3 then "gt 3"
|
||||
else if x < 3 then "lt 3"
|
||||
else "eq 3"
|
||||
```
|
||||
|
||||
#### Pattern Matching
|
||||
|
||||
```ml
|
||||
let is_pos x =
|
||||
match x > 0 with
|
||||
| true -> "positive"
|
||||
| false -> "negative"
|
||||
```
|
||||
|
||||
### Loops
|
||||
|
||||
#### For loop
|
||||
|
||||
```ml
|
||||
for i = 1 to 5
|
||||
do print_int i
|
||||
done
|
||||
```
|
||||
|
||||
#### While loop
|
||||
|
||||
Notice the ref is needed to update the while condition.
|
||||
|
||||
```ml
|
||||
let i = ref 0 in
|
||||
while !i < 5 do
|
||||
print_int !i;
|
||||
i := !i + 1
|
||||
done
|
||||
```
|
||||
|
||||
### Operators
|
||||
|
||||
#### Comparison Operators
|
||||
|
||||
```ml
|
||||
= (* equal to *)
|
||||
<> or != (* not equal to *)
|
||||
> (* greater than *)
|
||||
< (* less than *)
|
||||
>= (* greater than or eq to *)
|
||||
<= (* less than or eq to *)
|
||||
```
|
||||
|
||||
#### Arithmatic Operators
|
||||
|
||||
```ml
|
||||
(* int operator float operator *)
|
||||
+ +. (* addition *)
|
||||
- -. (* subtraction *)
|
||||
* *. (* multiplication *)
|
||||
/ /. (* division *)
|
||||
** (* power *)
|
||||
```
|
||||
|
||||
## Useful Tools
|
||||
|
||||
### List
|
||||
|
||||
#### Searching & Filtering
|
||||
|
||||
```ml
|
||||
# let lst = [1;2;3]
|
||||
|
||||
# List.filter (fun x -> x mod 2 = 0) lst
|
||||
|
||||
# List.find (fun x -> x = 4) lst
|
||||
Exception: Not_found
|
||||
|
||||
# List.sort compare [2; 1; 3]
|
||||
- : int list = [1; 2; 3]
|
||||
```
|
||||
|
||||
#### Applying Transformations
|
||||
|
||||
```ml
|
||||
(* Loop over list and apply fun f *)
|
||||
# List.iter f lst
|
||||
|
||||
(* map a function to each elem *)
|
||||
# List.map (fun x -> x + x) lst
|
||||
- : int list = [2; 4; 6]
|
||||
|
||||
(* Apply an operator between elements *)
|
||||
(* Ex. 1 + 2 + 3 *)
|
||||
# let sum = List.fold_left ( + ) 0
|
||||
# sum lst
|
||||
```
|
||||
|
||||
### Associaton Lists
|
||||
|
||||
#### Definition and Access
|
||||
|
||||
```ml
|
||||
# let scores =
|
||||
[("math", 91); ("phil", 89); ("stats", 94)]
|
||||
|
||||
# List.assoc "stats" scores;;
|
||||
- : int = 94
|
||||
|
||||
# List.mem_assoc "math" scores
|
||||
- : bool = true
|
||||
```
|
||||
|
||||
#### Split and Combine
|
||||
|
||||
```ml
|
||||
# List.split scores
|
||||
- : string list * int list = (["math"; "phil"; "stats"], [91; 89; 94])
|
||||
|
||||
# List.combine [1;2;3] [4; 5; 6]
|
||||
- : (int * int) list = [(1, 4); (2, 5); (3, 6)]
|
||||
```
|
||||
|
||||
Association lists are similar to dictionaries or hashmaps
|
||||
|
||||
### Hash Tables
|
||||
|
||||
#### Initialize & Add Data
|
||||
|
||||
```ml
|
||||
|
||||
# let my_htable = Hashtbl.create 3
|
||||
val my_htable : ('_weak1, '_weak2) Hashtbl.t = <abstr>
|
||||
|
||||
# Hashtbl.add my_htable "A" "John";
|
||||
Hashtbl.add my_htable "A" "Jane";
|
||||
Hashtbl.add my_htable "B" "Max";;
|
||||
|
||||
```
|
||||
|
||||
#### Find Data
|
||||
|
||||
```ml
|
||||
# Hashtbl.find my_htable "A"
|
||||
- : string = "Jane"
|
||||
|
||||
(* find all *)
|
||||
# Hashtbl.find_all my_htable "A"
|
||||
- : string list = ["Jane"; "John"]
|
||||
```
|
||||
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 128 128"><path fill="currentColor" d="M65.004 115.355c-.461-.894-1.004-2.796-1.356-3.601c-.378-.711-1.46-2.692-1.984-3.332c-1.164-1.332-1.437-1.438-1.809-3.23c-.628-3.067-2.148-8.462-4.042-12.227c-1.004-2-2.626-3.606-4.067-5.07c-1.246-1.247-4.121-3.31-4.668-3.227c-4.766.894-6.226 5.586-8.457 9.27c-1.27 2.062-2.516 3.769-3.52 5.937c-.898 1.98-.812 4.23-2.331 5.938a15.4 15.4 0 0 0-3.333 5.855c-.195.453-.546 4.957-1.003 6.016l7.02-.438c6.585.461 4.687 2.961 14.858 2.438l16.098-.54a25 25 0 0 0-1.433-3.792zM111.793 8.254H16.207C7.312 8.23.086 15.457.086 24.352v35.105c2.352-.812 5.578-5.75 6.668-6.934c1.789-2.062 2.16-4.77 3.059-6.378c2.062-3.793 2.433-6.477 7.101-6.477c2.164 0 3.063.516 4.5 2.516c.996 1.332 2.79 3.957 3.602 5.668c1.004 1.98 2.523 4.582 3.254 5.125c.515.351.972.722 1.433.894c.707.27 1.356-.27 1.902-.629c.622-.539.895-1.52 1.52-2.953c.895-2.086 1.813-4.418 2.332-5.312c.914-1.461 1.273-3.254 2.25-4.067c1.461-1.246 3.441-1.355 3.957-1.437c2.98-.625 4.336 1.437 5.777 2.707c.973.894 2.243 2.605 3.246 4.851c.708 1.793 1.606 3.52 2.067 4.5c.351.98 1.266 2.606 1.789 4.582c.543 1.711 1.809 3.067 2.352 3.961c0 0 .812 2.164 5.476 4.145a35 35 0 0 0 4.336 1.52c2.066.734 4.047.644 6.563.374c1.789 0 2.793-2.625 3.601-4.683c.438-1.254.98-4.774 1.25-5.758c.27-.996-.437-1.707.192-2.625c.722-.977 1.164-1.082 1.519-2.332c.914-2.793 5.957-2.875 8.832-2.875c2.414 0 2.063 2.332 6.125 1.52c2.336-.434 4.586.273 7.023.995c2.063.543 4.043 1.168 5.204 2.524c.73.898 2.629 5.312.73 5.476c.164.188.36.645.625.817c-.46 1.707-2.25.46-3.332.27c-1.355-.27-2.332 0-3.684.624c-2.335.996-5.668.918-7.726 2.625c-1.715 1.438-1.715 4.582-2.543 6.371c0 0-2.254 5.696-6.996 9.192c-1.278.914-3.715 3.058-8.918 3.871c-2.356.355-4.586.355-7.024.27c-1.164-.079-2.332-.079-3.52-.079c-.706 0-3.062-.109-2.96.164l-.27.645c.024.29.063.602.164.895c.102.515.102.976.192 1.437c0 .98-.086 2.063 0 3.066c.082 2.063.894 3.957 1.004 6.102c.078 2.355 1.246 4.875 2.414 6.77c.46.707 1.086.789 1.355 1.71c.352.98 0 2.141.188 3.227c.625 4.227 1.875 8.73 3.773 12.61v.078c2.332-.352 4.77-1.247 7.836-1.684c5.664-.832 13.5-.461 18.54-.914c12.796-1.168 19.706 5.226 31.148 2.601V24.336c-.063-8.895-7.293-16.102-16.207-16.102zM64.086 83.855q0-.28 0 0m-34.457 14.75c.894-1.98 1.433-4.125 2.144-6.101c.73-1.899 1.813-4.61 3.684-5.582c-.246-.274-3.957-.375-4.934-.461c-1.082-.086-2.171-.273-3.25-.438a135 135 0 0 1-6.125-1.265c-1.168-.274-5.21-1.715-6.02-2.067c-2.085-.894-3.421-3.52-4.96-3.246c-.977.188-1.98.54-2.605 1.54c-.543.812-.731 2.242-1.083 3.226c-.437 1.086-1.168 2.164-1.707 3.25c-1.277 1.875-3.332 3.582-4.23 5.484c-.191.457-.27.895-.457 1.356v21.683c1.082.188 2.16.371 3.328.73c8.996 2.438 11.164 2.606 19.98 1.63l.813-.11c.625-1.437 1.188-6.207 1.629-7.644c.352-1.164.812-2.063.996-3.14c.164-1.09 0-2.173-.102-3.15c-.171-2.628 1.895-3.519 2.899-5.69zm0 0"/></svg>
|
||||
|
After Width: | Height: | Size: 2.9 KiB |
Reference in new issue
Block a user