+
2
|
list
|
skin
|
login
|
editor
α-wwwiki
::
curry
user:none
(4298 bytes)
_h1 curryfication _p Some sources : _ul [[curry-cooking-up-tastier-functions|http://javascriptweblog.wordpress.com/2010/04/05/curry-cooking-up-tastier-functions/]] _ul [[top_ten|http://javascriptweblog.wordpress.com/2010/04/02/my-top-10-javascript-blogs/]] _ul [[www.svendtofte.com/code/curried_javascript|http://www.svendtofte.com/code/curried_javascript/]] _ul [[slid.es/gsklee/functional-programming-in-5-minutes|http://slid.es/gsklee/functional-programming-in-5-minutes#]] _h3 1) basic example {pre °° {input {@ type="submit" value="do it" onclick="•• var add = function (a,b,c) { return a+b+c; } var curried_add = function (a) { return function (b) { return function (c) { return a+b+c; } } } var content = add(1,2,3) + ' = ' + curried_add(1)(2)(3); getId('curry').innerHTML = content; this.disabled = true; ••"}} °°} {input {@ type="submit" value="do it" onclick="°° // var add = function (a,b,c) { return a+b+c; } var curried_add = function (a) { return function (b) { return function (c) { return a+b+c; } } } var content = add(1,2,3) + ' = ' + curried_add(1)(2)(3); // getId('curry').innerHTML = content; this.disabled = true; °°"}} {pre {@ id="curry"}} _h3 2) derivees of a function _p Grace to curryfication, the derivee of a function for a given value can be done in two "partial" steps, first creating the function, then applying it at a given value. And so, it is possible to define next derivees {u before} applying them at a given value. {pre Instead of : derivee( func, x), we use : derivee(func)(x) or in 2 steps : 1) f1 = derivee(func) -> function 2) f1(x) -> value } _h4 first derivees of cubic function {p Enter a value : {input {@ type="text" value="2" onblur="°° var derivee = function (f) { return function (x) { return (f(x+0.01)-f(x-0.01))/(2*0.01) } } var f0 = function (x) { return x*x*x; } // f0 = x^3 = 1 var f1 = derivee(f0); // f1 = 3*x^2 = 3 var f2 = derivee(f1); // f2 = 6*x = 6 var f3 = derivee(f2); // f3 = 6 var f4 = derivee(f3); // f4 = 0 var x = parseFloat( this.value ); getId('console').innerHTML = f0(x)+'\n'+f1(x)+'\n'+f2(x)+'\n'+f3(x)+'\n'+f4(x); °°"}}, then click outside the field.} {pre {@ id="console"}} _h4 code {pre °° var derivee = function (f) { return function (x) { return (f(x+0.01)-f(x-0.01))/(2*0.01) } } var f0 = function (x) { return x*x*x; } // f0 = x^3 = 1 var f1 = derivee(f0); // f1 = 3*x^2 = 3 var f2 = derivee(f1); // f2 = 6*x = 6 var f3 = derivee(f2); // f3 = 6 var f4 = derivee(f3); // f4 = 0 var x = parseFloat( this.value ); getId('console').innerHTML = f0(x)+'\n'+f1(x)+'\n'+f2(x)+'\n'+f3(x)+'\n'+f4(x); °°} _h6 The 4th derivee can be rewritten as : {pre var f4 = derivee(f3) = derivee(derivee(f2)) = derivee(derivee(derivee(f1))) = derivee(derivee(derivee(derivee(f0)))) var y = derivee(derivee(derivee(derivee(f0))))(x) = (derivee (derivee (derivee (derivee (f0)))) (x)) } _h6 a javascript code which looks like pure LISP code (see [[diff_int|../lambdaway_2.0/?view=diff_int]] in lambdaway) {pre °° function cubic at x=1 {define cubic (:x) (* :x :x :x)} -> cubic {cubic 1} -> 1 {{D cubic} 1} -> 3.0000009999997523 (≠ 3) {{D {D cubic}} 1} -> 5.999999999894978 (≠ -6) {{D {D {D cubic}}} 1} -> 6.0000000245974405 (≠ 6) {{D {D {D {D cubic}}}} 1} -> 0.00004163336342344337 (≠ 0) °°} _h3 todo _p Transpose the javascript curryfication in lambdatalk. Something like this : {pre °° {define add (a b) return parseFloat(a)+parseFloat(b);} {define curry_add (a b) •• if (b === undefined) return '{lambda (b) return parseFloat('+a+') + parseFloat(b);}'; else return parseFloat(a)+parseFloat(b); ••} {curry_add 1 2} == {{curry_add 1} 2} °°} {pre {define add (a b) return parseFloat(a)+parseFloat(b);} {define curry_add (a b) °° if (b === undefined) return '{lambda (b) return parseFloat('+a+') + parseFloat(b);}'; else return parseFloat(a)+parseFloat(b); °°} {curry_add 1 2} == {{curry_add 1} 2} }