More List and ListPair utilities
authorpaulson
Thu Apr 03 10:33:33 1997 +0200 (1997-04-03)
changeset 28844f2a4c27f9b5
parent 2883 fd1c0b8e9b61
child 2885 8d229dc0cfe2
More List and ListPair utilities
src/Pure/basis.ML
     1.1 --- a/src/Pure/basis.ML	Thu Apr 03 10:32:34 1997 +0200
     1.2 +++ b/src/Pure/basis.ML	Thu Apr 03 10:33:33 1997 +0200
     1.3 @@ -75,6 +75,24 @@
     1.4      | mapPartial f (x::xs) = 
     1.5           (case f x of General.NONE   => mapPartial f xs
     1.6                      | General.SOME y => y :: mapPartial f xs);
     1.7 +
     1.8 +  fun find _ []        = General.NONE
     1.9 +    | find p (x :: xs) = if p x then General.SOME x else find p xs;
    1.10 +
    1.11 +
    1.12 +  (*copy the list preserving elements that satisfy the predicate*)
    1.13 +  fun filter p [] = []
    1.14 +    | filter p (x :: xs) = if p x then x :: filter p xs else filter p xs;
    1.15 +
    1.16 +  (*Partition list into elements that satisfy predicate and those that don't.
    1.17 +    Preserves order of elements in both lists.*)
    1.18 +  fun partition (p: 'a->bool) (ys: 'a list) : ('a list * 'a list) =
    1.19 +      let fun part ([], answer) = answer
    1.20 +	    | part (x::xs, (ys, ns)) = if p(x)
    1.21 +	      then  part (xs, (x::ys, ns))
    1.22 +	      else  part (xs, (ys, x::ns))
    1.23 +      in  part (rev ys, ([], []))  end;
    1.24 +
    1.25    end;
    1.26  
    1.27  
    1.28 @@ -91,14 +109,14 @@
    1.29    fun map f ([], [])      = []
    1.30      | map f (x::xs,y::ys) = f(x,y) :: map f (xs,ys);
    1.31  
    1.32 -  fun exists pred =
    1.33 +  fun exists p =
    1.34      let fun boolf ([], [])      = false
    1.35 -	  | boolf (x::xs,y::ys) = pred(x,y) orelse boolf (xs,ys)
    1.36 +	  | boolf (x::xs,y::ys) = p(x,y) orelse boolf (xs,ys)
    1.37      in boolf end;
    1.38  
    1.39 -  fun all pred =
    1.40 +  fun all p =
    1.41      let fun boolf ([], [])      = true
    1.42 -	  | boolf (x::xs,y::ys) = pred(x,y) andalso boolf (xs,ys)
    1.43 +	  | boolf (x::xs,y::ys) = p(x,y) andalso boolf (xs,ys)
    1.44      in boolf end;
    1.45    end;
    1.46