質問

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,

役に立ちましたか?

解決

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

他のヒント

If you don't want to use the functor syntax in Ankur's answer you could also write something like

local
    fun {Add A}
        fun {$ B} A+B end
    end
in
    {Browse {{Add 3} 4}}
end
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top