Sanchime.Learn/DataStructures/Lists/Link.fs

38 lines
854 B
Forth

namespace Sanchime.DataStructures.Lists
module Link =
exception EmptyList
type LinkList<'T> =
| Cons of 'T * 'T LinkList
| Nil
let first = function
| Nil -> raise EmptyList
| Cons(value, _) -> value
let last = function
| Nil -> Nil
| Cons(_, tail) -> tail
let length list =
let rec loop list count =
match list with
| Nil -> count
| Cons(_, tail) -> loop tail (count + 1)
loop list 0
let rec map mapper = function
| Nil -> Nil
| Cons(head, tail) -> Cons(mapper head, map mapper tail)
let rec append left right =
match left with
| Nil -> right
| Cons(head, tail) -> Cons(head, append tail right)
let empty = function
| Nil -> true
| Cons(_, _) -> false