[MLton-commit] r6620
Vesa Karvonen
vesak at mlton.org
Wed May 14 09:22:41 PDT 2008
Another example.
----------------------------------------------------------------------
A mltonlib/trunk/com/ssh/extended-basis/unstable/example/iter/mean.mlb
A mltonlib/trunk/com/ssh/extended-basis/unstable/example/iter/mean.sml
----------------------------------------------------------------------
Added: mltonlib/trunk/com/ssh/extended-basis/unstable/example/iter/mean.mlb
===================================================================
--- mltonlib/trunk/com/ssh/extended-basis/unstable/example/iter/mean.mlb 2008-05-10 11:57:30 UTC (rev 6619)
+++ mltonlib/trunk/com/ssh/extended-basis/unstable/example/iter/mean.mlb 2008-05-14 16:22:08 UTC (rev 6620)
@@ -0,0 +1,17 @@
+(* Copyright (C) 2008 Vesa Karvonen
+ *
+ * This code is released under the MLton license, a BSD-style license.
+ * See the LICENSE file or http://mlton.org/License for details.
+ *)
+
+../../basis.mlb
+
+local
+ ann
+ "warnUnused true"
+ "sequenceNonUnit warn"
+ in
+ mean.sml
+ end
+in
+end
Property changes on: mltonlib/trunk/com/ssh/extended-basis/unstable/example/iter/mean.mlb
___________________________________________________________________
Name: svn:eol-style
+ native
Added: mltonlib/trunk/com/ssh/extended-basis/unstable/example/iter/mean.sml
===================================================================
--- mltonlib/trunk/com/ssh/extended-basis/unstable/example/iter/mean.sml 2008-05-10 11:57:30 UTC (rev 6619)
+++ mltonlib/trunk/com/ssh/extended-basis/unstable/example/iter/mean.sml 2008-05-14 16:22:08 UTC (rev 6620)
@@ -0,0 +1,28 @@
+(* Copyright (C) 2008 Vesa Karvonen
+ *
+ * This code is released under the MLton license, a BSD-style license.
+ * See the LICENSE file or http://mlton.org/License for details.
+ *)
+
+(**
+ * This is an example from
+ *
+ * http://article.gmane.org/gmane.comp.lang.haskell.cafe/39806
+ *
+ * translated to SML using iterator combinators.
+ *
+ * The mean computation runs in constant space. The sequence of real
+ * numbers is generated twice, first to compute the sum and then to
+ * compute the length. Of course, one could also compute the sum and
+ * length simultaneously and only generated the sequence once.
+ *)
+
+open Cvt Iter
+
+fun length xs = reduce 0 op + (const 1) xs
+val sum = reduce 0.0 op + id
+fun mean xs = sum xs / real (length xs)
+
+val n = valOf (Real.fromString (hd (CommandLine.arguments ()))) handle _ => 1e9
+
+val () = println (G (mean (realsTo n From 1.0 $)))
Property changes on: mltonlib/trunk/com/ssh/extended-basis/unstable/example/iter/mean.sml
___________________________________________________________________
Name: svn:eol-style
+ native
More information about the MLton-commit
mailing list