diff options
| author | Paul Buetow <paul@buetow.org> | 2023-03-31 01:00:53 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2023-03-31 01:00:53 +0300 |
| commit | cca259cdb780316a2d636039709a90a44590156d (patch) | |
| tree | d48676b261a27387bf76e67eda478a8f367f3315 /gemfeed/2010-04-09-standard-ml-and-haskell.html | |
| parent | b1eb5dbb9b771af50e5e1992c101104936df8b0f (diff) | |
use of brs
Diffstat (limited to 'gemfeed/2010-04-09-standard-ml-and-haskell.html')
| -rw-r--r-- | gemfeed/2010-04-09-standard-ml-and-haskell.html | 81 |
1 files changed, 54 insertions, 27 deletions
diff --git a/gemfeed/2010-04-09-standard-ml-and-haskell.html b/gemfeed/2010-04-09-standard-ml-and-haskell.html index a7d7416e..fde33f72 100644 --- a/gemfeed/2010-04-09-standard-ml-and-haskell.html +++ b/gemfeed/2010-04-09-standard-ml-and-haskell.html @@ -9,28 +9,37 @@ </head> <body> <h1>Standard ML and Haskell</h1> -<p class="quote"><i>Published at 2010-04-09T22:57:36+01:00</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 knew a little Haskell, I could not help myself, and I also implemented the same exercises in Haskell.</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 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. </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> +<span class="quote"><i>Published at 2010-04-09T22:57:36+01:00</i></span><br /> +<br /> +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.<br /> +<br /> +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. <br /> +<br /> +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:<br /> +<br /> <h2>Defining a multi-data type</h2> -<p>Standard ML:</p> +Standard ML:<br /> +<br /> <pre> datatype ’a multi = EMPTY | ELEM of ’a | UNION of ’a multi * ’a multi -</pre><br /> -<p>Haskell:</p> +</pre> +<br /> +Haskell:<br /> +<br /> <pre> data (Eq a) => Multi a = Empty | Elem a | Union (Multi a) (Multi a) deriving Show -</pre><br /> +</pre> +<br /> <h2>Processing a multi</h2> -<p>Standard ML:</p> +Standard ML:<br /> +<br /> <pre> fun number (EMPTY) _ = 0 | number (ELEM x) w = if x = w then 1 else 0 @@ -38,17 +47,21 @@ fun number (EMPTY) _ = 0 fun test_number w = number (UNION (EMPTY, \ UNION (ELEM 4, UNION (ELEM 6, \ UNION (UNION (ELEM 4, ELEM 4), EMPTY))))) w -</pre><br /> -<p>Haskell:</p> +</pre> +<br /> +Haskell:<br /> +<br /> <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><br /> +</pre> +<br /> <h2>Simplify function</h2> -<p>Standard ML:</p> +Standard ML:<br /> +<br /> <pre> fun simplify (UNION (x,y)) = let fun is_empty (EMPTY) = true | is_empty _ = false @@ -63,8 +76,10 @@ fun simplify (UNION (x,y)) = else UNION (x’, y’) end | simplify x = x -</pre><br /> -<p>Haskell:</p> +</pre> +<br /> +Haskell:<br /> +<br /> <pre> simplify (Union x y) | (isEmpty x’) && (isEmpty y’) = Empty @@ -77,9 +92,11 @@ simplify (Union x y) x’ = simplify x y’ = simplify y simplify x = x -</pre><br /> +</pre> +<br /> <h2>Delete all</h2> -<p>Standard ML:</p> +Standard ML:<br /> +<br /> <pre> fun delete_all m w = let fun delete_all’ (ELEM x) = if x = w then EMPTY else ELEM x @@ -87,17 +104,21 @@ fun delete_all m w = | delete_all’ x = x in simplify (delete_all’ m) end -</pre><br /> -<p>Haskell:</p> +</pre> +<br /> +Haskell:<br /> +<br /> <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><br /> +</pre> +<br /> <h2>Delete one</h2> -<p>Standard ML:</p> +Standard ML:<br /> +<br /> <pre> fun delete_one m w = let fun delete_one’ (UNION (x,y)) = @@ -114,8 +135,10 @@ fun delete_one m w = val (m’, _) = delete_one’ m in simplify m’ end -</pre><br /> -<p>Haskell:</p> +</pre> +<br /> +Haskell:<br /> +<br /> <pre> delete_one m w = do let (m’, _) = delete_one’ m @@ -130,9 +153,11 @@ delete_one m w = do delete_one’ (Elem x) = if x == w then (Empty, True) else (Elem x, False) delete_one’ x = (x, False) -</pre><br /> +</pre> +<br /> <h2>Higher-order functions</h2> -<p>The first line is always the SML code, the second line the Haskell variant:</p> +The first line is always the SML code, the second line the Haskell variant:<br /> +<br /> <pre> fun make_map_fn f1 = fn (x,y) => f1 x :: y make_map_fn f1 = \x y -> f1 x : y @@ -145,8 +170,10 @@ 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 /> -<p>E-Mail your comments to hi@paul.cyou :-)</p> +</pre> +<br /> +E-Mail your comments to hi@paul.cyou :-)<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</a> | |
