Search code examples
functional-programmingsmlsmlnj

New to ML: How to store return values of type a* a* a*


I have a program that returns int*int

(Example for illustration purposes): fun program(a,b) = (1,2)

I want to do something along the lines:

fun program(a,b)
if a = 0 then (1,2)
else
val x,y = program(a-1,b)
return (x-1, y)

Basically, I want to manipulate the tuple that is returned, and then return a modification of it.

Thanks


Solution

  • This works almost exactly as you wrote it, except that your syntax is a bit off:

    fun program(a,b) =
      if a = 0 then (1,2)
      else
        let val (x,y) = program(a-1,b) in
          (x-1, y)
        end
    

    Specifically:

    1. Functions are defined by fun f args = body - you left out the =.
    2. Variables are bound with let val foo = bar in baz end.
    3. There is no return keyword in sml.