Search code examples
return-valuesmlsmlnj

Returning two variables in SML


I have a function below that uses variable X and variable A.

How can I return both of these variables to be able to use these values further down the program.

val a = 1000;
val x = 5;

fun test (x,a) =
    if (a<1) then(
    x)

    else( 
    test(x+1,a-1)
    )

Solution

  • You just return a pair:

    fun test (x, a) = if a < 1 then (x, a) else test (x+1, a-1)
    

    You receive it by pattern matching:

    val (y, z) = test (10, 11)