DEV Community

Cover image for SML - The language I love to hate. (Recursion example).
Eckhardt
Eckhardt

Posted on

SML - The language I love to hate. (Recursion example).

Standard ML has been around for a while, I never cared for it until enrolling at OSSU's program. It's a functional programming language with very interesting features like immutability and relies heavily on the usage of recursion.

A recursive example in SML

fun factorial n = 
    if n = 0 then 1 else n * factorial (n - 1)
Enter fullscreen mode Exit fullscreen mode

This function can also be expressed as:

fun factorial(n : int) = 
    if n = 0
    then 1
    else n * factorial (n - 1)
Enter fullscreen mode Exit fullscreen mode

If you use factorial it may look something like this:

val result = factorial (5) (* equivalent: 5 x 4 x 3 x 2 x 1 *)
Enter fullscreen mode Exit fullscreen mode

And in actual fact, this function actually (cheekily) multiplies by 1 again on the last iteration, which has no effect on the output: 120.

Writing SML as a web developer

During the course assignments it was a REAL pain to not have the ability to declare variables in the upper scope and change their value, or use loops, and dealing with the thinking work that goes into recursion.

Hot tip: If a function accepts an Array / List that needs to be processed in a loop - it most probably can be done with recursion too. 🀘

A JS example re-written in SML

Return the sum of all elements in an array

// Example 1
function sumArray (nums) {
  let total = 0;
  for (let i = 0; i < nums.length; i++) {
     total += nums[i]
  }
  return total
}

sumArray([1, 2, 3, 4, 5]) // 15

// Example 2
const sumArray = (nums) => nums.reduce((prev, curr) => prev + curr, 0)

sumArray([1, 2, 3, 4, 5]) // 15
Enter fullscreen mode Exit fullscreen mode

Much of the logic above is either ludicrous or doesn't exist in SML, and we'll have to do something like:

fun sum_array (nums: int list) =
  if null nums
  then 0
  else hd nums + sum_array(tl nums)

val summed = sum_array([1, 2, 3, 4, 5])  (* 15 *)
Enter fullscreen mode Exit fullscreen mode

These were just a few quick examples. If you want to see me explain these things (a little) more and If you want to see me do this with 5 slightly more complex problems, give my latest Youtube video a watch:


Appreciate the πŸ‘

Conclusion

I think we developers should definitely focus on the language that is most demanded from us - to improve our skills / knowledge, nevertheless - SML has taught me that there are different and sometimes better and cleaner ways of solving programming issues.


This has been Eckhardt Dreyer, coming to you with a look into what I explore on the web. If you like what I make Buy me a coffee? 🍺

Top comments (1)

Collapse
 
jzimmerman135 profile image
Jacob Zimmerman

Next time try

fun sum_array nums = List.foldl op + 0 nums
Enter fullscreen mode Exit fullscreen mode

or

fun sum_array [] = 0
  | sum_array [x :: xs] = x + sum_array xs
Enter fullscreen mode Exit fullscreen mode

Please don't write articles about a language you don't have experience with