+
1
|
skin
|
login
|
edit
ELS_2014
::
p227
user:anonymous
_h3 2.2.7) first class functions _p According to [[rosettacode.org|http://rosettacode.org/wiki/First-class_functions#JavaScript]], lambdatalk has First Class Functions: {blockquote _p A language has first-class functions if it can do each of the following without recursively invoking a compiler or interpreter or otherwise metaprogramming: _ul Create new functions from preexisting functions at run-time _ul Store functions in collections _ul Use functions as arguments to other functions _ul Use functions as return values of other functions } _p Example: {pre 1) add cube and cuberoot user functions, 2) store sin, cos and cube in the "array" fun 3) store asin, acos and cuberoot in the "array" inv 4) define compose(f,g,x) as f(g(x)) 5) display compose(fun[i],inv[i],0.5) for i in [0,2] {b The result must be always 0.5}, within the limits of computational accuracy. } {pre {@ class="code_bloc"} °° {pre {def cube {lambda {:x} {pow :x 3}}} {def cuberoot {lambda {:x} {pow :x {/ 1 3}}}} {def compose {lambda {:f :g :x} {:f {:g :x}}}} {def fun sin cos cube} {def inv asin acos cuberoot} {def display {lambda {:i} {br}{compose {nth :i {fun}} {nth :i {inv}} 0.5} }} {map display {serie 0 2}} } °° } {pre {@ class="view_bloc"} {pre {def cube {lambda {:x} {pow :x 3}}} {def cuberoot {lambda {:x} {pow :x {/ 1 3}}}} {def compose {lambda {:f :g :x} {:f {:g :x}}}} {def fun sin cos cube} {def inv asin acos cuberoot} {def display {lambda {:i} {br}{compose {nth :i {fun}} {nth :i {inv}} 0.5} }} {map display {serie 0 2}} } } {div {@ style="clear:both;"}} _p More can be seen in [[alphawiki|http://epsilonwiki.free.fr/alphawiki_2/index.php?view=syntax_others_firstclassfunctions]]. _p Let's now see [[let and set! special forms|?view=p228]]. {include YAW_frame}