1(* ========================================================================= *) 2(* A POSSIBLY-INFINITE STREAM DATATYPE FOR ML *) 3(* Copyright (c) 2001-2004 Joe Hurd, distributed under the GNU GPL version 2 *) 4(* ========================================================================= *) 5 6signature Stream = 7sig 8 9(* ------------------------------------------------------------------------- *) 10(* The stream type *) 11(* ------------------------------------------------------------------------- *) 12 13datatype 'a stream = NIL | CONS of 'a * (unit -> 'a stream) 14 15(* If you're wondering how to create an infinite stream: *) 16(* val stream4 = let fun s4 () = Stream.CONS (4,s4) in s4 () end; *) 17 18(* ------------------------------------------------------------------------- *) 19(* Stream constructors *) 20(* ------------------------------------------------------------------------- *) 21 22val repeat : 'a -> 'a stream 23 24val count : int -> int stream 25 26val powers : ('a -> 'a) -> 'a -> 'a stream 27 28(* ------------------------------------------------------------------------- *) 29(* Stream versions of standard list operations: these should all terminate *) 30(* ------------------------------------------------------------------------- *) 31 32val cons : 'a -> (unit -> 'a stream) -> 'a stream 33 34val null : 'a stream -> bool 35 36val hd : 'a stream -> 'a (* raises Empty *) 37 38val tl : 'a stream -> 'a stream (* raises Empty *) 39 40val hd_tl : 'a stream -> 'a * 'a stream (* raises Empty *) 41 42val sing : 'a -> 'a stream 43 44val append : 'a stream -> (unit -> 'a stream) -> 'a stream 45 46val map : ('a -> 'b) -> 'a stream -> 'b stream 47 48val maps : ('a -> 's -> 'b * 's) -> 's -> 'a stream -> 'b stream 49 50val zipwith : ('a -> 'b -> 'c) -> 'a stream -> 'b stream -> 'c stream 51 52val zip : 'a stream -> 'b stream -> ('a * 'b) stream 53 54val take : int -> 'a stream -> 'a stream (* raises Subscript *) 55 56val drop : int -> 'a stream -> 'a stream (* raises Subscript *) 57 58(* ------------------------------------------------------------------------- *) 59(* Stream versions of standard list operations: these might not terminate *) 60(* ------------------------------------------------------------------------- *) 61 62val length : 'a stream -> int 63 64val exists : ('a -> bool) -> 'a stream -> bool 65 66val all : ('a -> bool) -> 'a stream -> bool 67 68val filter : ('a -> bool) -> 'a stream -> 'a stream 69 70val foldl : ('a * 's -> 's) -> 's -> 'a stream -> 's 71 72val flatten : 'a stream stream -> 'a stream 73 74val partial_map : ('a -> 'b option) -> 'a stream -> 'b stream 75 76val partial_maps : ('a -> 's -> 'b option * 's) -> 's -> 'a stream -> 'b stream 77 78(* ------------------------------------------------------------------------- *) 79(* Stream operations *) 80(* ------------------------------------------------------------------------- *) 81 82val memoize : 'a stream -> 'a stream 83 84val to_list : 'a stream -> 'a list 85 86val from_list : 'a list -> 'a stream 87 88val to_textfile : {filename : string} -> string stream -> unit 89 90val from_textfile : {filename : string} -> string stream (* line by line *) 91 92end 93