Standard ML (or golfier: SML) is a general-purpose, modular, functional programming language with compile-time type checking and type inference. (from Wikipedia)

Though you almost certainly will never win a code golf challenge with SML, (ab-) using it for golfing can nevertheless be quite fun (<- keyword for function declaration). So without further ado ...

What tips do you have for golfing in Standard ML?

Please only post tips which are somewhat unique to SML. General golfing tips belong to Tips for golfing in all languages.


Use symbolic identifiers

From The Definition of Standard Ml (revised) [PDF], Section 2.4:

An identifier is either alphanumeric: any sequence of letters, digits, primes (') and underbars (_) starting with a letter or prime, or symbolic: any non-empty sequence of the following symbols:

!  %  &  $  #  +  -  /  :  <  =  >  ?  @  \  ~  ‘  ^  |  * 

In either case, however, reserved words are excluded. This means that for example # and | are not identifiers, but ## and |=| are identifiers.

The important part (concerning golfing) is that you do not need white space between symbolic identifiers and alphanumeric identifiers or keywords.


fun f a b c=if b then a else c

By replacing f by $ and b by !, we can save 6 bytes:

fun$a!c=if!then a else c

Note, however, that a symbolic identifier needs white space when next to other symbols. That's why we cannot replace a and c in the above example to get to the same byte count:

fun f$b!=if b then$else!    (* syntax error because != is parsed as one token *)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.