Talk:Fixed-point combinator: Difference between revisions

Content deleted Content added
Wstomv (talk | contribs)
Line 261:
 
::I think we should return most of the code examples (excl. wrong ones, as C#). While it's the case that they do not add any encyclopedic value, I find them really useful when one tries to grasp anonymous recursion. They may provide better background for understanding somewhat more abstract way of implementing recursion with y-combinator serving as quick how-it's-done reference than more theoretically-inclined lambda calculus or ML/Haskell - imo most people comfort with these languages already have sound theoretical foundations and concrete code example adds little or no benefit. I suggest keeping at least Python/Javascript and Scheme/Lisp. [[User:БръмБръм|БръмБръм]] ([[User talk:БръмБръм|talk]]) 13:40, 14 February 2013 (UTC)
 
== Why no detail for imperative language w/o recursion? ==
 
Under "Explanation for imperative programmers", it is stated that "It is difficult and confusing to try to write fixed_point() in imperative terms, so we will not attempt it."
 
However, the example there can be fixed to address this (if I am not mistaken), without the explicit introduction of a general fixed-point combinator for that language, i.e., it can be changed to show how a non-iterative, non-recursive solution can be programmed. How about this rewrite (changes in '''bold'''):
 
:How is a recursive function written in these languages? It is done by breaking the function into two parts. The first part looks very similar to the recursive version of the function. The difference is that it takes a function as a parameter and calls that parameter instead of calling itself recursively. (Remember, functions are values so they can be passed as arguments to functions. This isn't often done in imperative programming.)
:
factorial_helper(function foo, integer n) { ''--- added function as a parameter''
if n=0 then
return 1;
else
return n * foo('''foo,''' n-1); ''--- recursive call replaced with call to parameter''
}
:
:Now, if that function parameter was "factorial", then this helper function would calculate the factorial. So, the second part of building the recursive function is to create a function "factorial" and set it equal to "factorial_helper" with "factorial_helper" as the first parameter.
:
factorial(integer n) {
factorial_helper('''factorial_helper''', n) ''--- "factorial_helper" is passed as the function parameter to itself''
}
:
Now, the subsequent paragraph can be deleted:
 
:This doesn't quite work because we've run into the same problem as before: the function "factorial" is using its value before we get to the end of its definition. The fixed-point function solves this for us. Due to its special structure, it is able to call "factorial_helper" recursively without using a value before it is defined. The fixed point function takes the helper as an argument and returns the recursive function. (Remember functions are values - they can be passed as arguments and returned by functions.)
:
factorial = fixed_point(factorial_helper)
:
:It is difficult and confusing to try to write fixed_point() in imperative terms, so we will not attempt it.
:
Instead, it can be pointed out that this specific solution could be obtained via a general fixed-point combinator, which can also be used for other recursive definitions, rather than relying on ad hoc solutions (for the fixed point).
 
[[User:Wstomv|Wstomv]] ([[User talk:Wstomv|talk]]) 20:34, 26 August 2013 (UTC)