src/Pure/basis.ML
changeset 2265 3123fef88dce
parent 2230 275a5a699ff7
child 2402 b3d273ce5601
     1.1 --- a/src/Pure/basis.ML	Thu Nov 28 10:41:14 1996 +0100
     1.2 +++ b/src/Pure/basis.ML	Thu Nov 28 10:42:19 1996 +0100
     1.3 @@ -10,6 +10,8 @@
     1.4  Full compatibility cannot be obtained using a file: what about char constants?
     1.5  *)
     1.6  
     1.7 +exception Subscript;
     1.8 +
     1.9  structure Bool =
    1.10    struct
    1.11    fun toString true  = "true"
    1.12 @@ -23,6 +25,62 @@
    1.13    fun min (x, y) = if x < y then x else y : int;
    1.14    end;
    1.15  
    1.16 +
    1.17 +structure List =
    1.18 +  struct
    1.19 +  exception Empty
    1.20 +
    1.21 +  fun last []      = raise Empty
    1.22 +    | last [x]     = x
    1.23 +    | last (x::xs) = last xs;
    1.24 +
    1.25 +  fun nth (xs, n) =
    1.26 +      let fun h []      _ = raise Subscript
    1.27 +	    | h (x::xs) n = if n=0 then x else h xs (n-1)
    1.28 +      in if n<0 then raise Subscript else h xs n end;
    1.29 +
    1.30 +  fun drop (xs, n) =
    1.31 +      let fun h xs      0 = xs
    1.32 +	    | h []      n = raise Subscript
    1.33 +	    | h (x::xs) n = h xs (n-1)
    1.34 +      in if n<0 then raise Subscript else h xs n end;
    1.35 +
    1.36 +  fun take (xs, n) =
    1.37 +      let fun h xs      0 = []
    1.38 +	    | h []      n = raise Subscript
    1.39 +	    | h (x::xs) n = x :: h xs (n-1)
    1.40 +      in if n<0 then raise Subscript else h xs n end;
    1.41 +
    1.42 +  fun concat []      = []
    1.43 +    | concat (l::ls) = l @ concat ls;
    1.44 +  end;
    1.45 +
    1.46 +
    1.47 +structure ListPair =
    1.48 +  struct
    1.49 +  fun zip ([], [])      = []
    1.50 +    | zip (x::xs,y::ys) = (x,y) :: zip(xs,ys);
    1.51 +
    1.52 +  fun unzip [] = ([],[])
    1.53 +    | unzip((x,y)::pairs) =
    1.54 +	  let val (xs,ys) = unzip pairs
    1.55 +	  in  (x::xs, y::ys)  end;
    1.56 +
    1.57 +  fun map f ([], [])      = []
    1.58 +    | map f (x::xs,y::ys) = f(x,y) :: map f (xs,ys);
    1.59 +
    1.60 +  fun exists pred =
    1.61 +    let fun boolf ([], [])      = false
    1.62 +	  | boolf (x::xs,y::ys) = pred(x,y) orelse boolf (xs,ys)
    1.63 +    in boolf end;
    1.64 +
    1.65 +  fun all pred =
    1.66 +    let fun boolf ([], [])      = true
    1.67 +	  | boolf (x::xs,y::ys) = pred(x,y) andalso boolf (xs,ys)
    1.68 +    in boolf end;
    1.69 +  end;
    1.70 +
    1.71 +
    1.72  structure TextIO =
    1.73    struct
    1.74    type instream = instream