structure hml :> HML = struct structure L = List structure B = Word8Vector structure BS = Word8VectorSlice fun fst (x, _) = x fun snd (_, x) = x fun id x = x fun foldl1 _ [] = raise Fail "foldl1: empty list" | foldl1 f (x::xs) = L.foldl f x xs fun zip_with _ [] _ = [] | zip_with _ _ [] = [] | zip_with f (x::xs) (y::ys) = f x y :: zip_with f xs ys fun zip x y = let fun f p q = (p, q) in zip_with f x y end fun replicate 0 x = nil | replicate n x = x :: replicate (n - 1) x fun intersperse _ [] = [] | intersperse _ [x] = [x] | intersperse n (x::xs) = x :: n :: intersperse n xs fun map_accum_l _ s [] = (s, []) | map_accum_l f s (x::xs) = let val (s1, y) = f s x val (s2, ys) = map_accum_l f s1 xs in (s2, y :: ys) end fun maximum_by f xs = let fun g (p, q) = if f (p, q) = LESS then q else p in foldl1 g xs end fun transpose l = let fun f [] = NONE | f (x::_) = SOME x fun g [] = [] | g (_::xs) = xs fun h [] = [] | h (NONE::xs) = h xs | h (SOME x::xs) = x :: h xs in if L.null l then [] else if L.all L.null l then [] else h (L.map f l) :: transpose (L.map g l) end fun b_unpack v = B.foldr (fn (x, xs) => x :: xs) nil v fun b_take n v = BS.vector (BS.slice (v, 0, SOME n)) fun b_drop n v = BS.vector (BS.slice (v, n, NONE)) fun b_split_at n v = (b_take n v, b_drop n v) end