Tips for Golfing in Standard ML

6

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.

Laikoni

Posted 2017-12-13T22:32:21.283

Reputation: 23 676

Answers

6

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 don not need white space between symbolic identifiers and alphanumeric identifiers or keywords.

Example

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 *)

Laikoni

Posted 2017-12-13T22:32:21.283

Reputation: 23 676