Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. fun focus (x :: xs, ys) = SOME (x, xs, ys)
  2. | focus _ = NONE
  3.  
  4. fun first xs = focus (xs, nil)
  5. fun next (x, xs, ys) = focus (xs, x :: ys)
  6.  
  7. fun focused (x, _, _) = x
  8. fun restore (x, xs, ys) = (rev ys, x :: xs)
  9.  
  10. fun loop (_, NONE) = NONE
  11. | loop (x, SOME xs) =
  12. if x = focused xs then
  13. SOME xs
  14. else
  15. loop (x, next xs)
  16.  
  17. fun splitAtFirst (x, xs) =
  18. case loop (x, first xs) of
  19. NONE => (xs, nil)
  20. | SOME xs => restore xs
  21.  
  22. fun loop (_, xs, NONE) = xs
  23. | loop (x, xs, SOME ys) =
  24. if x = focused ys then
  25. loop (x, SOME ys, next ys)
  26. else
  27. loop (x, xs, next ys)
  28.  
  29. fun splitAtLast (x, xs) =
  30. case loop (x, NONE, first xs) of
  31. NONE => (xs, nil)
  32. | SOME xs => restore xs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement