Search code examples
functional-programminghigher-order-functionsozmozart

Is there a way to store function/procedure calls in Oz?


I want to know if there is a way to bind a whole function call in a variable in Oz. For example, I do have:

fun {F1 A1 A2} 
    A1+A2 
end

and a local variable X.

What I want to do, is store the call (as-is) {F1 1 2} in X and not its result: 3, so that I may call again {F1 1 2} again by somehow referring to X.

Is this possible in Oz? If so, how?

Thank you for your answers.


Solution

  • The easiest way is to dynamically create a function that takes no arguments, e.g.

    fun {CreateCall F A1 A2}
       fun {$}
          {F A1 A2}
       end
    end
    
    fun {F A1 A2}
       A1 + A2
    end
    
    C = {CreateCall F 1 2}
    
    {Show {C}}
    

    The function CreateCall creates and returns an anonymous nullary function which calls F with the given arguments. (This technique is similar to partial function application in languages like Haskell.)

    It is possible to generalize this for an arbitrary number of arguments using the library function Procedure.apply (doc). If you want to do this and need help, let me know.