summaryrefslogtreecommitdiff
path: root/gemfeed/2010-05-07-lazy-evaluation-with-standard-ml.html
diff options
context:
space:
mode:
Diffstat (limited to 'gemfeed/2010-05-07-lazy-evaluation-with-standard-ml.html')
-rw-r--r--gemfeed/2010-05-07-lazy-evaluation-with-standard-ml.html123
1 files changed, 123 insertions, 0 deletions
diff --git a/gemfeed/2010-05-07-lazy-evaluation-with-standard-ml.html b/gemfeed/2010-05-07-lazy-evaluation-with-standard-ml.html
new file mode 100644
index 00000000..641d8c0c
--- /dev/null
+++ b/gemfeed/2010-05-07-lazy-evaluation-with-standard-ml.html
@@ -0,0 +1,123 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+<title>Lazy Evaluation with Standard ML</title>
+<link rel="shortcut icon" type="image/gif" href="/favicon.ico" />
+<link rel="stylesheet" href="../style.css" />
+<link rel="stylesheet" href="style-override.css" />
+</head>
+<body>
+<p class="header">
+<a href="https://foo.zone">Home</a> | <a href="https://codeberg.org/snonux/foo.zone/src/branch/content-md/gemfeed/2010-05-07-lazy-evaluation-with-standard-ml.md">Markdown</a> | <a href="gemini://foo.zone/gemfeed/2010-05-07-lazy-evaluation-with-standard-ml.gmi">Gemini</a>
+</p>
+<h1 style='display: inline' id='lazy-evaluation-with-standard-ml'>Lazy Evaluation with Standard ML</h1><br />
+<br />
+<span class='quote'>Published at 2010-05-07T08:17:59+01:00</span><br />
+<br />
+<pre>
+ _____|~~\_____ _____________
+ _-~ \ | \
+ _- | ) \ |__/ \ \
+ _- ) | | | \ \
+ _- | ) / |--| | |
+ __-_______________ /__/_______| |_________
+( |---- | |
+ `---------------&#39;--\\\\ .`--&#39; -Glyde-
+ `||||
+</pre>
+<br />
+<span>In contrast to Haskell, Standard SML does not use lazy evaluation by default but an eager evaluation. </span><br />
+<br />
+<a class='textlink' href='https://en.wikipedia.org/wiki/Eager_evaluation'>https://en.wikipedia.org/wiki/Eager_evaluation</a><br />
+<a class='textlink' href='https://en.wikipedia.org/wiki/Lazy_evaluation'>https://en.wikipedia.org/wiki/Lazy_evaluation</a><br />
+<br />
+<br />
+<span>You can solve specific problems with lazy evaluation easier than with eager evaluation. For example, you might want to list the number Pi or another infinite list of something. With the help of lazy evaluation, each element of the list is calculated when it is accessed first, but not earlier.</span><br />
+<br />
+<h2 style='display: inline' id='emulating-lazy-evaluation-in-sml'>Emulating lazy evaluation in SML</h2><br />
+<br />
+<span>However, it is possible to emulate lazy evaluation in most eager evaluation languages. This is how it is done with Standard ML (with some play with an infinite list of natural number tuples filtering out 0 elements):</span><br />
+<br />
+<pre>
+type ’a lazy = unit -&gt; ’a;
+
+fun force (f:’a lazy) = f ();
+fun delay x = (fn () =&gt; x) : ’a lazy;
+
+datatype ’a sequ = NIL | CONS of ’a * ’a sequ lazy;
+
+fun first 0 s = []
+ | first n NIL = []
+ | first n (CONS (i,r)) = i :: first (n-1) (force r);
+
+fun filters p NIL = NIL
+ | filters p (CONS (x,r)) =
+ if p x
+ then CONS (x, fn () =&gt; filters p (force r))
+ else
+ filters p (force r);
+
+fun nat_pairs () =
+ let
+ fun from_pair (x,0) =
+ CONS ((x,0), fn () =&gt; from_pair (0,x+1))
+ | from_pair (up,dn) =
+ CONS ((up,dn), fn () =&gt; from_pair (up+1,dn-1))
+ in from_pair (0,0)
+ end;
+
+(* Test
+val test = first 10 (nat_pairs ())
+*)
+
+fun nat_pairs_not_null () =
+ filters (fn (x,y) =&gt; x &gt; 0 andalso y &gt; 0) (nat_pairs ());
+
+(* Test
+val test = first 10 (nat_pairs_not_null ());
+*)
+</pre>
+<br />
+<a class='textlink' href='http://smlnj.org/'>http://smlnj.org/</a><br />
+<br />
+<h2 style='display: inline' id='real-laziness-with-haskell-'>Real laziness with Haskell </h2><br />
+<br />
+<span>As Haskell already uses lazy evaluation by default, there is no need to construct a new data type. Lists in Haskell are lazy by default. You will notice that the code is also much shorter and easier to understand than the SML version. </span><br />
+<br />
+<pre>
+{- Just to make it look like the ML example -}
+first = take
+filters = filter
+
+{- Implementation -}
+nat_pairs = from_pair 0 0
+ where
+ from_pair x 0 = [x,0] : from_pair 0 (x+1)
+ from_pair up dn = [up,dn] : from_pair (up+1) (dn-1)
+
+{- Test:
+first 10 nat_pairs
+-}
+
+nat_pairs_not_null = filters (\[x,y] -&gt; x &gt; 0 &amp;&amp; y &gt; 0) nat_pairs
+
+{- Test:
+first 10 nat_pairs_not_null
+-}
+</pre>
+<br />
+<a class='textlink' href='http://www.haskell.org/'>http://www.haskell.org/</a><br />
+<br />
+<span>E-Mail your comments to <span class='inlinecode'>paul@nospam.buetow.org</span> :-)</span><br />
+<br />
+<a class='textlink' href='../'>Back to the main site</a><br />
+<p class="footer">
+ Generated with <a href="https://codeberg.org/snonux/gemtexter">Gemtexter 3.0.1-develop</a> |
+ served by <a href="https://www.OpenBSD.org">OpenBSD</a>/<a href="https://man.openbsd.org/relayd.8">relayd(8)</a>+<a href="https://man.openbsd.org/httpd.8">httpd(8)</a> |
+ <a href="https://foo.zone/site-mirrors.html">Site Mirrors</a>
+ <br />
+ Webring: <a href="https://shring.sh/foo.zone/previous">previous</a> | <a href="https://shring.sh">shring</a> | <a href="https://shring.sh/foo.zone/next">next</a>
+</p>
+</body>
+</html>