| 1 |
= ReturnStatement |
| 2 |
|
| 3 |
Programmers coming from languages that have a `return` statement, such |
| 4 |
as C, Java, and Python, often ask how one can translate functions that |
| 5 |
return early into SML. This page briefly describes a number of ways |
| 6 |
to translate uses of `return` to SML. |
| 7 |
|
| 8 |
== Conditional iterator function |
| 9 |
|
| 10 |
A conditional iterator function, such as |
| 11 |
https://smlfamily.github.io/Basis/list.html#SIG:LIST.find:VAL[`List.find`], |
| 12 |
https://smlfamily.github.io/Basis/list.html#SIG:LIST.exists:VAL[`List.exists`], |
| 13 |
or |
| 14 |
https://smlfamily.github.io/Basis/list.html#SIG:LIST.all:VAL[`List.all`] |
| 15 |
is probably what you want in most cases. Unfortunately, it might be |
| 16 |
the case that the particular conditional iteration pattern that you |
| 17 |
want isn't provided for your data structure. Usually the best |
| 18 |
alternative in such a case is to implement the desired iteration |
| 19 |
pattern as a higher-order function. For example, to implement a |
| 20 |
`find` function for arrays (which already exists as |
| 21 |
https://smlfamily.github.io/Basis/array.html#SIG:ARRAY.findi:VAL[`Array.find`]) |
| 22 |
one could write |
| 23 |
|
| 24 |
[source,sml] |
| 25 |
---- |
| 26 |
fun find predicate array = let |
| 27 |
fun loop i = |
| 28 |
if i = Array.length array then |
| 29 |
NONE |
| 30 |
else if predicate (Array.sub (array, i)) then |
| 31 |
SOME (Array.sub (array, i)) |
| 32 |
else |
| 33 |
loop (i+1) |
| 34 |
in |
| 35 |
loop 0 |
| 36 |
end |
| 37 |
---- |
| 38 |
|
| 39 |
Of course, this technique, while probably the most common case in |
| 40 |
practice, applies only if you are essentially iterating over some data |
| 41 |
structure. |
| 42 |
|
| 43 |
== Escape handler |
| 44 |
|
| 45 |
Probably the most direct way to translate code using `return` |
| 46 |
statements is to basically implement `return` using exception |
| 47 |
handling. The mechanism can be packaged into a reusable module with |
| 48 |
the signature |
| 49 |
(https://github.com/MLton/mltonlib/blob/master/com/ssh/extended-basis/unstable/public/control/exit.sig[`exit.sig`]): |
| 50 |
[source,sml] |
| 51 |
---- |
| 52 |
include::https://raw.github.com/MLton/mltonlib/master/com/ssh/extended-basis/unstable/public/control/exit.sig[indent=0,lines=6..-1] |
| 53 |
---- |
| 54 |
|
| 55 |
(<<References#HarperEtAl93,Typing First-Class Continuations in ML>> |
| 56 |
discusses the typing of a related construct.) The implementation |
| 57 |
(https://github.com/MLton/mltonlib/blob/master/com/ssh/extended-basis/unstable/detail/control/exit.sml[`exit.sml`]) |
| 58 |
is straightforward: |
| 59 |
[source,sml] |
| 60 |
---- |
| 61 |
include::https://raw.github.com/MLton/mltonlib/master/com/ssh/extended-basis/unstable/detail/control/exit.sml[indent=0,lines=6..-1] |
| 62 |
---- |
| 63 |
|
| 64 |
Here is an example of how one could implement a `find` function given |
| 65 |
an `app` function: |
| 66 |
[source,sml] |
| 67 |
---- |
| 68 |
fun appToFind (app : ('a -> unit) -> 'b -> unit) |
| 69 |
(predicate : 'a -> bool) |
| 70 |
(data : 'b) = |
| 71 |
Exit.call |
| 72 |
(fn return => |
| 73 |
(app (fn x => |
| 74 |
if predicate x then |
| 75 |
return (SOME x) |
| 76 |
else |
| 77 |
()) |
| 78 |
data |
| 79 |
; NONE)) |
| 80 |
---- |
| 81 |
|
| 82 |
In the above, as soon as the expression `predicate x` evaluates to |
| 83 |
`true` the `app` invocation is terminated. |
| 84 |
|
| 85 |
|
| 86 |
== Continuation-passing Style (CPS) |
| 87 |
|
| 88 |
A general way to implement complex control patterns is to use |
| 89 |
http://en.wikipedia.org/wiki/Continuation-passing_style[CPS]. In CPS, |
| 90 |
instead of returning normally, functions invoke a function passed as |
| 91 |
an argument. In general, multiple continuation functions may be |
| 92 |
passed as arguments and the ordinary return continuation may also be |
| 93 |
used. As an example, here is a function that finds the leftmost |
| 94 |
element of a binary tree satisfying a given predicate: |
| 95 |
[source,sml] |
| 96 |
---- |
| 97 |
datatype 'a tree = LEAF | BRANCH of 'a tree * 'a * 'a tree |
| 98 |
|
| 99 |
fun find predicate = let |
| 100 |
fun recurse continue = |
| 101 |
fn LEAF => |
| 102 |
continue () |
| 103 |
| BRANCH (lhs, elem, rhs) => |
| 104 |
recurse |
| 105 |
(fn () => |
| 106 |
if predicate elem then |
| 107 |
SOME elem |
| 108 |
else |
| 109 |
recurse continue rhs) |
| 110 |
lhs |
| 111 |
in |
| 112 |
recurse (fn () => NONE) |
| 113 |
end |
| 114 |
---- |
| 115 |
|
| 116 |
Note that the above function returns as soon as the leftmost element |
| 117 |
satisfying the predicate is found. |