Published at 2010-04-09T22:57:36+01:00

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.

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.

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:

Standard ML:

datatype’a multi = EMPTY | ELEMof’a | UNIONof’a multi * ’a multi

Haskell:

data(Eq a) => Multi a = Empty | Elem a | Union (Multi a) (Multi a)derivingShow

Standard ML:

funnumber (EMPTY) _ = 0 | number (ELEM x) w =ifx = wthen1else0 | number (UNION (x,y)) w = (number x w) + (number y w)funtest_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 =ifx == wthen1else0 test_number w = number (Union Empty \ (Union (Elem 4) (Union (Elem 6) \ (Union (Union (Elem 4) (Elem 4)) Empty)))) w

Standard ML:

funsimplify (UNION (x,y)) =letfunis_empty (EMPTY) = true | is_empty _ = falsevalx’ = simplify xvaly’ = simplify yinif(is_empty x’)andalso(is_empty y’)thenEMPTYelseif(is_empty x’)theny’elseif(is_empty y’)thenx’elseUNION (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’whereisEmpty Empty = True isEmpty_= False x’ = simplify x y’ = simplify y simplify x = x

Standard ML:

fundelete_all m w =letfundelete_all’ (ELEM x) =ifx = wthenEMPTYelseELEM x | delete_all’ (UNION (x,y)) = UNION (delete_all’ x, delete_all’ y) | delete_all’ x = xinsimplify (delete_all’ m)end

Haskell:

delete_all m w = simplify (delete_all’ m)wheredelete_all’ (Elem x) =ifx == wthenEmptyelseElem x delete_all’ (Union x y) = Union (delete_all’ x) (delete_all’ y) delete_all’ x = x

Standard ML:

fundelete_one m w =letfundelete_one’ (UNION (x,y)) =letval(x’, deleted) = delete_one’ xinifdeletedthen(UNION (x’, y), deleted)elseletval(y’, deleted) = delete_one’ yin(UNION (x, y’), deleted)endend| delete_one’ (ELEM x) =ifx = wthen(EMPTY, true)else(ELEM x, false) | delete_one’ x = (x, false)val(m’, _) = delete_one’ minsimplify m’end

Haskell:

delete_one m w =dolet(m’,_) = delete_one’ m simplify m’wheredelete_one’ (Union x y) =let(x’, deleted) = delete_one’ xinifdeletedthen(Union x’ y, deleted)elselet(y’, deleted) = delete_one’ yin(Union x y’, deleted) delete_one’ (Elem x) =ifx == wthen(Empty, True)else(Elem x, False) delete_one’ x = (x, False)

The first line is always the SML code, the second line 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 your comments to paul@nospam.buetow.org :-)

Back to the main site