-
-
Save Invizory/3c08a1670eaec308006a0016cef9d02c to your computer and use it in GitHub Desktop.
Практикум, 29.09.2023
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exception Kek; | |
exception Lol(string); | |
let a = try (raise(Lol("hello"))) { | |
| Lol(x) => x | |
}; | |
let b = switch ("hello, " ++ "world!") { | |
| s => s | |
| exception Lol(x) => x | |
}; | |
raise(Kek); | |
raise(Lol("spherical horse in vacuum hung up XD")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module X = { | |
let hello = "hello"; | |
let inc = x => x + 1; | |
let twice = x => x * 2; | |
module Y = { | |
let x = 1; | |
}; | |
}; | |
let hello = X.hello; | |
let one = X.Y.x; | |
let two = X.inc(1); | |
let six = X.twice(3); | |
module Stack: { | |
type t('a); | |
let empty: t('a); | |
let push: (t('a), 'a) => t('a); | |
let top: t('a) => option('a); | |
let pop: t('a) => option(t('a)); | |
} = { | |
type t('a) = list('a); | |
let empty = []; | |
let push = (stack, x) => [x, ...stack]; | |
let top = stack => | |
switch (stack) { | |
| [] => None | |
| [head, ..._] => Some(head) | |
}; | |
let pop = stack => | |
switch (stack) { | |
| [] => None | |
| [_, ...tail] => Some(tail) | |
}; | |
}; | |
let st = Stack.push(Stack.empty, 1); | |
// let st: Stack.t(int) = [1, 2, 3]; | |
module M: { | |
type t; | |
let x: t; | |
} = { | |
type t = int; | |
let x: t = 2; | |
let y: t = 3; | |
}; | |
module type T = { | |
let x: int; | |
}; | |
module F = (N: T): T => { | |
let x = N.x + 1; | |
}; | |
module One: T = { | |
let x = 1; | |
}; | |
module Two = F(One); | |
module Three = F(Two); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type my_option('a) = | |
| MyNone | |
| MySome('a); | |
type my_result('ok, 'error) = | |
| MyOk('ok) | |
| MyError('error); | |
let a = MySome(42); | |
let b = MyNone; | |
let a = Some(42); | |
let b = None; | |
let c = a |> Base.Option.map(~f=(x) => x + 1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type t('a) = | |
| Empty | |
| Cons({ | |
head: 'a, | |
tail: t('a), | |
}); | |
let empty = Empty; | |
let one = Cons({head: 1, tail: Empty}); | |
let two = Cons({head: 2, tail: Cons({head: 1, tail: Empty})}); | |
let hello = Cons({head: "hello", tail: Empty}); | |
let rec len = xs => | |
switch (xs) { | |
| Empty => 0 | |
| Cons({tail, _}) => 1 + len(tail) | |
}; | |
let rec len_fun = | |
fun | |
| Empty => 0 | |
| Cons({tail, _}) => 1 + len_fun(tail); | |
let rec (+++) = (xs, ys) => | |
switch (xs) { | |
| Empty => ys | |
| Cons({head, tail}) => Cons({head, tail: tail +++ ys}) | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let f = (x, y, ~a, ~b) => x + y + a + b; | |
let g = f(~b=4, ~a=3, 1); | |
let n = g(2); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open Base; | |
module type S = { | |
type t; | |
let of_int: int => option(t); | |
let to_int: t => int; | |
let to_string: t => string; | |
}; | |
module Quantity: S = { | |
type t = int; | |
let of_int = fun | |
| x when x > 0 => Some(x) | |
| _ => None; | |
let to_int = (x) => x; | |
let to_string = Int.to_string; | |
}; | |
let q = Quantity.of_int(1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open Base; | |
let xs = [3, 2, 1]; | |
let xs_2 = (::)(3, (::)(2, (::)(1, []))); | |
let ys = List.map(xs, ~f=(x) => x + 1); | |
let sum = List.fold(xs, ~init=0, ~f=(+)); | |
let product = List.fold(xs, ~init=1, ~f=(*)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment