diff options
| author | Paul Buetow <paul@buetow.org> | 2021-05-14 23:00:17 +0100 |
|---|---|---|
| committer | Paul Buetow <git@mx.buetow.org> | 2021-05-21 05:11:05 +0100 |
| commit | cb817ae72e5dd72098ebf86763690368936c607c (patch) | |
| tree | 1baa54d2382a3bfd3502f37fae7348c3558b0f85 | |
| parent | 75f2dc159283286a145c3076a6ca829db3e6af3c (diff) | |
add old haskell and sml post
| -rw-r--r-- | content/gemtext/gemfeed/2010-04-09-standard-ml-and-haskell.gmi | 174 | ||||
| -rw-r--r-- | content/gemtext/gemfeed/atom.xml | 156 | ||||
| -rw-r--r-- | content/gemtext/gemfeed/index.gmi | 1 | ||||
| -rw-r--r-- | content/gemtext/index.gmi | 1 | ||||
| -rw-r--r-- | content/html/gemfeed/2010-04-09-standard-ml-and-haskell.html | 193 | ||||
| -rw-r--r-- | content/html/gemfeed/atom.xml | 156 | ||||
| -rw-r--r-- | content/html/gemfeed/index.html | 1 | ||||
| -rw-r--r-- | content/html/index.html | 1 | ||||
| -rw-r--r-- | content/md/gemfeed/2010-04-09-standard-ml-and-haskell.md | 174 | ||||
| -rw-r--r-- | content/md/gemfeed/index.md | 1 | ||||
| -rw-r--r-- | content/md/index.md | 1 | ||||
| -rw-r--r-- | content/meta/gemfeed/2010-04-09-standard-ml-and-haskell.meta | 5 |
12 files changed, 862 insertions, 2 deletions
diff --git a/content/gemtext/gemfeed/2010-04-09-standard-ml-and-haskell.gmi b/content/gemtext/gemfeed/2010-04-09-standard-ml-and-haskell.gmi new file mode 100644 index 00000000..3d1411f7 --- /dev/null +++ b/content/gemtext/gemfeed/2010-04-09-standard-ml-and-haskell.gmi @@ -0,0 +1,174 @@ +# Standard ML and Haskell + +> Written by Paul Buetow 2010-04-09 + +I am currently looking into the functional programming language Standard ML (aka SML). The purpose is to refresh my functional programming skills and to learn something new too. Since I already know a little Haskell, could I do not help myself and I implemented the same exercises in Haskell too. + +As you will see, SML and Haskell are very similar (at least when it comes to the basics). However, the syntax of Haskell is little bit more "advanced". Haskell utilises fewer keywords (e.g. no val, end, fun, fn ...). Haskell also allows to explicitly write down the function types. What I have been missing in SML so far is the so-called pattern guards. Although this is a very superficial comparison for now, so far I like Haskell more than SML. Nevertheless, I thought it would be fun to demonstrate a few simple functions of both languages to show off the similarities. + +Haskell is also a "pure functional" programming language, whereas SML also makes explicit use of imperative concepts. I am by far not a specialist in either of these languages but here are a few functions implemented in both, SML and Haskell: + +## Defining a multi data type + +Standard ML: + +``` +datatype ’a multi + = EMPTY + | ELEM of ’a + | UNION of ’a multi * ’a multi +``` + +Haskell: + +``` +data (Eq a) => Multi a + = Empty + | Elem a + | Union (Multi a) (Multi a) + deriving Show +``` + +## Processing a multi + +Standard ML: + +``` +fun number (EMPTY) _ = 0 + | number (ELEM x) w = if x = w then 1 else 0 + | number (UNION (x,y)) w = (number x w) + (number y w) +fun test_number w = number (UNION (EMPTY, \ + UNION (ELEM 4, UNION (ELEM 6, \ + UNION (UNION (ELEM 4, ELEM 4), EMPTY))))) w +``` + +Haskell: + +``` +number Empty _ = 0 +number (Elem x) w = if x == w then 1 else 0 +test_number w = number (Union Empty \ + (Union (Elem 4) (Union (Elem 6) \ + (Union (Union (Elem 4) (Elem 4)) Empty)))) w +``` + +## Simplify function + +Standard ML: + +``` +fun simplify (UNION (x,y)) = + let fun is_empty (EMPTY) = true | is_empty _ = false + val x’ = simplify x + val y’ = simplify y + in if (is_empty x’) andalso (is_empty y’) + then EMPTY + else if (is_empty x’) + then y’ + else if (is_empty y’) + then x’ + else UNION (x’, y’) + end + | simplify x = x +``` + +Haskell: + +``` +simplify (Union x y) + | (isEmpty x’) && (isEmpty y’) = Empty + | isEmpty x’ = y’ + | isEmpty y’ = x’ + | otherwise = Union x’ y’ + where + isEmpty Empty = True + isEmpty _ = False + x’ = simplify x + y’ = simplify y +simplify x = x +``` + +## Delete all + +Standard ML: + +``` +fun delete_all m w = + let fun delete_all’ (ELEM x) = if x = w then EMPTY else ELEM x + | delete_all’ (UNION (x,y)) = UNION (delete_all’ x, delete_all’ y) + | delete_all’ x = x + in simplify (delete_all’ m) + end +``` + +Haskell: + +``` +delete_all m w = simplify (delete_all’ m) + where + delete_all’ (Elem x) = if x == w then Empty else Elem x + delete_all’ (Union x y) = Union (delete_all’ x) (delete_all’ y) + delete_all’ x = x +``` + +## Delete one + +Standard ML: + +``` +fun delete_one m w = + let fun delete_one’ (UNION (x,y)) = + let val (x’, deleted) = delete_one’ x + in if deleted + then (UNION (x’, y), deleted) + else let val (y’, deleted) = delete_one’ y + in (UNION (x, y’), deleted) + end + end + | delete_one’ (ELEM x) = + if x = w then (EMPTY, true) else (ELEM x, false) + | delete_one’ x = (x, false) + val (m’, _) = delete_one’ m + in simplify m’ + end +``` + +Haskell: + +``` +delete_one m w = do + let (m’, _) = delete_one’ m + simplify m’ + where + delete_one’ (Union x y) = + let (x’, deleted) = delete_one’ x + in if deleted + then (Union x’ y, deleted) + else let (y’, deleted) = delete_one’ y + in (Union x y’, deleted) + delete_one’ (Elem x) = + if x == w then (Empty, True) else (Elem x, False) + delete_one’ x = (x, False) +``` + +## Higher order functions + +The first line is always the SML code, the second line always the Haskell variant: + +``` +fun make_map_fn f1 = fn (x,y) => f1 x :: y +make_map_fn f1 = \x y -> f1 x : y + +fun make_filter_fn f1 = fn (x,y) => if f1 x then x :: y else y +make_filter_fn f1 = \x y -> if f1 then x : y else y + +fun my_map f l = foldr (make_map_fn f) [] l +my_map f l = foldr (make_map_fn f) [] l + +fun my_filter f l = foldr (make_filter_fn f) [] l +my_filter f l = foldr (make_filter_fn f) [] l +``` + +E-Mail me your thoughts at comments@mx.buetow.org! + +=> ../ Go back to the main site diff --git a/content/gemtext/gemfeed/atom.xml b/content/gemtext/gemfeed/atom.xml index bd99e96b..89cd5036 100644 --- a/content/gemtext/gemfeed/atom.xml +++ b/content/gemtext/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-14T14:54:18+01:00</updated> + <updated>2021-05-14T23:00:11+01:00</updated> <title>buetow.org feed</title> <subtitle>Having fun with computers!</subtitle> <link href="gemini://buetow.org/gemfeed/atom.xml" rel="self" /> @@ -1237,6 +1237,160 @@ BB </content> </entry> <entry> + <title>Standard ML and Haskell</title> + <link href="gemini://buetow.org/gemfeed/2010-04-09-standard-ml-and-haskell.gmi" /> + <id>gemini://buetow.org/gemfeed/2010-04-09-standard-ml-and-haskell.gmi</id> + <updated>2010-04-09T22:57:36+01:00</updated> + <author> + <name>Paul Buetow</name> + <email>comments@mx.buetow.org</email> + </author> + <summary>I am currently looking into the functional programming language Standard ML (aka SML). The purpose is to refresh my functional programming skills and to learn something new too. Since I already know a little Haskell, could I do not help myself and I implemented the same exercises in Haskell too.. .....to read on please visit my site.</summary> + <content type="xhtml"> + <div xmlns="http://www.w3.org/1999/xhtml"> + <h1>Standard ML and Haskell</h1> +<p class="quote"><i>Written by Paul Buetow 2010-04-09</i></p> +<p>I am currently looking into the functional programming language Standard ML (aka SML). The purpose is to refresh my functional programming skills and to learn something new too. Since I already know a little Haskell, could I do not help myself and I implemented the same exercises in Haskell too.</p> +<p>As you will see, SML and Haskell are very similar (at least when it comes to the basics). However, the syntax of Haskell is little bit more "advanced". Haskell utilises fewer keywords (e.g. no val, end, fun, fn ...). Haskell also allows to explicitly write down the function types. What I have been missing in SML so far is the so-called pattern guards. Although this is a very superficial comparison for now, so far I like Haskell more than SML. Nevertheless, I thought it would be fun to demonstrate a few simple functions of both languages to show off the similarities. </p> +<p>Haskell is also a "pure functional" programming language, whereas SML also makes explicit use of imperative concepts. I am by far not a specialist in either of these languages but here are a few functions implemented in both, SML and Haskell:</p> +<h2>Defining a multi data type</h2> +<p>Standard ML:</p> +<pre> +datatype ’a multi + = EMPTY + | ELEM of ’a + | UNION of ’a multi * ’a multi +</pre> +<p>Haskell:</p> +<pre> +data (Eq a) => Multi a + = Empty + | Elem a + | Union (Multi a) (Multi a) + deriving Show +</pre> +<h2>Processing a multi</h2> +<p>Standard ML:</p> +<pre> +fun number (EMPTY) _ = 0 + | number (ELEM x) w = if x = w then 1 else 0 + | number (UNION (x,y)) w = (number x w) + (number y w) +fun test_number w = number (UNION (EMPTY, \ + UNION (ELEM 4, UNION (ELEM 6, \ + UNION (UNION (ELEM 4, ELEM 4), EMPTY))))) w +</pre> +<p>Haskell:</p> +<pre> +number Empty _ = 0 +number (Elem x) w = if x == w then 1 else 0 +test_number w = number (Union Empty \ + (Union (Elem 4) (Union (Elem 6) \ + (Union (Union (Elem 4) (Elem 4)) Empty)))) w +</pre> +<h2>Simplify function</h2> +<p>Standard ML:</p> +<pre> +fun simplify (UNION (x,y)) = + let fun is_empty (EMPTY) = true | is_empty _ = false + val x’ = simplify x + val y’ = simplify y + in if (is_empty x’) andalso (is_empty y’) + then EMPTY + else if (is_empty x’) + then y’ + else if (is_empty y’) + then x’ + else UNION (x’, y’) + end + | simplify x = x +</pre> +<p>Haskell:</p> +<pre> +simplify (Union x y) + | (isEmpty x’) && (isEmpty y’) = Empty + | isEmpty x’ = y’ + | isEmpty y’ = x’ + | otherwise = Union x’ y’ + where + isEmpty Empty = True + isEmpty _ = False + x’ = simplify x + y’ = simplify y +simplify x = x +</pre> +<h2>Delete all</h2> +<p>Standard ML:</p> +<pre> +fun delete_all m w = + let fun delete_all’ (ELEM x) = if x = w then EMPTY else ELEM x + | delete_all’ (UNION (x,y)) = UNION (delete_all’ x, delete_all’ y) + | delete_all’ x = x + in simplify (delete_all’ m) + end +</pre> +<p>Haskell:</p> +<pre> +delete_all m w = simplify (delete_all’ m) + where + delete_all’ (Elem x) = if x == w then Empty else Elem x + delete_all’ (Union x y) = Union (delete_all’ x) (delete_all’ y) + delete_all’ x = x +</pre> +<h2>Delete one</h2> +<p>Standard ML:</p> +<pre> +fun delete_one m w = + let fun delete_one’ (UNION (x,y)) = + let val (x’, deleted) = delete_one’ x + in if deleted + then (UNION (x’, y), deleted) + else let val (y’, deleted) = delete_one’ y + in (UNION (x, y’), deleted) + end + end + | delete_one’ (ELEM x) = + if x = w then (EMPTY, true) else (ELEM x, false) + | delete_one’ x = (x, false) + val (m’, _) = delete_one’ m + in simplify m’ + end +</pre> +<p>Haskell:</p> +<pre> +delete_one m w = do + let (m’, _) = delete_one’ m + simplify m’ + where + delete_one’ (Union x y) = + let (x’, deleted) = delete_one’ x + in if deleted + then (Union x’ y, deleted) + else let (y’, deleted) = delete_one’ y + in (Union x y’, deleted) + delete_one’ (Elem x) = + if x == w then (Empty, True) else (Elem x, False) + delete_one’ x = (x, False) +</pre> +<h2>Higher order functions</h2> +<p>The first line is always the SML code, the second line always the Haskell variant:</p> +<pre> +fun make_map_fn f1 = fn (x,y) => f1 x :: y +make_map_fn f1 = \x y -> f1 x : y + +fun make_filter_fn f1 = fn (x,y) => if f1 x then x :: y else y +make_filter_fn f1 = \x y -> if f1 then x : y else y + +fun my_map f l = foldr (make_map_fn f) [] l +my_map f l = foldr (make_map_fn f) [] l + +fun my_filter f l = foldr (make_filter_fn f) [] l +my_filter f l = foldr (make_filter_fn f) [] l +</pre> +<p>E-Mail me your thoughts at comments@mx.buetow.org!</p> + </div> + </content> + </entry> + <entry> <title>Perl Poetry</title> <link href="gemini://buetow.org/gemfeed/2008-06-26-perl-poetry.gmi" /> <id>gemini://buetow.org/gemfeed/2008-06-26-perl-poetry.gmi</id> diff --git a/content/gemtext/gemfeed/index.gmi b/content/gemtext/gemfeed/index.gmi index 6b35f04a..b08827d8 100644 --- a/content/gemtext/gemfeed/index.gmi +++ b/content/gemtext/gemfeed/index.gmi @@ -11,4 +11,5 @@ => ./2016-04-03-offsite-backup-with-zfs.gmi 2016-04-03 - Offsite backup with ZFS => ./2011-05-07-perl-daemon-service-framework.gmi 2011-05-07 - Perl Daemon (Service Framework) => ./2010-05-09-the-fype-programming-language.gmi 2010-05-09 - The Fype Programming Language +=> ./2010-04-09-standard-ml-and-haskell.gmi 2010-04-09 - Standard ML and Haskell => ./2008-06-26-perl-poetry.gmi 2008-06-26 - Perl Poetry diff --git a/content/gemtext/index.gmi b/content/gemtext/index.gmi index 3020e633..ad6a89d9 100644 --- a/content/gemtext/index.gmi +++ b/content/gemtext/index.gmi @@ -61,4 +61,5 @@ I have switched blog software multiple times. I might be back filling some of th => ./gemfeed/2016-04-03-offsite-backup-with-zfs.gmi 2016-04-03 - Offsite backup with ZFS => ./gemfeed/2011-05-07-perl-daemon-service-framework.gmi 2011-05-07 - Perl Daemon (Service Framework) => ./gemfeed/2010-05-09-the-fype-programming-language.gmi 2010-05-09 - The Fype Programming Language +=> ./gemfeed/2010-04-09-standard-ml-and-haskell.gmi 2010-04-09 - Standard ML and Haskell => ./gemfeed/2008-06-26-perl-poetry.gmi 2008-06-26 - Perl Poetry diff --git a/content/html/gemfeed/2010-04-09-standard-ml-and-haskell.html b/content/html/gemfeed/2010-04-09-standard-ml-and-haskell.html new file mode 100644 index 00000000..95099e43 --- /dev/null +++ b/content/html/gemfeed/2010-04-09-standard-ml-and-haskell.html @@ -0,0 +1,193 @@ +<!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>Having fun with computers!</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%; +} +p.quote:before { + content: " | "; + padding-left: 2px; +} +a.textlink:before { + content: " ⇒ "; + padding-left: 2px; +} +a.textlink { + text-decoration: none; + color: #FF0000; +} +a.textlink:hover { + text-decoration: underline; +} +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>Standard ML and Haskell</h1> +<p class="quote"><i>Written by Paul Buetow 2010-04-09</i></p> +<p>I am currently looking into the functional programming language Standard ML (aka SML). The purpose is to refresh my functional programming skills and to learn something new too. Since I already know a little Haskell, could I do not help myself and I implemented the same exercises in Haskell too.</p> +<p>As you will see, SML and Haskell are very similar (at least when it comes to the basics). However, the syntax of Haskell is little bit more "advanced". Haskell utilises fewer keywords (e.g. no val, end, fun, fn ...). Haskell also allows to explicitly write down the function types. What I have been missing in SML so far is the so-called pattern guards. Although this is a very superficial comparison for now, so far I like Haskell more than SML. Nevertheless, I thought it would be fun to demonstrate a few simple functions of both languages to show off the similarities. </p> +<p>Haskell is also a "pure functional" programming language, whereas SML also makes explicit use of imperative concepts. I am by far not a specialist in either of these languages but here are a few functions implemented in both, SML and Haskell:</p> +<h2>Defining a multi data type</h2> +<p>Standard ML:</p> +<pre> +datatype ’a multi + = EMPTY + | ELEM of ’a + | UNION of ’a multi * ’a multi +</pre> +<p>Haskell:</p> +<pre> +data (Eq a) => Multi a + = Empty + | Elem a + | Union (Multi a) (Multi a) + deriving Show +</pre> +<h2>Processing a multi</h2> +<p>Standard ML:</p> +<pre> +fun number (EMPTY) _ = 0 + | number (ELEM x) w = if x = w then 1 else 0 + | number (UNION (x,y)) w = (number x w) + (number y w) +fun test_number w = number (UNION (EMPTY, \ + UNION (ELEM 4, UNION (ELEM 6, \ + UNION (UNION (ELEM 4, ELEM 4), EMPTY))))) w +</pre> +<p>Haskell:</p> +<pre> +number Empty _ = 0 +number (Elem x) w = if x == w then 1 else 0 +test_number w = number (Union Empty \ + (Union (Elem 4) (Union (Elem 6) \ + (Union (Union (Elem 4) (Elem 4)) Empty)))) w +</pre> +<h2>Simplify function</h2> +<p>Standard ML:</p> +<pre> +fun simplify (UNION (x,y)) = + let fun is_empty (EMPTY) = true | is_empty _ = false + val x’ = simplify x + val y’ = simplify y + in if (is_empty x’) andalso (is_empty y’) + then EMPTY + else if (is_empty x’) + then y’ + else if (is_empty y’) + then x’ + else UNION (x’, y’) + end + | simplify x = x +</pre> +<p>Haskell:</p> +<pre> +simplify (Union x y) + | (isEmpty x’) && (isEmpty y’) = Empty + | isEmpty x’ = y’ + | isEmpty y’ = x’ + | otherwise = Union x’ y’ + where + isEmpty Empty = True + isEmpty _ = False + x’ = simplify x + y’ = simplify y +simplify x = x +</pre> +<h2>Delete all</h2> +<p>Standard ML:</p> +<pre> +fun delete_all m w = + let fun delete_all’ (ELEM x) = if x = w then EMPTY else ELEM x + | delete_all’ (UNION (x,y)) = UNION (delete_all’ x, delete_all’ y) + | delete_all’ x = x + in simplify (delete_all’ m) + end +</pre> +<p>Haskell:</p> +<pre> +delete_all m w = simplify (delete_all’ m) + where + delete_all’ (Elem x) = if x == w then Empty else Elem x + delete_all’ (Union x y) = Union (delete_all’ x) (delete_all’ y) + delete_all’ x = x +</pre> +<h2>Delete one</h2> +<p>Standard ML:</p> +<pre> +fun delete_one m w = + let fun delete_one’ (UNION (x,y)) = + let val (x’, deleted) = delete_one’ x + in if deleted + then (UNION (x’, y), deleted) + else let val (y’, deleted) = delete_one’ y + in (UNION (x, y’), deleted) + end + end + | delete_one’ (ELEM x) = + if x = w then (EMPTY, true) else (ELEM x, false) + | delete_one’ x = (x, false) + val (m’, _) = delete_one’ m + in simplify m’ + end +</pre> +<p>Haskell:</p> +<pre> +delete_one m w = do + let (m’, _) = delete_one’ m + simplify m’ + where + delete_one’ (Union x y) = + let (x’, deleted) = delete_one’ x + in if deleted + then (Union x’ y, deleted) + else let (y’, deleted) = delete_one’ y + in (Union x y’, deleted) + delete_one’ (Elem x) = + if x == w then (Empty, True) else (Elem x, False) + delete_one’ x = (x, False) +</pre> +<h2>Higher order functions</h2> +<p>The first line is always the SML code, the second line always the Haskell variant:</p> +<pre> +fun make_map_fn f1 = fn (x,y) => f1 x :: y +make_map_fn f1 = \x y -> f1 x : y + +fun make_filter_fn f1 = fn (x,y) => if f1 x then x :: y else y +make_filter_fn f1 = \x y -> if f1 then x : y else y + +fun my_map f l = foldr (make_map_fn f) [] l +my_map f l = foldr (make_map_fn f) [] l + +fun my_filter f l = foldr (make_filter_fn f) [] l +my_filter f l = foldr (make_filter_fn f) [] l +</pre> +<p>E-Mail me your thoughts at comments@mx.buetow.org!</p> +<a class="textlink" href="../">Go back to the main site</a><br /> +</body> +</html> diff --git a/content/html/gemfeed/atom.xml b/content/html/gemfeed/atom.xml index 10d6f703..b351cf62 100644 --- a/content/html/gemfeed/atom.xml +++ b/content/html/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-14T14:54:18+01:00</updated> + <updated>2021-05-14T23:00:11+01:00</updated> <title>buetow.org feed</title> <subtitle>Having fun with computers!</subtitle> <link href="https://buetow.org/gemfeed/atom.xml" rel="self" /> @@ -1237,6 +1237,160 @@ BB </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> + <updated>2010-04-09T22:57:36+01:00</updated> + <author> + <name>Paul Buetow</name> + <email>comments@mx.buetow.org</email> + </author> + <summary>I am currently looking into the functional programming language Standard ML (aka SML). The purpose is to refresh my functional programming skills and to learn something new too. Since I already know a little Haskell, could I do not help myself and I implemented the same exercises in Haskell too.. .....to read on please visit my site.</summary> + <content type="xhtml"> + <div xmlns="http://www.w3.org/1999/xhtml"> + <h1>Standard ML and Haskell</h1> +<p class="quote"><i>Written by Paul Buetow 2010-04-09</i></p> +<p>I am currently looking into the functional programming language Standard ML (aka SML). The purpose is to refresh my functional programming skills and to learn something new too. Since I already know a little Haskell, could I do not help myself and I implemented the same exercises in Haskell too.</p> +<p>As you will see, SML and Haskell are very similar (at least when it comes to the basics). However, the syntax of Haskell is little bit more "advanced". Haskell utilises fewer keywords (e.g. no val, end, fun, fn ...). Haskell also allows to explicitly write down the function types. What I have been missing in SML so far is the so-called pattern guards. Although this is a very superficial comparison for now, so far I like Haskell more than SML. Nevertheless, I thought it would be fun to demonstrate a few simple functions of both languages to show off the similarities. </p> +<p>Haskell is also a "pure functional" programming language, whereas SML also makes explicit use of imperative concepts. I am by far not a specialist in either of these languages but here are a few functions implemented in both, SML and Haskell:</p> +<h2>Defining a multi data type</h2> +<p>Standard ML:</p> +<pre> +datatype ’a multi + = EMPTY + | ELEM of ’a + | UNION of ’a multi * ’a multi +</pre> +<p>Haskell:</p> +<pre> +data (Eq a) => Multi a + = Empty + | Elem a + | Union (Multi a) (Multi a) + deriving Show +</pre> +<h2>Processing a multi</h2> +<p>Standard ML:</p> +<pre> +fun number (EMPTY) _ = 0 + | number (ELEM x) w = if x = w then 1 else 0 + | number (UNION (x,y)) w = (number x w) + (number y w) +fun test_number w = number (UNION (EMPTY, \ + UNION (ELEM 4, UNION (ELEM 6, \ + UNION (UNION (ELEM 4, ELEM 4), EMPTY))))) w +</pre> +<p>Haskell:</p> +<pre> +number Empty _ = 0 +number (Elem x) w = if x == w then 1 else 0 +test_number w = number (Union Empty \ + (Union (Elem 4) (Union (Elem 6) \ + (Union (Union (Elem 4) (Elem 4)) Empty)))) w +</pre> +<h2>Simplify function</h2> +<p>Standard ML:</p> +<pre> +fun simplify (UNION (x,y)) = + let fun is_empty (EMPTY) = true | is_empty _ = false + val x’ = simplify x + val y’ = simplify y + in if (is_empty x’) andalso (is_empty y’) + then EMPTY + else if (is_empty x’) + then y’ + else if (is_empty y’) + then x’ + else UNION (x’, y’) + end + | simplify x = x +</pre> +<p>Haskell:</p> +<pre> +simplify (Union x y) + | (isEmpty x’) && (isEmpty y’) = Empty + | isEmpty x’ = y’ + | isEmpty y’ = x’ + | otherwise = Union x’ y’ + where + isEmpty Empty = True + isEmpty _ = False + x’ = simplify x + y’ = simplify y +simplify x = x +</pre> +<h2>Delete all</h2> +<p>Standard ML:</p> +<pre> +fun delete_all m w = + let fun delete_all’ (ELEM x) = if x = w then EMPTY else ELEM x + | delete_all’ (UNION (x,y)) = UNION (delete_all’ x, delete_all’ y) + | delete_all’ x = x + in simplify (delete_all’ m) + end +</pre> +<p>Haskell:</p> +<pre> +delete_all m w = simplify (delete_all’ m) + where + delete_all’ (Elem x) = if x == w then Empty else Elem x + delete_all’ (Union x y) = Union (delete_all’ x) (delete_all’ y) + delete_all’ x = x +</pre> +<h2>Delete one</h2> +<p>Standard ML:</p> +<pre> +fun delete_one m w = + let fun delete_one’ (UNION (x,y)) = + let val (x’, deleted) = delete_one’ x + in if deleted + then (UNION (x’, y), deleted) + else let val (y’, deleted) = delete_one’ y + in (UNION (x, y’), deleted) + end + end + | delete_one’ (ELEM x) = + if x = w then (EMPTY, true) else (ELEM x, false) + | delete_one’ x = (x, false) + val (m’, _) = delete_one’ m + in simplify m’ + end +</pre> +<p>Haskell:</p> +<pre> +delete_one m w = do + let (m’, _) = delete_one’ m + simplify m’ + where + delete_one’ (Union x y) = + let (x’, deleted) = delete_one’ x + in if deleted + then (Union x’ y, deleted) + else let (y’, deleted) = delete_one’ y + in (Union x y’, deleted) + delete_one’ (Elem x) = + if x == w then (Empty, True) else (Elem x, False) + delete_one’ x = (x, False) +</pre> +<h2>Higher order functions</h2> +<p>The first line is always the SML code, the second line always the Haskell variant:</p> +<pre> +fun make_map_fn f1 = fn (x,y) => f1 x :: y +make_map_fn f1 = \x y -> f1 x : y + +fun make_filter_fn f1 = fn (x,y) => if f1 x then x :: y else y +make_filter_fn f1 = \x y -> if f1 then x : y else y + +fun my_map f l = foldr (make_map_fn f) [] l +my_map f l = foldr (make_map_fn f) [] l + +fun my_filter f l = foldr (make_filter_fn f) [] l +my_filter f l = foldr (make_filter_fn f) [] l +</pre> +<p>E-Mail me your thoughts at comments@mx.buetow.org!</p> + </div> + </content> + </entry> + <entry> <title>Perl Poetry</title> <link href="https://buetow.org/gemfeed/2008-06-26-perl-poetry.html" /> <id>https://buetow.org/gemfeed/2008-06-26-perl-poetry.html</id> diff --git a/content/html/gemfeed/index.html b/content/html/gemfeed/index.html index 55f0fc7e..971b9fd6 100644 --- a/content/html/gemfeed/index.html +++ b/content/html/gemfeed/index.html @@ -60,6 +60,7 @@ h2, h3 { <a class="textlink" href="./2016-04-03-offsite-backup-with-zfs.html">2016-04-03 - Offsite backup with ZFS</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-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 /> </body> </html> diff --git a/content/html/index.html b/content/html/index.html index 6c8bed44..6d3b1337 100644 --- a/content/html/index.html +++ b/content/html/index.html @@ -96,6 +96,7 @@ h2, h3 { <a class="textlink" href="./gemfeed/2016-04-03-offsite-backup-with-zfs.html">2016-04-03 - Offsite backup with ZFS</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-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 /> </body> </html> diff --git a/content/md/gemfeed/2010-04-09-standard-ml-and-haskell.md b/content/md/gemfeed/2010-04-09-standard-ml-and-haskell.md new file mode 100644 index 00000000..f43ef55a --- /dev/null +++ b/content/md/gemfeed/2010-04-09-standard-ml-and-haskell.md @@ -0,0 +1,174 @@ +# Standard ML and Haskell + +> Written by Paul Buetow 2010-04-09 + +I am currently looking into the functional programming language Standard ML (aka SML). The purpose is to refresh my functional programming skills and to learn something new too. Since I already know a little Haskell, could I do not help myself and I implemented the same exercises in Haskell too. + +As you will see, SML and Haskell are very similar (at least when it comes to the basics). However, the syntax of Haskell is little bit more "advanced". Haskell utilises fewer keywords (e.g. no val, end, fun, fn ...). Haskell also allows to explicitly write down the function types. What I have been missing in SML so far is the so-called pattern guards. Although this is a very superficial comparison for now, so far I like Haskell more than SML. Nevertheless, I thought it would be fun to demonstrate a few simple functions of both languages to show off the similarities. + +Haskell is also a "pure functional" programming language, whereas SML also makes explicit use of imperative concepts. I am by far not a specialist in either of these languages but here are a few functions implemented in both, SML and Haskell: + +## Defining a multi data type + +Standard ML: + +``` +datatype ’a multi + = EMPTY + | ELEM of ’a + | UNION of ’a multi * ’a multi +``` + +Haskell: + +``` +data (Eq a) => Multi a + = Empty + | Elem a + | Union (Multi a) (Multi a) + deriving Show +``` + +## Processing a multi + +Standard ML: + +``` +fun number (EMPTY) _ = 0 + | number (ELEM x) w = if x = w then 1 else 0 + | number (UNION (x,y)) w = (number x w) + (number y w) +fun test_number w = number (UNION (EMPTY, \ + UNION (ELEM 4, UNION (ELEM 6, \ + UNION (UNION (ELEM 4, ELEM 4), EMPTY))))) w +``` + +Haskell: + +``` +number Empty _ = 0 +number (Elem x) w = if x == w then 1 else 0 +test_number w = number (Union Empty \ + (Union (Elem 4) (Union (Elem 6) \ + (Union (Union (Elem 4) (Elem 4)) Empty)))) w +``` + +## Simplify function + +Standard ML: + +``` +fun simplify (UNION (x,y)) = + let fun is_empty (EMPTY) = true | is_empty _ = false + val x’ = simplify x + val y’ = simplify y + in if (is_empty x’) andalso (is_empty y’) + then EMPTY + else if (is_empty x’) + then y’ + else if (is_empty y’) + then x’ + else UNION (x’, y’) + end + | simplify x = x +``` + +Haskell: + +``` +simplify (Union x y) + | (isEmpty x’) && (isEmpty y’) = Empty + | isEmpty x’ = y’ + | isEmpty y’ = x’ + | otherwise = Union x’ y’ + where + isEmpty Empty = True + isEmpty _ = False + x’ = simplify x + y’ = simplify y +simplify x = x +``` + +## Delete all + +Standard ML: + +``` +fun delete_all m w = + let fun delete_all’ (ELEM x) = if x = w then EMPTY else ELEM x + | delete_all’ (UNION (x,y)) = UNION (delete_all’ x, delete_all’ y) + | delete_all’ x = x + in simplify (delete_all’ m) + end +``` + +Haskell: + +``` +delete_all m w = simplify (delete_all’ m) + where + delete_all’ (Elem x) = if x == w then Empty else Elem x + delete_all’ (Union x y) = Union (delete_all’ x) (delete_all’ y) + delete_all’ x = x +``` + +## Delete one + +Standard ML: + +``` +fun delete_one m w = + let fun delete_one’ (UNION (x,y)) = + let val (x’, deleted) = delete_one’ x + in if deleted + then (UNION (x’, y), deleted) + else let val (y’, deleted) = delete_one’ y + in (UNION (x, y’), deleted) + end + end + | delete_one’ (ELEM x) = + if x = w then (EMPTY, true) else (ELEM x, false) + | delete_one’ x = (x, false) + val (m’, _) = delete_one’ m + in simplify m’ + end +``` + +Haskell: + +``` +delete_one m w = do + let (m’, _) = delete_one’ m + simplify m’ + where + delete_one’ (Union x y) = + let (x’, deleted) = delete_one’ x + in if deleted + then (Union x’ y, deleted) + else let (y’, deleted) = delete_one’ y + in (Union x y’, deleted) + delete_one’ (Elem x) = + if x == w then (Empty, True) else (Elem x, False) + delete_one’ x = (x, False) +``` + +## Higher order functions + +The first line is always the SML code, the second line always the Haskell variant: + +``` +fun make_map_fn f1 = fn (x,y) => f1 x :: y +make_map_fn f1 = \x y -> f1 x : y + +fun make_filter_fn f1 = fn (x,y) => if f1 x then x :: y else y +make_filter_fn f1 = \x y -> if f1 then x : y else y + +fun my_map f l = foldr (make_map_fn f) [] l +my_map f l = foldr (make_map_fn f) [] l + +fun my_filter f l = foldr (make_filter_fn f) [] l +my_filter f l = foldr (make_filter_fn f) [] l +``` + +E-Mail me your thoughts at comments@mx.buetow.org! + +[Go back to the main site](../) diff --git a/content/md/gemfeed/index.md b/content/md/gemfeed/index.md index 841eba09..98922fef 100644 --- a/content/md/gemfeed/index.md +++ b/content/md/gemfeed/index.md @@ -11,4 +11,5 @@ [2016-04-03 - Offsite backup with ZFS](./2016-04-03-offsite-backup-with-zfs.md) [2011-05-07 - Perl Daemon (Service Framework)](./2011-05-07-perl-daemon-service-framework.md) [2010-05-09 - The Fype Programming Language](./2010-05-09-the-fype-programming-language.md) +[2010-04-09 - Standard ML and Haskell](./2010-04-09-standard-ml-and-haskell.md) [2008-06-26 - Perl Poetry](./2008-06-26-perl-poetry.md) diff --git a/content/md/index.md b/content/md/index.md index 3bac82ba..8658bd60 100644 --- a/content/md/index.md +++ b/content/md/index.md @@ -61,4 +61,5 @@ I have switched blog software multiple times. I might be back filling some of th [2016-04-03 - Offsite backup with ZFS](./gemfeed/2016-04-03-offsite-backup-with-zfs.md) [2011-05-07 - Perl Daemon (Service Framework)](./gemfeed/2011-05-07-perl-daemon-service-framework.md) [2010-05-09 - The Fype Programming Language](./gemfeed/2010-05-09-the-fype-programming-language.md) +[2010-04-09 - Standard ML and Haskell](./gemfeed/2010-04-09-standard-ml-and-haskell.md) [2008-06-26 - Perl Poetry](./gemfeed/2008-06-26-perl-poetry.md) diff --git a/content/meta/gemfeed/2010-04-09-standard-ml-and-haskell.meta b/content/meta/gemfeed/2010-04-09-standard-ml-and-haskell.meta new file mode 100644 index 00000000..325d65d6 --- /dev/null +++ b/content/meta/gemfeed/2010-04-09-standard-ml-and-haskell.meta @@ -0,0 +1,5 @@ +local meta_date="2010-04-09T22:57:36+01:00" +local meta_author="Paul Buetow" +local meta_email="comments@mx.buetow.org" +local meta_title="Standard ML and Haskell" +local meta_summary="I am currently looking into the functional programming language Standard ML (aka SML). The purpose is to refresh my functional programming skills and to learn something new too. Since I already know a little Haskell, could I do not help myself and I implemented the same exercises in Haskell too.. .....to read on please visit my site." |
