Created
June 9, 2019 01:42
-
-
Save fakenickels/02832cc578b7266687683f58dae60487 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 SafeList = { | |
type empty = | |
| Empty; | |
type nonEmpty = | |
| NonEmpty; | |
type t('a, 's) = | |
| []: t('a, empty) | |
| ::(('a, t('a, 's))): t('a, nonEmpty); | |
let rec length: type s. t(_, s) => int = | |
fun | |
| [] => 0 | |
| [h, ...t] => 1 + length(t); | |
let head: type a. t(a, nonEmpty) => a = | |
fun | |
| [x, ..._] => x; | |
let tail: t('a, nonEmpty) => t('a, nonEmpty) = | |
fun | |
| [a] => [a] | |
| [a, b, ...tail] => [b, ...tail] | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment