Apr 29, 2013, 04:23 PM #1 Sydde macrumors 68000     Join Date: Aug 2009 about recursive looping I was casually reading through a long tutorial on one of those odd functional languages (Haskell) and got to wondering about the recursion thing. Obviously, recursive loops in a procedural language look kind of elegant but could present something of a risk if compiled verbatim (an arbitrarily long run might result in a stack overrun, especially on a secondary thread). In functional languages, it seems like recursive loops (in source) are the de facto technique, presumably the compiler either converts them to iterative or uses some other implementation to prevent stack issues. My question is why do the functional languages (that I know of) prefer to express loops recursively? Is there some way that that construction looks more sensible to humans? Or is it simply that it is more concise? __________________ You got to be a spirit. You can't be no ghost. 0
Apr 29, 2013, 08:34 PM   #2
Catfish_Man
macrumors 68030

Join Date: Sep 2001
Location: Portland, OR
Quote:
 Originally Posted by Sydde I was casually reading through a long tutorial on one of those odd functional languages (Haskell) and got to wondering about the recursion thing. Obviously, recursive loops in a procedural language look kind of elegant but could present something of a risk if compiled verbatim (an arbitrarily long run might result in a stack overrun, especially on a secondary thread). In functional languages, it seems like recursive loops (in source) are the de facto technique, presumably the compiler either converts them to iterative or uses some other implementation to prevent stack issues. My question is why do the functional languages (that I know of) prefer to express loops recursively? Is there some way that that construction looks more sensible to humans? Or is it simply that it is more concise?
Recursion is fundamental to the underlying mathematical model of functional programming. Iteration requires mutable state (some form of index counter), and cannot be expressed purely in terms of functions. Stack overflow is avoided by tail-call optimization.

At the most extreme, Church Numerals express even basic arithmetic and numeric values in terms of functions.
0
 Apr 29, 2013, 09:39 PM #3 lee1210 macrumors 68040     Join Date: Jan 2005 Location: Dallas, TX In iterative languages that don't normally support tail call optimization to limit stack growth a technique called trampolining can be used to minimize stack depth. Generally functional languages support tail call optimization, so stack frames are reused for each call. -Lee 0
Apr 29, 2013, 10:05 PM   #4
softwareguy256
macrumors regular

Join Date: Jun 2010
Functional programming emphasize functions over storing explicit state. Using a loop requires state explicit state in the form of an iterator.

Quote:
 Originally Posted by Sydde I was casually reading through a long tutorial on one of those odd functional languages (Haskell) and got to wondering about the recursion thing. Obviously, recursive loops in a procedural language look kind of elegant but could present something of a risk if compiled verbatim (an arbitrarily long run might result in a stack overrun, especially on a secondary thread). In functional languages, it seems like recursive loops (in source) are the de facto technique, presumably the compiler either converts them to iterative or uses some other implementation to prevent stack issues. My question is why do the functional languages (that I know of) prefer to express loops recursively? Is there some way that that construction looks more sensible to humans? Or is it simply that it is more concise?
0

 MacRumors Forums