Skip to content

Commit

Permalink
refactor isSublist
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode committed Jan 3, 2024
1 parent f3db258 commit 8b518e2
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions exercises/practice/sublist/.meta/example.sml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ datatype relation =
| Unequal

local
fun isPrefix (listOne: int list, listTwo: int list): bool =
case (listOne, listTwo) of
(nil, _) => true
| (_, nil) => false
| (firstOne :: restOne, firstTwo :: restTwo) => firstOne = firstTwo andalso isPrefix (restOne, restTwo)

fun isSublist (listOne: int list, listTwo: int list): bool =
if length listTwo < length listOne then false
else if listOne = List.take (listTwo, length listOne) then true
else isSublist (listOne, tl listTwo)
isPrefix (listOne, listTwo) orelse (listTwo <> nil andalso isSublist (listOne, tl listTwo))
in
fun sublist (listOne: int list, listTwo: int list): relation =
if listOne = listTwo then Equal
Expand Down

0 comments on commit 8b518e2

Please sign in to comment.