Strictly speaking, the above definition pattern-matches on the value () .
Without pattern matching, it could look like this:
- fun bar (x : unit) = ([1], [2], [3]); val bar = fn : unit -> int list * int list * int list - bar (); val it = ([1],[2],[3]) : int list * int list * int list
answered Oct 22, 2015 at 12:24
66.1k 3 3 gold badges 44 44 silver badges 85 85 bronze badges
Yes, the syntax () is a bit misleading in that this is an actual value.
Commented Oct 23, 2015 at 10:04
In SML type unit often represents an input/output action or more generally something which involves side effects. A somewhat realistic example of a function of the sort you are looking for would be one which returns 3 randomly generated lists. Another example would be one which pulls numbers from standard input something like:
open TextIO fun split s = String.tokens (fn c => c = #",") s fun toInt s = valOf (Int.fromString s) fun toIntList line = map toInt (split line) fun getInts prompt = ( print prompt; case inputLine(stdIn) of SOME line => toIntList line | NONE => [] ) fun getLists() = let val prompt = "Enter integers, separated by a comma: " in (getInts prompt, getInts prompt, getInts prompt) end
The type of getLists is
val getLists = fn : unit -> int list * int list * int list
a typical "run" of getLists :
- getLists(); Enter integers, separated by a comma: 1,2,3 Enter integers, separated by a comma: 4,5,6 Enter integers, separated by a comma: 7,8,9 val it = ([1,2,3],[4,5,6],[7,8,9]) : int list * int list * int list