Search code examples
logichigher-order-functionsozmozart

Returning a function from a function in OZ. Higher order problems


I am trying to write a function that will essentially return a function as a result. I am trying to achieve something like this:

{{Add 3}4}

where the result will come out as 7. I found the following SO question that has the same question, but the answer did not work for me. I receive "Variable Adder not Introduced". I have tried declaring it locally, and also setting a variable equal to it, but as of yet, I have not gotten anything to work. I know what I want to do; I want to return a function that has been decorated by an outer function. Unfortunately, I can't find anything solid in the documentation on how to do that. Here is what I've been trying, based on the online docs.

declare
local
   proc {And M ?B}
      if M > 0 then B = M else B = 0 end
   end
in
   proc {Add2 J ?B}
     J +  {And}
   end
end

When I call

{{Browse {Add2 1}2}

I'm hoping to get 3. What I actually get is a compilation with no errors, that doesn't actually browse to anything. I know this isn't what I'm looking for, but it's all i've got so far. I want to take J, and add it to the result of calling whatever is left out there. Can anyone point the way?

Thanks,


Solution

  • Using function:

    functor
    import
       Application
       System
    define
       fun {Add X}
           fun {$ N} X+N end
       end
       {System.showInfo {{Add 10} 20}}
       {Application.exit 0}
    end
    

    Using procedure:

    functor
    import
       Application
       System
    define
       proc {Add X P}
           P = proc {$ N R} R = N+X end
       end
       {System.showInfo {{Add 10} 20}}
       {Application.exit 0}
    end