default.txt 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. (* list.sml
  2. *
  3. * COPYRIGHT (c) 2009 The Fellowship of SML/NJ (http://www.smlnj.org)
  4. * All rights reserved.
  5. *
  6. * Available (unqualified) at top level:
  7. * type list
  8. * val nil, ::, hd, tl, null, length, @, app, map, foldr, foldl, rev
  9. * exception Empty
  10. *
  11. * Consequently the following are not visible at top level:
  12. * val last, nth, take, drop, concat, revAppend, mapPartial, find, filter,
  13. * partition, exists, all, tabulate
  14. *
  15. * The following infix declarations will hold at top level:
  16. * infixr 5 :: @
  17. *
  18. *)
  19. structure List : LIST =
  20. struct
  21. val op + = InlineT.DfltInt.+
  22. val op - = InlineT.DfltInt.-
  23. val op < = InlineT.DfltInt.<
  24. val op <= = InlineT.DfltInt.<=
  25. val op > = InlineT.DfltInt.>
  26. val op >= = InlineT.DfltInt.>=
  27. (* val op = = InlineT.= *)
  28. datatype list = datatype list
  29. exception Empty = Empty
  30. (* these functions are implemented in base/system/smlnj/init/pervasive.sml *)
  31. val null = null
  32. val hd = hd
  33. val tl = tl
  34. val length = length
  35. val rev = rev
  36. val revAppend = revAppend
  37. val op @ = op @
  38. val foldr = foldr
  39. val foldl = foldl
  40. val app = app
  41. val map = map
  42. fun last [] = raise Empty
  43. | last [x] = x
  44. | last (_::r) = last r
  45. fun getItem [] = NONE
  46. | getItem (x::r) = SOME(x, r)
  47. fun nth (l,n) = let
  48. fun loop ((e::_),0) = e
  49. | loop ([],_) = raise Subscript
  50. | loop ((_::t),n) = loop(t,n-1)
  51. in
  52. if n >= 0 then loop (l,n) else raise Subscript
  53. end
  54. fun take (l, n) = let
  55. fun loop (l, 0) = []
  56. | loop ([], _) = raise Subscript
  57. | loop ((x::t), n) = x :: loop (t, n-1)
  58. in
  59. if n >= 0 then loop (l, n) else raise Subscript
  60. end
  61. fun drop (l, n) = let
  62. fun loop (l,0) = l
  63. | loop ([],_) = raise Subscript
  64. | loop ((_::t),n) = loop(t,n-1)
  65. in
  66. if n >= 0 then loop (l,n) else raise Subscript
  67. end
  68. fun concat [] = []
  69. | concat (l::r) = l @ concat r
  70. fun mapPartial pred l = let
  71. fun mapp ([], l) = rev l
  72. | mapp (x::r, l) = (case (pred x)
  73. of SOME y => mapp(r, y::l)
  74. | NONE => mapp(r, l)
  75. (* end case *))
  76. in
  77. mapp (l, [])
  78. end
  79. fun find pred [] = NONE
  80. | find pred (a::rest) = if pred a then SOME a else (find pred rest)
  81. fun filter pred [] = []
  82. | filter pred (a::rest) = if pred a then a::(filter pred rest)
  83. else (filter pred rest)
  84. fun partition pred l = let
  85. fun loop ([],trueList,falseList) = (rev trueList, rev falseList)
  86. | loop (h::t,trueList,falseList) =
  87. if pred h then loop(t, h::trueList, falseList)
  88. else loop(t, trueList, h::falseList)
  89. in loop (l,[],[]) end
  90. fun exists pred = let
  91. fun f [] = false
  92. | f (h::t) = pred h orelse f t
  93. in f end
  94. fun all pred = let
  95. fun f [] = true
  96. | f (h::t) = pred h andalso f t
  97. in f end
  98. fun tabulate (len, genfn) =
  99. if len < 0 then raise Size
  100. else let
  101. fun loop n = if n = len then []
  102. else (genfn n)::(loop(n+1))
  103. in loop 0 end
  104. fun collate compare = let
  105. fun loop ([], []) = EQUAL
  106. | loop ([], _) = LESS
  107. | loop (_, []) = GREATER
  108. | loop (x :: xs, y :: ys) =
  109. (case compare (x, y) of
  110. EQUAL => loop (xs, ys)
  111. | unequal => unequal)
  112. in
  113. loop
  114. end
  115. end (* structure List *)