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 binarySearchTree('a) = | |
| Empty | |
| Node(node('a)) | |
and node('a) = { | |
value: 'a, | |
left: binarySearchTree('a), | |
right: binarySearchTree('a), | |
}; | |
let empty = Empty; |
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
/*Single line function named square*/ | |
let square = x => x * x; | |
/* Multiline function named logNSquare */ | |
let logNSquare = x => { | |
let squared = x * x; | |
Js.log(squared); | |
squared; | |
}; |
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 rec insert = (tree, compare, v) => | |
switch (tree) { | |
| Empty => Node({value: v, left: Empty, right: Empty}) | |
| Node({value, left, right}) => | |
if (compare(v, value) == (-1)) { | |
Node({value, left: insert(left, compare, v), right}); | |
} else if (compare(v, value) == 1) { | |
Node({value, left, right: insert(right, compare, v)}); | |
} else { | |
tree; |
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 compare = (f, s) => | |
if (f < s) { | |
(-1); | |
} else if (f > s) { | |
1; | |
} else { | |
0; | |
}; |
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 rec remove = (tree, compare, v) => | |
switch (tree) { | |
| Empty => Empty | |
| Node({value, left: Empty, right: Empty}) => | |
compare(v, value) == 0 ? Empty : tree | |
| Node({value, left: Node(_) as left, right: Empty as right}) => | |
compare(v, value) == 0 ? | |
left : Node({value, left: remove(left, compare, v), right}) | |
| Node({value, left: Empty as left, right: Node(_) as right}) => | |
compare(v, value) == 0 ? |
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 rec min = node => | |
switch (node) { | |
| Empty => Empty | |
| Node({value, left, right}) => | |
if (left == Empty) { | |
node; | |
} else { | |
min(left); | |
} | |
}; |
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) = string; |
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 type Animal = { | |
/* Define abstract types t, dog, and cat */ | |
type t('a); | |
type dog; | |
type cat; | |
/* helpers to create a type dog or cat */ | |
let makeDog: string => t(dog); | |
let makeCat: string => t(cat); | |
let mate: (t('a), t('a)) => string; |
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 Animal: Animal = { | |
type t('a) = string; | |
type dog; | |
type cat; | |
let makeDog = d => d; | |
let makeCat = c => c; | |
let mate = (x, y) => {j|🌈 $x and $y mated! 🌈|j}; | |
}; |
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 type Animal = { | |
/* ... */ | |
let mate: (t('a), t('a)) => string; | |
let interMate: (t('a), t('b)) => string; | |
}; |
OlderNewer