Skip to content

Instantly share code, notes, and snippets.

@fakenickels
Created June 9, 2019 01:42
Show Gist options
  • Save fakenickels/02832cc578b7266687683f58dae60487 to your computer and use it in GitHub Desktop.
Save fakenickels/02832cc578b7266687683f58dae60487 to your computer and use it in GitHub Desktop.
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