summaryrefslogtreecommitdiff
path: root/gemfeed/2010-04-09-standard-ml-and-haskell.html
blob: 7fc14e02059188568865c7f493a654b4ed930f89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<!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>Standard ML and Haskell</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-04-09-standard-ml-and-haskell.md">Markdown</a> | <a href="gemini://foo.zone/gemfeed/2010-04-09-standard-ml-and-haskell.gmi">Gemini</a>
</p>
<h1 style='display: inline' id='standard-ml-and-haskell'>Standard ML and Haskell</h1><br />
<br />
<span class='quote'>Published at 2010-04-09T22:57:36+01:00</span><br />
<br />
<span>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 knew a little Haskell, I could not help myself, and I also implemented the same exercises in Haskell.</span><br />
<br />
<span>As you will see, SML and Haskell are very similar (at least when it comes to the basics). However, the syntax of Haskell is a bit more "advanced". Haskell utilizes fewer keywords (e.g. no val, end, fun, fn ...). Haskell also allows to write down the function types explicitly. 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. </span><br />
<br />
<span>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:</span><br />
<br />
<h2 style='display: inline' id='table-of-contents'>Table of Contents</h2><br />
<br />
<ul>
<li><a href='#standard-ml-and-haskell'>Standard ML and Haskell</a></li>
<li>⇢ <a href='#defining-a-multi-data-type'>Defining a multi-data type</a></li>
<li>⇢ <a href='#processing-a-multi'>Processing a multi</a></li>
<li>⇢ <a href='#simplify-function'>Simplify function</a></li>
<li>⇢ <a href='#delete-all'>Delete all</a></li>
<li>⇢ <a href='#delete-one'>Delete one</a></li>
<li>⇢ <a href='#higher-order-functions'>Higher-order functions</a></li>
</ul><br />
<h2 style='display: inline' id='defining-a-multi-data-type'>Defining a multi-data type</h2><br />
<br />
<span>Standard ML:</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><b><u><font color="#000000">datatype</font></u></b> ’a multi
	= EMPTY
	| ELEM <b><u><font color="#000000">of</font></u></b> ’a
	| UNION <b><u><font color="#000000">of</font></u></b> ’a multi * ’a multi
</pre>
<br />
<span>Haskell:</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><b><u><font color="#000000">data</font></u></b> (<b><font color="#000000">Eq</font></b> a) =&gt; <b><font color="#000000">Multi</font></b> a
    = <b><font color="#000000">Empty</font></b>
    | <b><font color="#000000">Elem</font></b> a
    | <b><font color="#000000">Union</font></b> (<b><font color="#000000">Multi</font></b> a) (<b><font color="#000000">Multi</font></b> a)
    <b><u><font color="#000000">deriving</font></u></b> <b><font color="#000000">Show</font></b>
</pre>
<br />
<h2 style='display: inline' id='processing-a-multi'>Processing a multi</h2><br />
<br />
<span>Standard ML:</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><b><u><font color="#000000">fun</font></u></b> number (EMPTY) _ = <font color="#000000">0</font>
	| number (ELEM x) w = <b><u><font color="#000000">if</font></u></b> x = w <b><u><font color="#000000">then</font></u></b> <font color="#000000">1</font> <b><u><font color="#000000">else</font></u></b> <font color="#000000">0</font>
	| number (UNION (x,y)) w = (number x w) + (number y w)
<b><u><font color="#000000">fun</font></u></b> test_number w = number (UNION (EMPTY, \
    UNION (ELEM <font color="#000000">4</font>, UNION (ELEM <font color="#000000">6</font>, \
    UNION (UNION (ELEM <font color="#000000">4</font>, ELEM <font color="#000000">4</font>), EMPTY))))) w 
</pre>
<br />
<span>Haskell:</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre>number <b><font color="#000000">Empty</font></b> <b><u><font color="#000000">_</font></u></b> = <font color="#000000">0</font>
number (<b><font color="#000000">Elem</font></b> x) w = <b><u><font color="#000000">if</font></u></b> x == w <b><u><font color="#000000">then</font></u></b> <font color="#000000">1</font> <b><u><font color="#000000">else</font></u></b> <font color="#000000">0</font>
test_number w = number (<b><font color="#000000">Union</font></b> <b><font color="#000000">Empty</font></b> \
    (<b><font color="#000000">Union</font></b> (<b><font color="#000000">Elem</font></b> <font color="#000000">4</font>) (<b><font color="#000000">Union</font></b> (<b><font color="#000000">Elem</font></b> <font color="#000000">6</font>) \
    (<b><font color="#000000">Union</font></b> (<b><font color="#000000">Union</font></b> (<b><font color="#000000">Elem</font></b> <font color="#000000">4</font>) (<b><font color="#000000">Elem</font></b> <font color="#000000">4</font>)) <b><font color="#000000">Empty</font></b>)))) w
</pre>
<br />
<h2 style='display: inline' id='simplify-function'>Simplify function</h2><br />
<br />
<span>Standard ML:</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><b><u><font color="#000000">fun</font></u></b> simplify (UNION (x,y)) =
    <b><u><font color="#000000">let</font></u></b> <b><u><font color="#000000">fun</font></u></b> is_empty (EMPTY) = true | is_empty _ = false
        <b><u><font color="#000000">val</font></u></b> x’ = simplify x
        <b><u><font color="#000000">val</font></u></b> y’ = simplify y
    <b><u><font color="#000000">in</font></u></b> <b><u><font color="#000000">if</font></u></b> (is_empty x’) <b><u><font color="#000000">andalso</font></u></b> (is_empty y’)
            <b><u><font color="#000000">then</font></u></b> EMPTY
       <b><u><font color="#000000">else</font></u></b> <b><u><font color="#000000">if</font></u></b> (is_empty x’)
            <b><u><font color="#000000">then</font></u></b> y’
       <b><u><font color="#000000">else</font></u></b> <b><u><font color="#000000">if</font></u></b> (is_empty y’)
            <b><u><font color="#000000">then</font></u></b> x’
       <b><u><font color="#000000">else</font></u></b> UNION (x’, y’)
    <b><u><font color="#000000">end</font></u></b>
  | simplify x = x
</pre>
<br />
<span>Haskell:</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre>simplify (<b><font color="#000000">Union</font></b> x y)
    | (isEmpty x’) &amp;&amp; (isEmpty y’) = <b><font color="#000000">Empty</font></b>
    | isEmpty x’ = y’
    | isEmpty y’ = x’
    | otherwise = <b><font color="#000000">Union</font></b> x’ y’
    <b><u><font color="#000000">where</font></u></b>
        isEmpty <b><font color="#000000">Empty</font></b> = <b><font color="#000000">True</font></b>
        isEmpty <b><u><font color="#000000">_</font></u></b> = <b><font color="#000000">False</font></b>
        x’ = simplify x
        y’ = simplify y
simplify x = x
</pre>
<br />
<h2 style='display: inline' id='delete-all'>Delete all</h2><br />
<br />
<span>Standard ML:</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><b><u><font color="#000000">fun</font></u></b> delete_all m w =
    <b><u><font color="#000000">let</font></u></b> <b><u><font color="#000000">fun</font></u></b> delete_all’ (ELEM x) = <b><u><font color="#000000">if</font></u></b> x = w <b><u><font color="#000000">then</font></u></b> EMPTY <b><u><font color="#000000">else</font></u></b> ELEM x
          | delete_all’ (UNION (x,y)) = UNION (delete_all’ x, delete_all’ y)
          | delete_all’ x = x
    <b><u><font color="#000000">in</font></u></b> simplify (delete_all’ m)
    <b><u><font color="#000000">end</font></u></b>
</pre>
<br />
<span>Haskell:</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre>delete_all m w = simplify (delete_all’ m)
    <b><u><font color="#000000">where</font></u></b>
        delete_all’ (<b><font color="#000000">Elem</font></b> x) = <b><u><font color="#000000">if</font></u></b> x == w <b><u><font color="#000000">then</font></u></b> <b><font color="#000000">Empty</font></b> <b><u><font color="#000000">else</font></u></b> <b><font color="#000000">Elem</font></b> x
        delete_all’ (<b><font color="#000000">Union</font></b> x y) = <b><font color="#000000">Union</font></b> (delete_all’ x) (delete_all’ y)
        delete_all’ x = x
</pre>
<br />
<h2 style='display: inline' id='delete-one'>Delete one</h2><br />
<br />
<span>Standard ML:</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><b><u><font color="#000000">fun</font></u></b> delete_one m w =
    <b><u><font color="#000000">let</font></u></b> <b><u><font color="#000000">fun</font></u></b> delete_one’ (UNION (x,y)) =
            <b><u><font color="#000000">let</font></u></b> <b><u><font color="#000000">val</font></u></b> (x’, deleted) = delete_one’ x
                <b><u><font color="#000000">in</font></u></b> <b><u><font color="#000000">if</font></u></b> deleted
                   <b><u><font color="#000000">then</font></u></b> (UNION (x’, y), deleted)
                   <b><u><font color="#000000">else</font></u></b> <b><u><font color="#000000">let</font></u></b> <b><u><font color="#000000">val</font></u></b> (y’, deleted) = delete_one’ y
                       <b><u><font color="#000000">in</font></u></b> (UNION (x, y’), deleted)
                   <b><u><font color="#000000">end</font></u></b>
                <b><u><font color="#000000">end</font></u></b>
          | delete_one’ (ELEM x) =
            <b><u><font color="#000000">if</font></u></b> x = w <b><u><font color="#000000">then</font></u></b> (EMPTY, true) <b><u><font color="#000000">else</font></u></b> (ELEM x, false)
          | delete_one’ x = (x, false)
            <b><u><font color="#000000">val</font></u></b> (m’, _) = delete_one’ m
        <b><u><font color="#000000">in</font></u></b> simplify m’
    <b><u><font color="#000000">end</font></u></b>
</pre>
<br />
<span>Haskell:</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre>delete_one m w = <b><u><font color="#000000">do</font></u></b>
    <b><u><font color="#000000">let</font></u></b> (m’, <b><u><font color="#000000">_</font></u></b>) = delete_one’ m
    simplify m’
    <b><u><font color="#000000">where</font></u></b>
        delete_one’ (<b><font color="#000000">Union</font></b> x y) =
            <b><u><font color="#000000">let</font></u></b> (x’, deleted) = delete_one’ x
            <b><u><font color="#000000">in</font></u></b> <b><u><font color="#000000">if</font></u></b> deleted
                <b><u><font color="#000000">then</font></u></b> (<b><font color="#000000">Union</font></b> x’ y, deleted)
                <b><u><font color="#000000">else</font></u></b> <b><u><font color="#000000">let</font></u></b> (y’, deleted) = delete_one’ y
                    <b><u><font color="#000000">in</font></u></b> (<b><font color="#000000">Union</font></b> x y’, deleted)
        delete_one’ (<b><font color="#000000">Elem</font></b> x) =
            <b><u><font color="#000000">if</font></u></b> x == w <b><u><font color="#000000">then</font></u></b> (<b><font color="#000000">Empty</font></b>, <b><font color="#000000">True</font></b>) <b><u><font color="#000000">else</font></u></b> (<b><font color="#000000">Elem</font></b> x, <b><font color="#000000">False</font></b>)
        delete_one’ x = (x, <b><font color="#000000">False</font></b>)
</pre>
<br />
<h2 style='display: inline' id='higher-order-functions'>Higher-order functions</h2><br />
<br />
<span>The first line is always the SML code, the second line the Haskell variant:</span><br />
<br />
<pre>
fun make_map_fn f1 = fn (x,y) =&gt; f1 x :: y
make_map_fn f1 = \x y -&gt; f1 x : y

fun make_filter_fn f1 = fn (x,y) =&gt; if f1 x then x :: y else y
make_filter_fn f1 = \x y -&gt; 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>
<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>