From b49a98fd63def1857a5e7f6fd935aa71c8507d45 Mon Sep 17 00:00:00 2001 From: Grant Smith <57376089+ggsmith842@users.noreply.github.com> Date: Wed, 7 Aug 2024 12:38:36 -0600 Subject: [PATCH 1/8] Create ocaml.md --- source/_posts/ocaml.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 source/_posts/ocaml.md diff --git a/source/_posts/ocaml.md b/source/_posts/ocaml.md new file mode 100644 index 0000000..fa5adb8 --- /dev/null +++ b/source/_posts/ocaml.md @@ -0,0 +1,30 @@ +--- +title: Ocaml +date: 2024-08-06 00:00:00 +background: bg-[#eeeee4] +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 +``` + +### Varaibles +```ml + + + +``` From da66798b3b1e6e5f814f9f7e62c749517d89a0aa Mon Sep 17 00:00:00 2001 From: Grant Smith <57376089+ggsmith842@users.noreply.github.com> Date: Wed, 7 Aug 2024 13:38:56 -0600 Subject: [PATCH 2/8] Update ocaml.md --- source/_posts/ocaml.md | 61 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/source/_posts/ocaml.md b/source/_posts/ocaml.md index fa5adb8..6479166 100644 --- a/source/_posts/ocaml.md +++ b/source/_posts/ocaml.md @@ -22,9 +22,66 @@ let () = 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 +``` + +### Comments +```ml +(* A single line comment *) + +(* A multi-line comment +* where to explain +* something complex *) +``` + +### Expressions and Primitive Types +```ml +(* an expression *) +50 * 50 + +(* int *) +5 + 5 + +(* float *) +3.14 +. 2.72 + +(* bool *) +let theTruth = true +let untrue = false + +(* char *) +'a' + +(* string *) +"a string" +``` + + + ### Varaibles ```ml - - +(* data is immutable in ocaml *) +let x = "a string" +let y = 1 +let z = [1;2;3] ``` + +### Functions +```ml +let add_two_ints x y = + let sum = x + y in + sum +``` + + From af6368af100edd35fc6736068d26430d15835c6e Mon Sep 17 00:00:00 2001 From: Grant Smith <57376089+ggsmith842@users.noreply.github.com> Date: Wed, 7 Aug 2024 13:41:17 -0600 Subject: [PATCH 3/8] Update ocaml.md --- source/_posts/ocaml.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/_posts/ocaml.md b/source/_posts/ocaml.md index 6479166..7ab5969 100644 --- a/source/_posts/ocaml.md +++ b/source/_posts/ocaml.md @@ -68,7 +68,7 @@ let untrue = false -### Varaibles +### Variables ```ml (* data is immutable in ocaml *) let x = "a string" From 55c9a87f0033b7c9f33a8b4fb60e9aa60021a972 Mon Sep 17 00:00:00 2001 From: ggsmith842 Date: Thu, 8 Aug 2024 13:51:58 -0600 Subject: [PATCH 4/8] add icon --- source/_posts/ocaml.md | 12 +++++++----- source/assets/icon/ocaml.svg | 1 + 2 files changed, 8 insertions(+), 5 deletions(-) create mode 100644 source/assets/icon/ocaml.svg diff --git a/source/_posts/ocaml.md b/source/_posts/ocaml.md index 7ab5969..7c157db 100644 --- a/source/_posts/ocaml.md +++ b/source/_posts/ocaml.md @@ -16,13 +16,14 @@ plugins: ## Getting Started ### hello.ml + ```ml let () = let message = "Hello, World!" in Printf.printf "%s\n" message ``` -Compile and Run +Compile and Run ```bash $ ocamlc -o hello.byte hello.ml @@ -30,12 +31,14 @@ $ ./hello.byte ``` Build and Run with Dune + ```bash $ dune build hello.exe $ _build/default/hello.exe ``` ### Comments + ```ml (* A single line comment *) @@ -45,6 +48,7 @@ $ _build/default/hello.exe ``` ### Expressions and Primitive Types + ```ml (* an expression *) 50 * 50 @@ -66,9 +70,8 @@ let untrue = false "a string" ``` - - ### Variables + ```ml (* data is immutable in ocaml *) let x = "a string" @@ -78,10 +81,9 @@ let z = [1;2;3] ``` ### Functions + ```ml let add_two_ints x y = let sum = x + y in sum ``` - - diff --git a/source/assets/icon/ocaml.svg b/source/assets/icon/ocaml.svg new file mode 100644 index 0000000..f29001c --- /dev/null +++ b/source/assets/icon/ocaml.svg @@ -0,0 +1 @@ + \ No newline at end of file From 141bfc1e6f2de434b17f8bdb5534819eb264a4e4 Mon Sep 17 00:00:00 2001 From: ggsmith842 Date: Thu, 8 Aug 2024 21:10:46 -0600 Subject: [PATCH 5/8] add sections & icon --- source/_posts/ocaml.md | 196 +++++++++++++++++++++++++++++------ source/assets/icon/ocaml.svg | 2 +- 2 files changed, 167 insertions(+), 31 deletions(-) diff --git a/source/_posts/ocaml.md b/source/_posts/ocaml.md index 7c157db..98b2270 100644 --- a/source/_posts/ocaml.md +++ b/source/_posts/ocaml.md @@ -1,7 +1,7 @@ --- title: Ocaml date: 2024-08-06 00:00:00 -background: bg-[#eeeee4] +background: bg-[#ec670f] tags: - scientific - functional @@ -18,7 +18,7 @@ plugins: ### hello.ml ```ml -let () = +# let () = let message = "Hello, World!" in Printf.printf "%s\n" message ``` @@ -37,53 +37,189 @@ $ dune build hello.exe $ _build/default/hello.exe ``` +### Imports + +Global Open + +```ml +# open Hex +``` + +Local Open + +```ml +# Hex.of_string "hex string" + +# let open Hex in + of_string "to hex" + +``` + ### Comments ```ml (* A single line comment *) (* A multi-line comment -* where to explain +* where we want to explain * something complex *) + ``` -### Expressions and Primitive Types +Documentation Comments ```ml -(* an expression *) -50 * 50 +val sum : int -> int -> int +(** [sum x y] returns the sum + of two integers [x] and [y] *) -(* int *) -5 + 5 - -(* float *) -3.14 +. 2.72 - -(* bool *) -let theTruth = true -let untrue = false - -(* char *) -'a' - -(* string *) -"a string" ``` -### Variables +## Data Types + +### Predefined Types + +Unit ```ml -(* data is immutable in ocaml *) -let x = "a string" -let y = 1 -let z = [1;2;3] - +# () +- : unit = () ``` +The unit is eqivalent to void + +```ml +# 5 (* int *) + +# 3.14 (* float *) + +# true (* bool *) +# false + +# 'a' (* char *) + +# "a string" (* string *) + +# String.to_bytes "hello" (* bytes *) +- : bytes = Bytes.of_string "hello" +``` + +### 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 + ### Functions ```ml -let add_two_ints x y = - let sum = x + y in - sum +# let sum x y = + let result = x + y in + result + +# sum 2 3 +-: int = 5 +``` + +Recursive Functions + +```ml +# let rec factorial n = + if n < 1 then 1 else n * factorial (n - 1) +``` + +Pipeline + +```ml +(* find log((x + y)!) *) +# sum 2 3 + |> factorial + |> float_of_int + |> log +- : float = 4.787... +``` + +### Control Flow and Operators + +If Statement + +```ml +# let is_pos x = + if x > 0 then "positive" else "negative" +``` + +Pattern Matching + +```ml +let is_pos x = + match x > 0 with + | true -> "positive" + | false -> "negative" +``` + +Operators + +```ml += (* equal to *) +<> or != (*not eqaul *) +> (* greater than *) +< (* less than *) +>= (* greater than or eq to *) +<= (* less than or eq to *) +``` + +### List Operations + +```ml +(* an int list*) +# let lst = [1;2;3] + +(* map a function to each elem *) +# List.map (fun x -> x + x) lst +- : int list = [2; 4; 6] + +# List.mem 4 lst +- : bool = false + +# List.find (fun x -> x = 4) lst +Exception: Not_found + +``` + +Associaton Lists + +```ml +# let scores = + [("math", 91); ("phil", 89); ("stats", 94)] + +# List.assoc "stats" scores;; +- : int = 94 ``` diff --git a/source/assets/icon/ocaml.svg b/source/assets/icon/ocaml.svg index f29001c..90792d5 100644 --- a/source/assets/icon/ocaml.svg +++ b/source/assets/icon/ocaml.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file From 2923f6f1601f16af5e149d598b03df5d4008dc27 Mon Sep 17 00:00:00 2001 From: ggsmith842 Date: Sat, 10 Aug 2024 13:04:51 -0600 Subject: [PATCH 6/8] add sections --- source/_posts/ocaml.md | 103 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 96 insertions(+), 7 deletions(-) diff --git a/source/_posts/ocaml.md b/source/_posts/ocaml.md index 98b2270..9b7c59a 100644 --- a/source/_posts/ocaml.md +++ b/source/_posts/ocaml.md @@ -1,5 +1,5 @@ --- -title: Ocaml +title: OCaml date: 2024-08-06 00:00:00 background: bg-[#ec670f] tags: @@ -8,7 +8,7 @@ tags: categories: - Programming intro: | - The [Ocaml](https://www.ocaml.org/) cheat sheet is a one-page reference sheet for the Ocaml programming language. + The [OCaml](https://www.ocaml.org/) cheat sheet is a one-page reference sheet for the OCaml programming language. plugins: - copyCode --- @@ -90,18 +90,31 @@ The unit is eqivalent to void ```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 @@ -138,6 +151,54 @@ Arrays are mutable 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 ```ml @@ -156,6 +217,12 @@ Recursive Functions if n < 1 then 1 else n * factorial (n - 1) ``` +Application Operator + +```ml +# log @@ float_of_int @@ sum 2 3 +``` + Pipeline ```ml @@ -167,7 +234,7 @@ Pipeline - : float = 4.787... ``` -### Control Flow and Operators +### Control Flow If Statement @@ -176,6 +243,15 @@ If Statement if x > 0 then "positive" else "negative" ``` +If else if else + +```ml +# let f x = + if x > 3 then "gt 3" + else if x < 3 then "lt 3" + else "eq 3" +``` + Pattern Matching ```ml @@ -185,17 +261,30 @@ let is_pos x = | false -> "negative" ``` -Operators +### Operators + +Comparison Operators ```ml = (* equal to *) -<> or != (*not eqaul *) +<> 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 *) + +``` + ### List Operations ```ml From ddef3a8b5dd715beb083fa7414f97648f9f3a211 Mon Sep 17 00:00:00 2001 From: ggsmith842 Date: Sat, 10 Aug 2024 19:43:49 -0600 Subject: [PATCH 7/8] formatting & add content --- source/_posts/ocaml.md | 46 +++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/source/_posts/ocaml.md b/source/_posts/ocaml.md index 9b7c59a..955e6ee 100644 --- a/source/_posts/ocaml.md +++ b/source/_posts/ocaml.md @@ -79,14 +79,14 @@ val sum : int -> int -> int ### Predefined Types -Unit +#### Unit ```ml -# () +# () (* eqivalent to void *) - : unit = () ``` -The unit is eqivalent to void +#### Basic Types ```ml # 5 (* int *) @@ -106,7 +106,7 @@ The unit is eqivalent to void # ref 0 (* reference *) ``` -Options & Results +#### Options & Results ```ml # Some 42 @@ -199,6 +199,8 @@ to complex or commonly used types val origin : point = (0., 0.) ``` +## Functions + ### Functions ```ml @@ -210,13 +212,15 @@ val origin : point = (0., 0.) -: int = 5 ``` -Recursive Functions +### Recursive Functions ```ml # let rec factorial n = if n < 1 then 1 else n * factorial (n - 1) ``` +### Chaining + Application Operator ```ml @@ -234,7 +238,9 @@ Pipeline - : float = 4.787... ``` -### Control Flow +## Control Flow + +### If Statements If Statement @@ -243,7 +249,7 @@ If Statement if x > 0 then "positive" else "negative" ``` -If else if else +If else if ```ml # let f x = @@ -261,6 +267,26 @@ let is_pos x = | false -> "negative" ``` +### Loops + +For loop + +```ml +for i = 1 to 5 + do print_int i +done +``` + +While loop + +```ml +let i = ref 0 in + while !i < 5 do + print_int !i; + i := !i + 1 +done +``` + ### Operators Comparison Operators @@ -282,10 +308,10 @@ Arithmatic Operators - -. (* subtraction *) * *. (* multiplication *) / /. (* division *) - + ** (* power *) ``` -### List Operations +### List Library ```ml (* an int list*) @@ -303,7 +329,7 @@ Exception: Not_found ``` -Associaton Lists +#### Associaton Lists ```ml # let scores = From 3a0be691fcfb4272178368253fa05d8bc77d11f3 Mon Sep 17 00:00:00 2001 From: ggsmith842 Date: Sun, 11 Aug 2024 21:40:54 -0600 Subject: [PATCH 8/8] add content & format --- source/_posts/ocaml.md | 182 ++++++++++++++++++++++++++++++++++------- 1 file changed, 153 insertions(+), 29 deletions(-) diff --git a/source/_posts/ocaml.md b/source/_posts/ocaml.md index 955e6ee..30ce9fc 100644 --- a/source/_posts/ocaml.md +++ b/source/_posts/ocaml.md @@ -23,29 +23,37 @@ plugins: Printf.printf "%s\n" message ``` -Compile and Run +#### Compile and Run ```bash $ ocamlc -o hello.byte hello.ml $ ./hello.byte ``` -Build and Run with Dune +#### 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 -Global Open +Install modules with opam + +```bash +$ opam install hex +``` + +#### Global Open ```ml # open Hex ``` -Local Open +#### Local Open ```ml # Hex.of_string "hex string" @@ -57,6 +65,8 @@ Local Open ### Comments +#### Line & Block Comments + ```ml (* A single line comment *) @@ -64,15 +74,17 @@ Local Open * where we want to explain * something complex *) +(* Outer comment + (* Nested comment *) + End of outer comment *) ``` -Documentation Comments +#### Documentation Comments ```ml val sum : int -> int -> int (** [sum x y] returns the sum of two integers [x] and [y] *) - ``` ## Data Types @@ -203,31 +215,80 @@ val origin : point = (0., 0.) ### 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 2 3 --: int = 5 + +# 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) ``` -### Chaining +The above is not very efficient and can cause stackoverflow. -Application Operator +#### Tail Recursion + +Makes use of a helper function and the acc argument. ```ml -# log @@ float_of_int @@ sum 2 3 +# let rec factorial_helper n acc = + if n = 0 then acc + else factorial_helper (n - 1) (n * acc) ``` -Pipeline +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)!) *) @@ -238,18 +299,20 @@ Pipeline - : 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 +#### If Statement ```ml # let is_pos x = if x > 0 then "positive" else "negative" ``` -If else if +#### If else if ```ml # let f x = @@ -258,7 +321,7 @@ If else if else "eq 3" ``` -Pattern Matching +#### Pattern Matching ```ml let is_pos x = @@ -269,7 +332,7 @@ let is_pos x = ### Loops -For loop +#### For loop ```ml for i = 1 to 5 @@ -277,7 +340,9 @@ for i = 1 to 5 done ``` -While loop +#### While loop + +Notice the ref is needed to update the while condition. ```ml let i = ref 0 in @@ -289,7 +354,7 @@ done ### Operators -Comparison Operators +#### Comparison Operators ```ml = (* equal to *) @@ -300,7 +365,7 @@ Comparison Operators <= (* less than or eq to *) ``` -Arithmatic Operators +#### Arithmatic Operators ```ml (* int operator float operator *) @@ -311,25 +376,43 @@ Arithmatic Operators ** (* power *) ``` -### List Library +## Useful Tools + +### List + +#### Searching & Filtering ```ml -(* an int list*) # 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] -# List.mem 4 lst -- : bool = false - -# List.find (fun x -> x = 4) lst -Exception: Not_found - +(* Apply an operator between elements *) +(* Ex. 1 + 2 + 3 *) +# let sum = List.fold_left ( + ) 0 +# sum lst ``` -#### Associaton Lists +### Associaton Lists + +#### Definition and Access ```ml # let scores = @@ -337,4 +420,45 @@ Exception: Not_found # 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 = + +# 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"] ```