list_extra.sml 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. structure ListExtra = struct
  2. fun is_list_prefix nil nil = true
  3. | is_list_prefix nil lst_elems = true
  4. | is_list_prefix prefix_elems nil = false
  5. | is_list_prefix prefix_elems lst_elems =
  6. if (hd prefix_elems) = (hd lst_elems)
  7. then is_list_prefix (tl prefix_elems) (tl lst_elems)
  8. else false;
  9. fun prepend nil nil = []
  10. | prepend xlst nil = xlst
  11. | prepend nil ys = ys
  12. | prepend (x::nil) ys = x :: ys
  13. | prepend (x::xs) ys =
  14. x :: prepend xs ys;
  15. fun sum_lists (nil, nil) = []
  16. | sum_lists (nil, l2) = []
  17. | sum_lists (l1, nil) = []
  18. | sum_lists (alst as (a::arest), blst as (b::brest)) =
  19. (a + b) :: sum_lists(arest, brest);
  20. fun make_list (0, _) = []
  21. | make_list (len, fill) =
  22. fill :: make_list (len - 1, fill);
  23. fun flatten nil = nil
  24. | flatten (l::ls) =
  25. l @ (flatten ls);
  26. fun remove nil elem = nil
  27. | remove (lst as (x::xs)) elem =
  28. if x = elem
  29. then xs
  30. else x :: (remove xs elem);
  31. (* fun last nil = raise Fail "empty list, not last element" *)
  32. (* | last (x::nil) = x *)
  33. (* | last (x::xs) = last xs; *)
  34. (* fun prexifes nil = [] *)
  35. (* | prexifes lst = *)
  36. (* let *)
  37. (* fun iter (nil, acc) = acc *)
  38. (* | iter (remaining, acc) = *)
  39. (* (hd remaining) :: acc *)
  40. (* in *)
  41. (* rev (iter (lst, [[]])) *)
  42. (* end; *)
  43. end;