[MLton-user] Extended Basis Library: iterating thunks

Geoffrey Alan Washburn geoffw at cis.upenn.edu
Mon Apr 16 16:09:26 PDT 2007


    A fairly straightforward extension.  It is perhaps debatable whether 
for non-unit thunks, iterate should return the result of the last call.

Index: detail/fn/thunk.sml
===================================================================
--- detail/fn/thunk.sml (revision 5526)
+++ detail/fn/thunk.sml (working copy)
@@ -7,4 +7,8 @@
 structure Thunk :> THUNK = struct
    open Thunk
    val mk = Fn.const
+
+   fun iterate thnk =
+       fn 0 => ()
+        | n => (thnk (); iterate thnk (n - 1)) 
 end
Index: public/fn/thunk.sig
===================================================================
--- public/fn/thunk.sig (revision 5526)
+++ public/fn/thunk.sig (working copy)
@@ -11,4 +11,7 @@
 
    val mk : 'a -> 'a t
    (** Constant thunk ({thunk x = let val x = x in fn () => x end}). *)
+
+   val iterate : 'a t -> Int.t Effect.t
+   (** {iterate thnk n == (thnk () (* 1 *); ... ; thnk () (* n *))} *)
 end

-- 
[Geoff Washburn|geoffw at cis.upenn.edu|http://www.cis.upenn.edu/~geoffw/]




More information about the MLton-user mailing list