Skip to content

Commit 210f5aa

Browse files
committed
use compare instead of guards
1 parent 5b10076 commit 210f5aa

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

source_md/making-our-own-types-and-typeclasses.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,10 +1139,11 @@ singleton x = Node x EmptyTree EmptyTree
11391139
11401140
treeInsert :: (Ord a) => a -> Tree a -> Tree a
11411141
treeInsert x EmptyTree = singleton x
1142-
treeInsert x (Node a left right)
1143-
| x == a = Node x left right
1144-
| x < a = Node a (treeInsert x left) right
1145-
| x > a = Node a left (treeInsert x right)
1142+
treeInsert x (Node a left right) =
1143+
case x `compare` a of
1144+
LT -> Node a (treeInsert x left) right
1145+
EQ -> Node x left right
1146+
GT -> Node a left (treeInsert x right)
11461147
```
11471148

11481149
The `singleton` function is just a shortcut for making a node that has something and then two empty subtrees.
@@ -1168,11 +1169,12 @@ If it's bigger, check to see if it's in the right subtree.
11681169

11691170
```{.haskell:hs}
11701171
treeElem :: (Ord a) => a -> Tree a -> Bool
1171-
treeElem x EmptyTree = False
1172-
treeElem x (Node a left right)
1173-
| x == a = True
1174-
| x < a = treeElem x left
1175-
| x > a = treeElem x right
1172+
treeElem _ EmptyTree = False
1173+
treeElem x (Node a left right) =
1174+
case x `compare` a of
1175+
LT -> treeElem x left
1176+
EQ -> True
1177+
GT -> treeElem x right
11761178
```
11771179

11781180
All we had to do was write up the previous paragraph in code.

0 commit comments

Comments
 (0)