Question

This is just an example. These two functions are connected. You really want to call the one called lowest. It should then return the lowest number of the two. This of course won't work because you at the time of compiling the reducemax function you make a call to an at the time undefined function called lowest.

fun reducemax (i:int * int):int =
    if (#1 i) > (#2 i)
    then lowest(((#1 i)-1), (#2 i))
    else lowest((#1 i), ((#2 i)-1));

fun lowest (i:int * int):int =
    if (#1 i) = (#2 i)
    then (#1 i)
    else reducemax((#1 i), (#2 i));

I know I can use let to declare the reducemax function inside lowest, but is there any way around this? For example like in C, declare the function without defining it. I do understand the reducemax function need to know lowest will return an int and taking a int * int argument.

Was it helpful?

Solution

Just replace the second fun with and and remove the semicolons. This defines mutually recursive functions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top