[pure-lang-svn] SF.net SVN: pure-lang:[694] pure/trunk/examples/hello.pure
Status: Beta
Brought to you by:
agraef
|
From: <ag...@us...> - 2008-09-04 00:45:21
|
Revision: 694
http://pure-lang.svn.sourceforge.net/pure-lang/?rev=694&view=rev
Author: agraef
Date: 2008-09-04 00:45:30 +0000 (Thu, 04 Sep 2008)
Log Message:
-----------
Add stream example.
Modified Paths:
--------------
pure/trunk/examples/hello.pure
Modified: pure/trunk/examples/hello.pure
===================================================================
--- pure/trunk/examples/hello.pure 2008-09-03 22:02:24 UTC (rev 693)
+++ pure/trunk/examples/hello.pure 2008-09-04 00:45:30 UTC (rev 694)
@@ -166,6 +166,26 @@
primes 100;
+/* Using streams (lazy lists), we can also compute the infinite list of *all*
+ prime numbers. */
+
+all_primes = sieve (2..inf) with
+ sieve [] = [];
+ sieve (p:qs) = p : sieve [q; q = qs; q mod p] &;
+end;
+
+// Assign this to a variable, so we can take advantage of memoization.
+let P = all_primes;
+
+// The primes <=100, this is the same as primes 100.
+let P1 = list $ takewhile (\x->x<=100) P; P1;
+
+// The first 30 primes.
+let P2 = list $ take 30 P; P2;
+
+// The 299th prime.
+P!299;
+
/* The classical n queens problem: Compute all placements of n queens on an
n x n board so that no two queens hold each other in check. This algorithm
demonstrates how you can use list comprehensions to organize backtracking
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|