Last active
January 6, 2019 13:07
-
-
Save VincentCordobes/edc3a1b35d9355a4c0a8928fadd4a7a0 to your computer and use it in GitHub Desktop.
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 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