Search code examples
functionsmlfunction-declaration

"Circular" Function Declaration in SML


I want to use functions in a "circular" way, as shown in the following example:

fun cll1 (s)= cll2(s);
fun cll2 (s)= cll3(s);
fun cll3 (s)= cll(s);

Writing this produces an error in SML that the constructor cll2 is unbound. Could someone help me write something along these lines? It's possible in C; I'd like to write it in SML.


Solution

  • You want the and keyword.

    fun cll1 s = cll2 s
    and cll2 s = cll3 s
    and cll3 s = cll s
    

    Obviously these definitions won't do since it's an infinite recursion (ordinarily you'd test for a base case in one or more of the functions), but that's the general form.