

 This page uses content from Wikipedia. The original article was at Currying. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance)



Task

Create a simple demonstrative example of Currying in a specific language.

Add any historic details as to how the feature made its way into the language.




 
Module LikeCpp {
      divide=lambda (x, y)->x/y
      partsof120=lambda divide ->divide(![], 120)
      Print "half of 120 is ";partsof120(2)
      Print "a third is ";partsof120(3)
      Print "and a quarter is ";partsof120(4)
}
LikeCpp
 
Module Joke {
      \\ we can call F1(),  with any number of arguments, and always read one and then
      \\ call itself passing the remain arguments
      \\ ![] take stack of values and place it in the next call.
      F1=lambda -> {
            if empty then exit
            Read x
            =x+lambda(![])
      }
 
      Print F1(F1(2),2,F1(-4))=0
      Print F1(-4,F1(2),2)=0
      Print F1(2, F1(F1(2),2))=F1(F1(F1(2),2),2)
      Print F1(F1(F1(2),2),2)=6
      Print F1(2, F1(2, F1(2),2))=F1(F1(F1(2),2, F1(2)),2)
      Print F1(F1(F1(2),2, F1(2)),2)=8
      Print F1(2, F1(10, F1(2, F1(2),2)))=F1(F1(F1(2),2, F1(2)),2, 10)
      Print F1(F1(F1(2),2, F1(2)),2, 10)=18
      Print F1(2,2,2,2,10)=18
      Print F1()=0
 
      Group F2 {
            Sum=0
            Function Add  (x){
                  .Sum+=x
                  =x
            }
      }
      Link F2.Add() to F2()
      Print F1(F1(F1(F2(2)),F2(2), F1(F2(2))),F2(2))=8
      Print F2.Sum=8
}
Joke
 