diff --git a/DataStructures/Lists/Link.fs b/DataStructures/Lists/Link.fs new file mode 100644 index 0000000..fa26d14 --- /dev/null +++ b/DataStructures/Lists/Link.fs @@ -0,0 +1,38 @@ +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 + \ No newline at end of file diff --git a/DataStructures/Trees/RedBlack.fs b/DataStructures/Trees/RedBlack.fs index 2c7bfd2..93aa843 100644 --- a/DataStructures/Trees/RedBlack.fs +++ b/DataStructures/Trees/RedBlack.fs @@ -24,5 +24,7 @@ module RedBlack = elif y < x then balance (color, a, y, loop b) else s - let (Node (_, a, b, c)) = loop s in Node (Black, a, b, c) + match loop s with + | Node (_, y, a, b) -> Node (Black, y, a, b) + | Leaf -> failwith "红黑树插入失败,返回Leaf节点" \ No newline at end of file diff --git a/Sanchime.Learn.fsproj b/Sanchime.Learn.fsproj index e5577eb..a1976d4 100644 --- a/Sanchime.Learn.fsproj +++ b/Sanchime.Learn.fsproj @@ -10,6 +10,7 @@ +