[MLton-commit] r6512
Vesa Karvonen
vesak at mlton.org
Fri Mar 28 05:56:45 PST 2008
Some examples.
----------------------------------------------------------------------
A mltonlib/trunk/com/ssh/extended-basis/unstable/example/
A mltonlib/trunk/com/ssh/extended-basis/unstable/example/iter/
A mltonlib/trunk/com/ssh/extended-basis/unstable/example/iter/eratosthenes.mlb
A mltonlib/trunk/com/ssh/extended-basis/unstable/example/iter/eratosthenes.sml
A mltonlib/trunk/com/ssh/extended-basis/unstable/example/iter/pythagoras.mlb
A mltonlib/trunk/com/ssh/extended-basis/unstable/example/iter/pythagoras.sml
----------------------------------------------------------------------
Added: mltonlib/trunk/com/ssh/extended-basis/unstable/example/iter/eratosthenes.mlb
===================================================================
--- mltonlib/trunk/com/ssh/extended-basis/unstable/example/iter/eratosthenes.mlb 2008-03-28 12:04:34 UTC (rev 6511)
+++ mltonlib/trunk/com/ssh/extended-basis/unstable/example/iter/eratosthenes.mlb 2008-03-28 13:56:44 UTC (rev 6512)
@@ -0,0 +1,9 @@
+(* 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.
+ *)
+
+$(MLTON_LIB)/com/ssh/extended-basis/unstable/basis.mlb
+
+eratosthenes.sml
Property changes on: mltonlib/trunk/com/ssh/extended-basis/unstable/example/iter/eratosthenes.mlb
___________________________________________________________________
Name: svn:eol-style
+ native
Added: mltonlib/trunk/com/ssh/extended-basis/unstable/example/iter/eratosthenes.sml
===================================================================
--- mltonlib/trunk/com/ssh/extended-basis/unstable/example/iter/eratosthenes.sml 2008-03-28 12:04:34 UTC (rev 6511)
+++ mltonlib/trunk/com/ssh/extended-basis/unstable/example/iter/eratosthenes.sml 2008-03-28 13:56:44 UTC (rev 6512)
@@ -0,0 +1,47 @@
+(* 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 basically an example from
+ *
+ * Eager Comprehensions in Scheme: The design of SRFI-42
+ * Sebastian Egner
+ * [http://library.readscheme.org/servlets/cite.ss?pattern=sw2005-Egner]
+ *
+ * translated to SML using iterator combinators.
+ *
+ * If you compare the SRFI-42 example from the above paper with the below
+ * translation, notice that the SRFI-42 version repeats the lines
+ *
+ *> (:range k 2 n)
+ *> (if (char=? (string-ref p k) #\1))
+ *
+ * twice, while the below version using iterator combinators defines the
+ * iterator
+ *
+ *> val primes = filter isPrime (upTo n From 2 $)
+ *
+ * and uses it twice. Also worth noting is that the below version returns
+ * an iterator rather than a list. Iterators and iterator combinators are
+ * first-class values, while SRFI-42 style eager comprehensions are
+ * second-class expressions.
+ *)
+
+open Cvt Iter
+
+fun eratosthenes n = let
+ val p = Array.array (n, true)
+ fun isPrime i = Array.sub (p, i)
+ val primes = filter isPrime (upTo n From 2 $)
+in
+ (primes >>= (fn k => upTo n From (2*k) By k $))
+ (fn i => Array.update (p, i, false))
+ ; primes
+end
+
+val n = valOf (Int.fromString (hd (CommandLine.arguments ())))
+
+val () = eratosthenes n (println o D)
Property changes on: mltonlib/trunk/com/ssh/extended-basis/unstable/example/iter/eratosthenes.sml
___________________________________________________________________
Name: svn:eol-style
+ native
Added: mltonlib/trunk/com/ssh/extended-basis/unstable/example/iter/pythagoras.mlb
===================================================================
--- mltonlib/trunk/com/ssh/extended-basis/unstable/example/iter/pythagoras.mlb 2008-03-28 12:04:34 UTC (rev 6511)
+++ mltonlib/trunk/com/ssh/extended-basis/unstable/example/iter/pythagoras.mlb 2008-03-28 13:56:44 UTC (rev 6512)
@@ -0,0 +1,9 @@
+(* 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.
+ *)
+
+$(MLTON_LIB)/com/ssh/extended-basis/unstable/basis.mlb
+
+pythagoras.sml
Property changes on: mltonlib/trunk/com/ssh/extended-basis/unstable/example/iter/pythagoras.mlb
___________________________________________________________________
Name: svn:eol-style
+ native
Added: mltonlib/trunk/com/ssh/extended-basis/unstable/example/iter/pythagoras.sml
===================================================================
--- mltonlib/trunk/com/ssh/extended-basis/unstable/example/iter/pythagoras.sml 2008-03-28 12:04:34 UTC (rev 6511)
+++ mltonlib/trunk/com/ssh/extended-basis/unstable/example/iter/pythagoras.sml 2008-03-28 13:56:44 UTC (rev 6512)
@@ -0,0 +1,43 @@
+(* 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 basically an example from
+ *
+ * Eager Comprehensions in Scheme: The design of SRFI-42
+ * Sebastian Egner
+ * [http://library.readscheme.org/servlets/cite.ss?pattern=sw2005-Egner]
+ *
+ * translated to SML using iterator combinators.
+ *
+ * One difference worth noting is that the below version returns an
+ * iterator rather than a list. Iterators and iterator combinators are
+ * first-class values, while SRFI-42 style eager comprehensions are
+ * second-class expressions.
+ *
+ * A less favorable observation is that the lack of custom notation for
+ * monads makes the translation somewhat noisier than the SRFI-42 version.
+ *)
+
+open Cvt Iter
+
+val sq = Int.sq
+val op >> = Monad.>>
+val guard = Monad.guard
+
+fun pythagoras n =
+ upTo (n+1) From 1 $ >>= (fn a =>
+ upTo (n+1) From a $ >>= (fn b =>
+ guard (sq a + sq b <= sq n) >>
+ upTo (n+1) From b $ >>= (fn c =>
+ guard (sq a + sq b = sq c) >>
+ return (a, b, c))))
+
+val n = valOf (Int.fromString (hd (CommandLine.arguments ())))
+
+val () =
+ (pythagoras n)
+ (fn (a, b, c) => printlns [D a, " ", D b, " ", D c])
Property changes on: mltonlib/trunk/com/ssh/extended-basis/unstable/example/iter/pythagoras.sml
___________________________________________________________________
Name: svn:eol-style
+ native
More information about the MLton-commit
mailing list