home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / src / mosmllib / List.sml < prev    next >
Encoding:
Text File  |  1997-08-18  |  2.3 KB  |  101 lines  |  [TEXT/R*ch]

  1. (* List -- as of 1995-03-08, 1996-04-19 *)
  2.  
  3. type 'a list = 'a list
  4.  
  5. exception Empty;
  6.  
  7. fun null [] = true
  8.   | null _  = false;
  9.  
  10. fun hd []      = raise Empty
  11.   | hd (x::xr) = x;
  12.  
  13. fun tl []      = raise Empty
  14.   | tl (x::xr) = xr;
  15.  
  16. fun last []      = raise Empty
  17.   | last [x]     = x
  18.   | last (x::xr) = last xr;
  19.  
  20. fun nth (xs, n) =
  21.     let fun h []      _ = raise Subscript
  22.       | h (x::xr) n = if n=0 then x else h xr (n-1)
  23.     in if n<0 then raise Subscript else h xs n end;
  24.  
  25. fun drop (xs, n) =
  26.     let fun h xs      0 = xs
  27.       | h []      n = raise Subscript
  28.       | h (x::xr) n = h xr (n-1)
  29.     in if n<0 then raise Subscript else h xs n end;
  30.  
  31. fun take (xs, n) =
  32.     let fun h xs      0 = []
  33.       | h []      n = raise Subscript
  34.       | h (x::xr) n = x :: h xr (n-1)
  35.     in if n<0 then raise Subscript else h xs n end;
  36.  
  37. fun length xs =
  38.     let fun acc []      k = k
  39.           | acc (x::xr) k = acc xr (k+1)
  40.     in acc xs 0 end;
  41.  
  42. local
  43.   fun revAcc [] ys = ys
  44.     | revAcc (x::xs) ys = revAcc xs (x::ys)
  45. in 
  46.   fun rev xs = revAcc xs []
  47.  
  48.   fun revAppend (xs, ys) = revAcc xs ys
  49. end
  50.  
  51. local
  52.   fun append [] ys = ys
  53.     | append (x::xs) ys = x :: append xs ys
  54. in
  55.   fun xs @ ys = append xs ys
  56. end
  57.  
  58. fun concat []        = []
  59.   | concat (xs::xsr) = xs @ concat xsr;
  60.  
  61. fun app f []      = ()
  62.   | app f (x::xr) = (f x; app f xr);
  63.  
  64. fun map f [] = []
  65.   | map f (x::xs) = f x :: map f xs
  66.  
  67. fun mapPartial f []      = []
  68.   | mapPartial f (x::xr) = case f x of NONE   => mapPartial f xr
  69.                                      | SOME r => r :: mapPartial f xr;
  70.  
  71. fun find p []      = NONE
  72.   | find p (x::xr) = if p x then SOME x else find p xr;
  73.  
  74. fun filter p []      = []
  75.   | filter p (x::xr) = if p x then x :: filter p xr else filter p xr;
  76.  
  77. fun partition p xs =
  78.     let fun h []      are aren't = (rev are, rev aren't)
  79.       | h (x::xr) are aren't = if p x then h xr (x::are) aren't
  80.                           else h xr are      (x::aren't)
  81.     in h xs [] [] end;
  82.  
  83. fun foldr f e []      = e
  84.   | foldr f e (x::xr) = f(x, foldr f e xr);
  85.  
  86. fun foldl f e []      = e
  87.   | foldl f e (x::xr) = foldl f (f(x, e)) xr;
  88.  
  89. fun exists p []      = false
  90.   | exists p (x::xr) = p x orelse exists p xr;
  91.  
  92. fun all p []      = true
  93.   | all p (x::xr) = p x andalso all p xr;
  94.  
  95. fun tabulate (n, f) =
  96.     let fun h i = if i<n then f i :: h (i+1) else []
  97.     in if n<0 then raise Size else h 0 end;
  98.  
  99. fun getItem []        = NONE
  100.   | getItem (x :: xr) = SOME (x, xr)
  101.