Function composition: Difference between revisions

no edit summary
(Add min)
No edit summary
Line 2,640:
<lang zkl>fcn fcomp(f,g,h,etc){
{ fcn(x,hgf){ T(x).pump(Void,hgf.xplode()) }.fp1(vm.arglist.reverse()); }</lang>
 
=={{header|ZX Spectrum Basic}}==
DEF FN commands can be nested, making this appear trivial:
<lang zxbasic>10 DEF FN f(x)=SQR x
20 DEF FN g(x)=ABS x
30 DEF FN c(x)=FN f(FN g(x))
40 PRINT FN c(-4)</lang>
Which gets you f(g(x)), for sure. But if you want g(f(x)) you need to DEF a whole new FN. Instead we can pass the function names as strings to a new function and numerically evaluate the string:
<lang zxbasic>10 DEF FN f(x)=SQR x
20 DEF FN g(x)=ABS x
30 DEF FN c(a$,b$,x)=VAL ("FN "+a$+"(FN "+b$+"(x))")
40 PRINT FN c("f","g",-4)
50 PRINT FN c("g","f",-4)</lang>
{{out}}
<pre>2
 
A Invalid argument, 50:1</pre>
77

edits