Elements of ML Programming

Ullman, Jeffrey D.
Date finished: 1998-12-14

(Posted on Slashdot)

This slim book is a fine introduction to the ML97 version of the ML programming language, and is worth reading by anyone interested in learning about functional programming. It starts off by teaching you about simple expressions and ML's basic data types, covers writing recursive functions to perform common tasks, and by the end is discussing data structures and information hiding. There are lots of examples and exercises, which you can try out by downloading the SML/NJ implementation. Ullman's writing style is simple and clear, so it's not difficult to understand, and I really enjoyed reading it (sadly a rarity with technical books these days).

ML is mostly a functional language, though it supports some degree of imperative programming, and is an elegant system worth attention. The language has some novel features; my favorite is its automatic type inference. A friend of mine once observed that many programming bugs stem from type mismatches, and points out that because of ML's strict type inference and checking, once you get an ML program to compile successfully it often produces correct results. For example, look at the following interaction with the SML/NJ interpreter:

- fun sumlist( nil ) = 0
= |   sumlist( x::xs ) = x + sumlist(xs);
*val sumlist = fn : int list -> int*

ML is smart enough to figure out that the resulting sumlist function takes a list of integers as input ('int list') and returns an integer ('-> int'). It can therefore report an error if you attempt to pass it a list of real numbers:

- sumlist( [1,2,3] );
*val it = 6 : int*
- sumlist( [1.0,4.0,2.0] );
*stdIn:31.1-31.25 Error: operator and operand
                 don't agree [tycon mismatch]
  operator domain: int list
  operand:         real list
  in expression:
    sumlist (1.0 :: 4.0 :: 2.0 :: nil)*

While it's unlikely that you'll ever write a large program in ML, or get a job through your ML knowledge, that only matters to market-driven drones. Hackers should read this book because it demonstrates an style of programming, which will benefit your programming in more conventional languages.

Tagged: programming languages

Permalink: http://books.amk.ca/1998/12/Elements_ML_Programming.html

%T  Elements of ML Programming
%@  1998-12-14
%A  Ullman, Jeffrey D.
%K  programming languages

http://books.amk.ca

Contact me