101 val foldr1: ('a * 'a -> 'a) -> 'a list -> 'a |
101 val foldr1: ('a * 'a -> 'a) -> 'a list -> 'a |
102 val foldln: ('a * int -> 'b -> 'b) -> 'a list -> 'b -> 'b |
102 val foldln: ('a * int -> 'b -> 'b) -> 'a list -> 'b -> 'b |
103 val unflat: 'a list list -> 'b list -> 'b list list |
103 val unflat: 'a list list -> 'b list -> 'b list list |
104 val splitAt: int * 'a list -> 'a list * 'a list |
104 val splitAt: int * 'a list -> 'a list * 'a list |
105 val dropwhile: ('a -> bool) -> 'a list -> 'a list |
105 val dropwhile: ('a -> bool) -> 'a list -> 'a list |
106 val map_nth_elem: int -> ('a -> 'a) -> 'a list -> 'a list |
106 val nth: 'a list -> int -> 'a |
|
107 val nth_update: int * 'a -> 'a list -> 'a list |
|
108 val nth_map: int -> ('a -> 'a) -> 'a list -> 'a list |
107 val split_last: 'a list -> 'a list * 'a |
109 val split_last: 'a list -> 'a list * 'a |
108 val nth_update: 'a -> int * 'a list -> 'a list |
|
109 val find_index: ('a -> bool) -> 'a list -> int |
110 val find_index: ('a -> bool) -> 'a list -> int |
110 val find_index_eq: ''a -> ''a list -> int |
111 val find_index_eq: ''a -> ''a list -> int |
111 val find_first: ('a -> bool) -> 'a list -> 'a option |
112 val find_first: ('a -> bool) -> 'a list -> 'a option |
112 val get_first: ('a -> 'b option) -> 'a list -> 'b option |
113 val get_first: ('a -> 'b option) -> 'a list -> 'b option |
113 val separate: 'a -> 'a list -> 'a list |
114 val separate: 'a -> 'a list -> 'a list |
144 val read_radixint: int * string list -> int * string list |
145 val read_radixint: int * string list -> int * string list |
145 val read_int: string list -> int * string list |
146 val read_int: string list -> int * string list |
146 val oct_char: string -> string |
147 val oct_char: string -> string |
147 |
148 |
148 (*strings*) |
149 (*strings*) |
149 val nth_elem_string: int * string -> string |
150 val nth_string: string -> int -> string |
150 val fold_string: (string -> 'a -> 'a) -> string -> 'a -> 'a |
151 val fold_string: (string -> 'a -> 'a) -> string -> 'a -> 'a |
151 val exists_string: (string -> bool) -> string -> bool |
152 val exists_string: (string -> bool) -> string -> bool |
152 val forall_string: (string -> bool) -> string -> bool |
153 val forall_string: (string -> bool) -> string -> bool |
153 val enclose: string -> string -> string -> string |
154 val enclose: string -> string -> string -> string |
154 val unenclose: string -> string |
155 val unenclose: string -> string |
261 include BASIC_LIBRARY |
262 include BASIC_LIBRARY |
262 val foldl: ('a * 'b -> 'a) -> 'a * 'b list -> 'a |
263 val foldl: ('a * 'b -> 'a) -> 'a * 'b list -> 'a |
263 val foldr: ('a * 'b -> 'b) -> 'a list * 'b -> 'b |
264 val foldr: ('a * 'b -> 'b) -> 'a list * 'b -> 'b |
264 val take: int * 'a list -> 'a list |
265 val take: int * 'a list -> 'a list |
265 val drop: int * 'a list -> 'a list |
266 val drop: int * 'a list -> 'a list |
266 val nth_elem: int * 'a list -> 'a |
|
267 val last_elem: 'a list -> 'a |
267 val last_elem: 'a list -> 'a |
268 val flat: 'a list list -> 'a list |
268 val flat: 'a list list -> 'a list |
269 val seq: ('a -> unit) -> 'a list -> unit |
269 val seq: ('a -> unit) -> 'a list -> unit |
270 val partition: ('a -> bool) -> 'a list -> 'a list * 'a list |
270 val partition: ('a -> bool) -> 'a list -> 'a list * 'a list |
271 val mapfilter: ('a -> 'b option) -> 'a list -> 'b list |
271 val mapfilter: ('a -> 'b option) -> 'a list -> 'b list |
537 fun dropwhile P [] = [] |
537 fun dropwhile P [] = [] |
538 | dropwhile P (ys as x::xs) = if P x then dropwhile P xs else ys; |
538 | dropwhile P (ys as x::xs) = if P x then dropwhile P xs else ys; |
539 |
539 |
540 (*return nth element of a list, where 0 designates the first element; |
540 (*return nth element of a list, where 0 designates the first element; |
541 raise EXCEPTION if list too short*) |
541 raise EXCEPTION if list too short*) |
542 fun nth_elem (i,xs) = List.nth(xs,i); |
542 fun nth xs i = List.nth (xs, i); |
543 |
543 |
544 fun map_nth_elem 0 f (x :: xs) = f x :: xs |
544 (*update nth element*) |
545 | map_nth_elem n f (x :: xs) = x :: map_nth_elem (n - 1) f xs |
545 fun nth_update (n, x) xs = |
546 | map_nth_elem _ _ [] = raise Subscript; |
546 (case splitAt (n, xs) of |
|
547 (_, []) => raise Subscript |
|
548 | (prfx, _ :: sffx') => prfx @ (x :: sffx')) |
|
549 |
|
550 fun nth_map 0 f (x :: xs) = f x :: xs |
|
551 | nth_map n f (x :: xs) = x :: nth_map (n - 1) f xs |
|
552 | nth_map _ _ [] = raise Subscript; |
547 |
553 |
548 (*last element of a list*) |
554 (*last element of a list*) |
549 val last_elem = List.last; |
555 val last_elem = List.last; |
550 |
556 |
551 (*rear decomposition*) |
557 (*rear decomposition*) |
552 fun split_last [] = raise Empty |
558 fun split_last [] = raise Empty |
553 | split_last [x] = ([], x) |
559 | split_last [x] = ([], x) |
554 | split_last (x :: xs) = apfst (cons x) (split_last xs); |
560 | split_last (x :: xs) = apfst (cons x) (split_last xs); |
555 |
|
556 (*update nth element*) |
|
557 fun nth_update x n_xs = |
|
558 (case splitAt n_xs of |
|
559 (_,[]) => raise Subscript |
|
560 | (prfx, _ :: sffx') => prfx @ (x :: sffx')) |
|
561 |
561 |
562 (*find the position of an element in a list*) |
562 (*find the position of an element in a list*) |
563 fun find_index pred = |
563 fun find_index pred = |
564 let fun find _ [] = ~1 |
564 let fun find _ [] = ~1 |
565 | find n (x :: xs) = if pred x then n else find (n + 1) xs; |
565 | find n (x :: xs) = if pred x then n else find (n + 1) xs; |
760 |
760 |
761 (** strings **) |
761 (** strings **) |
762 |
762 |
763 (* functions tuned for strings, avoiding explode *) |
763 (* functions tuned for strings, avoiding explode *) |
764 |
764 |
765 fun nth_elem_string (i, str) = |
765 fun nth_string str i = |
766 (case try String.substring (str, i, 1) of |
766 (case try String.substring (str, i, 1) of |
767 SOME s => s |
767 SOME s => s |
768 | NONE => raise Subscript); |
768 | NONE => raise Subscript); |
769 |
769 |
770 fun fold_string f str x0 = |
770 fun fold_string f str x0 = |
1099 let |
1099 let |
1100 fun qsort [] = [] |
1100 fun qsort [] = [] |
1101 | qsort (xs as [_]) = xs |
1101 | qsort (xs as [_]) = xs |
1102 | qsort (xs as [x, y]) = if ord (x, y) = GREATER then [y, x] else xs |
1102 | qsort (xs as [x, y]) = if ord (x, y) = GREATER then [y, x] else xs |
1103 | qsort xs = |
1103 | qsort xs = |
1104 let val (lts, eqs, gts) = part (nth_elem (length xs div 2, xs)) xs |
1104 let val (lts, eqs, gts) = part (nth xs (length xs div 2)) xs |
1105 in qsort lts @ eqs @ qsort gts end |
1105 in qsort lts @ eqs @ qsort gts end |
1106 and part _ [] = ([], [], []) |
1106 and part _ [] = ([], [], []) |
1107 | part pivot (x :: xs) = add (ord (x, pivot)) x (part pivot xs) |
1107 | part pivot (x :: xs) = add (ord (x, pivot)) x (part pivot xs) |
1108 and add LESS x (lts, eqs, gts) = (x :: lts, eqs, gts) |
1108 and add LESS x (lts, eqs, gts) = (x :: lts, eqs, gts) |
1109 | add EQUAL x (lts, eqs, gts) = (lts, x :: eqs, gts) |
1109 | add EQUAL x (lts, eqs, gts) = (lts, x :: eqs, gts) |
1141 |
1141 |
1142 fun random_range l h = |
1142 fun random_range l h = |
1143 if h < l orelse l < 0 then raise RANDOM |
1143 if h < l orelse l < 0 then raise RANDOM |
1144 else l + Real.floor (rmod (random ()) (real (h - l + 1))); |
1144 else l + Real.floor (rmod (random ()) (real (h - l + 1))); |
1145 |
1145 |
1146 fun one_of xs = nth_elem (random_range 0 (length xs - 1), xs); |
1146 fun one_of xs = nth xs (random_range 0 (length xs - 1)); |
1147 |
1147 |
1148 fun frequency xs = |
1148 fun frequency xs = |
1149 let |
1149 let |
1150 val sum = foldl op + (0, map fst xs); |
1150 val sum = foldl op + (0, map fst xs); |
1151 fun pick n ((k: int, x) :: xs) = |
1151 fun pick n ((k: int, x) :: xs) = |