mirror of
https://github.com/Fechin/reference.git
synced 2026-11-20 21:23:11 +03:00
add sections & icon
This commit is contained in:
1 parent
55c9a87f00
commit
141bfc1e6f
2 files changed
+167
-31
No files matched your search
+166
-30
@@ -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
|
||||
```
|
||||
@@ -1 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 256 256"><g fill="none"><g clip-path="url(#skillIconsOcaml1)"><rect width="256" height="256" fill="url(#skillIconsOcaml0)" rx="60"/><path fill="#fff" d="M143.426 232.018c-.547-2.194.548-4.938-.547-7.132c-.548-2.195-2.19-2.195-2.737-3.841c-2.19-4.389-4.927-9.327-4.927-14.814c0-4.389-1.643-8.778-2.19-13.167v-6.584c0-1.098 0-2.195-.547-3.292c0-.549 0-1.646-.548-1.646l.548-1.646c0-.549 4.926-.549 6.569-.549c2.737 0 4.927 0 7.664.549c5.474 0 9.853 0 15.328-.549c11.496-1.646 16.423-6.584 19.16-8.23c10.401-7.132 15.328-19.751 15.328-19.751c1.642-3.841 1.642-10.974 5.474-13.717c4.379-3.84 11.496-3.292 16.423-5.486c2.737-1.098 4.927-2.195 8.211-1.646c2.19.548 6.022 3.292 7.117-.549l-1.643-1.646c4.38-.549 0-9.876-1.642-12.071c-2.737-2.743-6.569-4.389-10.948-5.486c-5.475-1.646-10.402-3.292-15.328-2.195c-8.759 1.646-8.212-3.292-13.139-3.292c-6.021 0-16.97.549-19.16 6.584c-1.094 2.743-1.642 2.743-3.284 4.938c-1.095 1.646 0 3.292-.548 5.487c-.547 2.194-1.642 9.876-2.737 12.619c-1.642 4.389-3.832 9.876-7.664 9.876c-5.474.548-9.853 1.097-14.233-.549c-2.737-1.097-7.116-2.743-9.306-3.292c-10.401-4.389-12.044-8.778-12.044-8.778c-1.094-1.646-3.832-4.938-4.926-8.779c-1.095-4.389-3.285-7.681-3.832-9.876c-1.095-2.194-2.738-6.035-4.38-9.876c-2.19-4.938-4.927-8.778-7.116-10.424c-3.285-2.743-6.022-7.133-12.591-6.035c-1.095 0-5.474.548-8.76 3.291c-2.189 1.647-2.736 5.487-4.926 8.779c-1.095 1.646-3.285 7.133-4.927 11.522c-1.095 3.292-1.642 5.486-3.284 6.584c-1.095 1.097-2.19 2.194-3.832 1.646l-3.285-1.646c-1.642-1.098-4.927-7.133-7.117-10.973c-1.642-3.841-5.474-9.328-7.663-12.07c-3.285-4.39-4.927-5.487-9.854-5.487c-9.854 0-10.949 5.486-15.328 13.716c-2.19 3.841-2.737 9.327-6.57 13.716c-1.642 2.195-8.758 12.62-13.685 14.266v75.714v-.548c.547-1.098.547-2.195 1.095-2.743c2.19-3.841 6.569-7.682 8.759-12.071c1.095-2.195 2.737-4.389 3.832-7.133c.547-1.646 1.095-4.937 2.19-6.583c1.094-2.195 3.284-2.744 5.474-3.292c3.832-.549 6.569 4.938 10.948 7.132c1.642 1.097 10.401 3.841 13.139 4.389c4.379 1.098 8.758 1.646 13.138 2.744c2.19.548 4.38.548 7.116 1.097c2.19 0 10.401.549 10.401 1.097c-3.832 2.195-6.569 7.682-8.21 12.071c-1.643 4.389-2.738 9.327-4.38 13.168c-2.19 4.389-6.57 6.584-6.022 12.07c0 2.195.547 4.389 0 6.584c-.547 2.743-1.642 4.389-2.19 7.133c-1.095 3.292-2.19 13.716-3.284 16.459l8.758-1.097c1.095-2.195 1.643-12.07 2.19-13.168c1.643-4.938 3.832-8.778 7.117-12.619c3.284-3.841 3.284-8.23 4.927-12.619c2.19-4.938 4.926-8.23 7.664-12.619c4.926-7.681 8.211-18.106 18.065-20.301c1.095 0 7.116 4.39 9.853 7.133c3.285 3.292 6.57 6.584 8.759 10.973c3.832 8.23 7.664 20.301 8.759 26.336c.548 3.84 1.095 3.84 3.832 7.132c1.095 1.098 3.285 5.487 4.38 7.133c.547 1.646 2.189 5.486 2.737 7.681c.547 1.097 2.189 4.938 3.284 8.23h8.212c-3.285-9.327-6.022-18.654-7.117-27.982"/></g><defs><linearGradient id="skillIconsOcaml0" x1="127.705" x2="127.705" y1=".12" y2="257.041" gradientUnits="userSpaceOnUse"><stop stop-color="#f29100"/><stop offset="1" stop-color="#ec670f"/></linearGradient><clipPath id="skillIconsOcaml1"><rect width="256" height="256" fill="#fff" rx="60"/></clipPath></defs></g></svg>
|
||||
<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>
|
||||
|
Before Width: | Height: | Size: 3.1 KiB After Width: | Height: | Size: 2.9 KiB |
Reference in new issue
Block a user