Skip to content

Instantly share code, notes, and snippets.

@VincentCordobes
Last active January 6, 2019 13:07
Show Gist options
  • Save VincentCordobes/edc3a1b35d9355a4c0a8928fadd4a7a0 to your computer and use it in GitHub Desktop.
Save VincentCordobes/edc3a1b35d9355a4c0a8928fadd4a7a0 to your computer and use it in GitHub Desktop.
type List<T> =
| { type: "Empty" }
| {
type: "List";
head: T;
list: List<T>;
};
// use it like so
function empty<T>(): List<T> {
return { type: "Empty" };
}
function cons<T>(head: T, list: List<T>): List<T> {
return { type: "List", head, list };
}
const myNumbers: List<number> = cons(1, empty());
const yourNumbers = cons(3, cons(2, myNumbers));
console.log(yourNumbers);
// {
// type: "List",
// head: 3,
// list: {
// type: "List",
// head: 2,
// list: {
// type: "List",
// head: 1,
// list: {
// type: "Empty"
// }
// }
// }
// };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment