A language a week: why ML was my favourite
At the University of Latvia we had a separate course on programming languages. Once a week, a new language: a class, then homework in it. Lisp, Prolog, Ada, ML…
The one I liked most was ML. It seemed to me the cleanest functional language: nothing superfluous, no input/output, no hacks. You describe data types and functions that take them apart case by case, and that’s it.
One of the homeworks in spring 2004 was symbolic differentiation. An expression is a data type, and the derivative is a function with its own rule for each kind of expression:
datatype aexp = acon of int | var of string
| plus of aexp * aexp | times of aexp * aexp | ...
fun diff _ (acon(_)) = acon(0)
| diff x (var(y)) = if x = y then acon(1) else acon(0)
| diff x (plus(a,b)) = update(plus(diff x a, diff x b))
| diff x (times(a,b)) =
update(plus(update(times(diff x a, b)), update(times(a, diff x b))))
I had something to compare it with: a couple of years earlier, in my F(x) function plotter in Delphi, I had done derivatives by rewriting the formula string by the product and quotient rules. In ML the same thing is just the rules from a calculus textbook, written one under another.
Another task was a tree in which every node has from zero to three children and holds a list of numbers. Exactly the same tree turned up later in my Prolog homework.
Many years later Solidity gave me a similar feeling: a smart contract has nothing superfluous either. No input/output, no environment, just data and the functions that change it.