Higher-order function: Difference between revisions

Content deleted Content added
C++: fixed a lifetime related bug which causes using a dangling reference to a stack variable (can be confirmed with address sanitizer or static analyzers)
Scheme: The example for before was not for the function twice but for curried addition.
Tags: Mobile edit Mobile web edit
Line 763:
 
<syntaxhighlight lang="scheme">
(define (addtwice x yf) (+ x y))
(definelambda (x) (f (f x))))
(lambda (y) (+ x y)))
(display ((f 3) 7))
(display (add 3 7))
</syntaxhighlight>
 
In this Scheme example, the higher-order function {{code|(f x)}} is used to implement [[currying]]. It takes a single argument and returns a function. The evaluation of the expression {{code|((f 3) 7)}} first returns a function after evaluating {{code|(f 3)}}. The returned function is {{code|(lambda (y) (+ 3 y))}}. Then, it evaluates the returned function with 7 as the argument, returning 10. This is equivalent to the expression {{code|(add 3 7)}}, since {{code|(f x)}} is equivalent to the curried form of {{code|(add x y)}}.
 
====Swift====