implement function to go specific number in search-list.sml

This commit is contained in:
2024-11-12 07:57:36 +00:00
parent 1bae34a067
commit 3b81d087f0
2 changed files with 44 additions and 5 deletions

View File

@@ -135,10 +135,49 @@ struct
(* look at elements to see which way to traverse *)
case right of
hd :: _ =>
if Vector.sub (hd, 0) >= new then
insRight (new, left, right)
if Vector.sub (hd, 0) >= new then insRight (new, left, right)
else insLeft (new, left, right)
| [] => insLeft (new, left, right)
fun helpGoToNumLeft (num, left, right) =
case left of
hd :: tl =>
if num < Vector.sub (hd, 0) then
(* continue *)
helpGoToNumLeft
( num, tl
, joinStartOfRight (hd, right)
)
else
insLeft (new, left, right)
| [] =>
insLeft (new, left, right)
(* greater or equal to first element so return.
* Note: caller which destructures list expects found hd to always be
* on right. *)
{ left = tl
, right = joinStartOfRight (hd, right)
}
| [] => {left = left, right = right}
fun helpGoToNumRight (num, left, right) =
case right of
hd :: tl =>
if num > Vector.sub (hd, Vector.length hd - 1) then
(* continue *)
helpGoToNumRight
( num
, joinEndOfLeft (hd, left)
, tl
)
else
(* less than or equal to last element so return *)
{left = left, right = right}
| [] => {left = left, right = right}
fun goToNum (num, {left, right}: t) =
case right of
hd :: tl =>
if num > Vector.sub (hd, Vector.length hd - 1) then
helpGoToNumRight (num, left, right)
else
helpGoToNumLeft (num, left, right)
| [] => helpGoToNumLeft (num, left, right)
end

BIN
shf

Binary file not shown.