diff options
| author | Paul Buetow <git@mx.buetow.org> | 2021-05-20 08:31:19 +0100 |
|---|---|---|
| committer | Paul Buetow <git@mx.buetow.org> | 2021-05-20 08:31:19 +0100 |
| commit | 152643ce366c08e43268abd9a922329339dfce5a (patch) | |
| tree | 68816d986b391305a3d110b58b2526e851b83d90 | |
| parent | 6b8c4e61fa8d0ce6d24e8d930aec3aabfdb45b3b (diff) | |
Publishing new version
| -rw-r--r-- | gemfeed/2010-05-07-lazy-evaluation-with-standarn-ml.html | 186 | ||||
| -rw-r--r-- | gemfeed/atom.xml | 102 | ||||
| -rw-r--r-- | gemfeed/index.html | 1 | ||||
| -rw-r--r-- | index.html | 1 |
4 files changed, 289 insertions, 1 deletions
diff --git a/gemfeed/2010-05-07-lazy-evaluation-with-standarn-ml.html b/gemfeed/2010-05-07-lazy-evaluation-with-standarn-ml.html new file mode 100644 index 00000000..b81aa18b --- /dev/null +++ b/gemfeed/2010-05-07-lazy-evaluation-with-standarn-ml.html @@ -0,0 +1,186 @@ +<!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" /> +<style type="text/css"> +body { + margin: auto; + max-width: 900px; + background-color: #FFFFEF; + border: 1px dashed #880000; + border-radius: 8px; + padding: 5px; +} +img { + display:block; + max-width: 80%; +} +a.textlink:before { + content: " ⇒ "; + padding-left: 2px; +} +a.textlink { + text-decoration: none; + color: #FF0000; +} +a.textlink:hover { + text-decoration: underline; +} +i { + color: #48AAAD; +} +pre { + background-color: #F1F8E9; + border: 1px dashed #BB0000; + border-radius: 8px; + padding: 5px; + font-family: "Lucida Console", "Courier New", monospace; +} +h1 { + text-align: center; + color: #880000; +} +h2, h3 { + color: #BB0000; +} +</style> +</head> +<body> +<h1>Lazy Evaluation with Standard ML</h1> +<pre> + + _____|~~\_____ _____________ + _-~ \ | \ + _- | ) \ |__/ \ \ + _- ) | | | \ \ + _- | ) / |--| | | + __-_______________ /__/_______| |_________ +( |---- | | + `---------------'--\\\\ .`--' -Glyde- + `|||| +</pre> +<p class="quote"><i>Written by Paul Buetow 2010-05-07</i></p> +<p>In contrast to Haskell, Standard SML does not use lazy evaluation by default, but eager evaluation. </p> +<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 /> +<p>You can solve certain 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.</p> +<h2>Emulating lazy evaluation in SML</h2> +<p>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):</p> +<pre> +type ’a lazy = unit -> ’a; + +fun force (f:’a lazy) = f (); +fun delay x = (fn () => 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 () => filters p (force r)) + else + filters p (force r); + +fun nat_pairs () = + let + fun from_pair (x,0) = + CONS ((x,0), fn () => from_pair (0,x+1)) + | from_pair (up,dn) = + CONS ((up,dn), fn () => 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) => x > 0 andalso y > 0) (nat_pairs ()); + +(* Test +val test = first 10 (nat_pairs_not_null ()); +*) +</pre> +<a class="textlink" href="http://smlnj.org/">http://smlnj.org/</a><br /> +<h2>Real laziness with Haskell </h2> +<p>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 because of that. </p> +<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] -> x > 0 && y > 0) nat_pairs + +{- Test: +first 10 nat_pairs_not_null +-} +</pre> +<a class="textlink" href="http://www.haskell.org/">http://www.haskell.org/</a><br /> +<p>E-Mail me your thoughts at comments@mx.buetow.org!</p> +<a class="textlink" href="../">Go back to the main site</a><br /> +<!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" /> +<style type="text/css"> +body { + margin: auto; + max-width: 900px; + background-color: #FFFFEF; + border: 1px dashed #880000; + border-radius: 8px; + padding: 5px; +} +img { + display:block; + max-width: 80%; +} +a.textlink:before { + content: " ⇒ "; + padding-left: 2px; +} +a.textlink { + text-decoration: none; + color: #FF0000; +} +a.textlink:hover { + text-decoration: underline; +} +i { + color: #48AAAD; +} +pre { + background-color: #F1F8E9; + border: 1px dashed #BB0000; + border-radius: 8px; + padding: 5px; + font-family: "Lucida Console", "Courier New", monospace; +} +h1 { + text-align: center; + color: #880000; +} +h2, h3 { + color: #BB0000; +} +</style> +</head> +<body> diff --git a/gemfeed/atom.xml b/gemfeed/atom.xml index 8bad3304..8751b47a 100644 --- a/gemfeed/atom.xml +++ b/gemfeed/atom.xml @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> <feed xmlns="http://www.w3.org/2005/Atom"> - <updated>2021-05-19T22:03:14+01:00</updated> + <updated>2021-05-20T08:26:57+01:00</updated> <title>buetow.org feed</title> <subtitle>Having fun with computers!</subtitle> <link href="https://buetow.org/gemfeed/atom.xml" rel="self" /> @@ -2197,6 +2197,106 @@ BB </content> </entry> <entry> + <title>Lazy Evaluation with Standard ML</title> + <link href="https://buetow.org/gemfeed/2010-05-07-lazy-evaluation-with-standarn-ml.html" /> + <id>https://buetow.org/gemfeed/2010-05-07-lazy-evaluation-with-standarn-ml.html</id> + <updated>2010-05-07T08:17:59+01:00</updated> + <author> + <name>Paul Buetow</name> + <email>comments@mx.buetow.org</email> + </author> + <summary>In contrast to Haskell, Standard SML does not use lazy evaluation by default, but strict evaluation. . .....to read on please visit my site.</summary> + <content type="xhtml"> + <div xmlns="http://www.w3.org/1999/xhtml"> + <h1>Lazy Evaluation with Standard ML</h1> +<pre> + + _____|~~\_____ _____________ + _-~ \ | \ + _- | ) \ |__/ \ \ + _- ) | | | \ \ + _- | ) / |--| | | + __-_______________ /__/_______| |_________ +( |---- | | + `---------------'--\\\\ .`--' -Glyde- + `|||| +</pre> +<p class="quote"><i>Written by Paul Buetow 2010-05-07</i></p> +<p>In contrast to Haskell, Standard SML does not use lazy evaluation by default, but eager evaluation. </p> +<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 /> +<p>You can solve certain 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.</p> +<h2>Emulating lazy evaluation in SML</h2> +<p>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):</p> +<pre> +type ’a lazy = unit -> ’a; + +fun force (f:’a lazy) = f (); +fun delay x = (fn () => 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 () => filters p (force r)) + else + filters p (force r); + +fun nat_pairs () = + let + fun from_pair (x,0) = + CONS ((x,0), fn () => from_pair (0,x+1)) + | from_pair (up,dn) = + CONS ((up,dn), fn () => 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) => x > 0 andalso y > 0) (nat_pairs ()); + +(* Test +val test = first 10 (nat_pairs_not_null ()); +*) +</pre> +<a class="textlink" href="http://smlnj.org/">http://smlnj.org/</a><br /> +<h2>Real laziness with Haskell </h2> +<p>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 because of that. </p> +<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] -> x > 0 && y > 0) nat_pairs + +{- Test: +first 10 nat_pairs_not_null +-} +</pre> +<a class="textlink" href="http://www.haskell.org/">http://www.haskell.org/</a><br /> +<p>E-Mail me your thoughts at comments@mx.buetow.org!</p> + </div> + </content> + </entry> + <entry> <title>Standard ML and Haskell</title> <link href="https://buetow.org/gemfeed/2010-04-09-standard-ml-and-haskell.html" /> <id>https://buetow.org/gemfeed/2010-04-09-standard-ml-and-haskell.html</id> diff --git a/gemfeed/index.html b/gemfeed/index.html index c68a757a..5952cdd3 100644 --- a/gemfeed/index.html +++ b/gemfeed/index.html @@ -63,6 +63,7 @@ h2, h3 { <a class="textlink" href="./2014-03-24-the-fibonacci.pl.c-polyglot.html">2014-03-24 - The fibonacci.pl.c Polyglot</a><br /> <a class="textlink" href="./2011-05-07-perl-daemon-service-framework.html">2011-05-07 - Perl Daemon (Service Framework)</a><br /> <a class="textlink" href="./2010-05-09-the-fype-programming-language.html">2010-05-09 - The Fype Programming Language</a><br /> +<a class="textlink" href="./2010-05-07-lazy-evaluation-with-standarn-ml.html">2010-05-07 - Lazy Evaluation with Standard ML</a><br /> <a class="textlink" href="./2010-04-09-standard-ml-and-haskell.html">2010-04-09 - Standard ML and Haskell</a><br /> <a class="textlink" href="./2008-06-26-perl-poetry.html">2008-06-26 - Perl Poetry</a><br /> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> @@ -100,6 +100,7 @@ h2, h3 { <a class="textlink" href="./gemfeed/2014-03-24-the-fibonacci.pl.c-polyglot.html">2014-03-24 - The fibonacci.pl.c Polyglot</a><br /> <a class="textlink" href="./gemfeed/2011-05-07-perl-daemon-service-framework.html">2011-05-07 - Perl Daemon (Service Framework)</a><br /> <a class="textlink" href="./gemfeed/2010-05-09-the-fype-programming-language.html">2010-05-09 - The Fype Programming Language</a><br /> +<a class="textlink" href="./gemfeed/2010-05-07-lazy-evaluation-with-standarn-ml.html">2010-05-07 - Lazy Evaluation with Standard ML</a><br /> <a class="textlink" href="./gemfeed/2010-04-09-standard-ml-and-haskell.html">2010-04-09 - Standard ML and Haskell</a><br /> <a class="textlink" href="./gemfeed/2008-06-26-perl-poetry.html">2008-06-26 - Perl Poetry</a><br /> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
