First-class functions/Use numbers analogously: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
m (→{{header|Phix}}: tidy) |
||
Line 1,163: | Line 1,163: | ||
Compared to first class functions, there are (as in my view there should be) significant differences in the treatment of numbers and functions, but as mentioned on that page tagging ctable entries should be quite sufficient. |
Compared to first class functions, there are (as in my view there should be) significant differences in the treatment of numbers and functions, but as mentioned on that page tagging ctable entries should be quite sufficient. |
||
<lang Phix>sequence ctable = {} |
<lang Phix>sequence ctable = {} |
||
function compose(integer f, |
function compose(integer f, g) |
||
ctable = append(ctable,{f,g}) |
ctable = append(ctable,{f,g}) |
||
integer cdx = length(ctable) |
|||
return cdx |
|||
end function |
end function |
||
function call_composite(integer |
function call_composite(integer cdx, atom x) |
||
integer g |
integer {f,g} = ctable[cdx] |
||
return f(g(x)) |
|||
return call_func(f,{call_func(g,{x})}) |
|||
end function |
end function |
||
function plus1(atom x) |
function plus1(atom x) |
||
return x+1 |
return x+1 |
||
end function |
end function |
||
function halve(atom x) |
function halve(atom x) |
||
return x/2 |
return x/2 |
||
end function |
end function |
||
constant m = compose( |
constant m = compose(halve,plus1) |
||
?call_composite(m,1) -- displays 1 |
?call_composite(m,1) -- displays 1 |
||
?call_composite(m,4) -- displays 2.5</lang> |
?call_composite(m,4) -- displays 2.5</lang> |