Jump to content

Currying: Difference between revisions

2,206 bytes added ,  5 years ago
no edit summary
m (→‎{{header|C}}: Remove vanity tags)
No edit summary
Line 1,035:
assert(add2(5) == 2+5)
</lang>
 
=={{header|M2000 Interpreter}}==
<lang M2000 Interpreter>
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
</lang>
Without joke, can anyone answer this puzzle?
<lang M2000 Interpreter>
Module Puzzle {
Global Group F2 {
Sum=0
Sum2=0
Function Add (x){
.Sum+=x
=x
}
}
F1=lambda -> {
if empty then exit
Read x
Print ">>>", F2.Sum
F2.Sum2++ ' add one each time we read x
=x+lambda(![])
}
Link F2.Add() to F2()
P=F1(F1(F1(F2(2)),F2(2), F1(F2(2))),F2(2))=8
Print F2.Sum=8
Print F2.Sum2=7
\\ We read 7 times x, but we get 8, 2+2+2+2
\\ So 3 times x was zero, or not?
\\ but where we pass zero?
\\ zero return from F1 if no argument pass, so how x get zero??
}
Puzzle
</lang>
 
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.