export type List = Node | null; export class Node { readonly value: T; readonly next: List; constructor(value: T, next: List) { this.value = value; this.next = next; } toString(): string { let result = "" + this.value + " → "; if (this.next === null) { result += "null"; } else { result += this.next; } return result; } } export let first = (list: List): T => { if (list === null) { throw new Error("Cannot call function `first` on null."); } else { return list.value; } }; export let rest = (list: List): List => { if (list === null) { throw new Error("Cannot call function `rest` on null."); } else { return list.next; } }; export let cons = (value: T, list: List = null): List => { return new Node(value, list); }; export let listify = (...values: T[]): List => { if (values.length === 0) { return null; } else { return cons(values[0], listify.apply(null, values.slice(1))); } };