Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 29, 2013, 04:23 PM   #1
Sydde
macrumors 68000
 
Sydde's Avatar
 
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.
Sydde is offline   0 Reply With Quote
Old Apr 29, 2013, 08:34 PM   #2
Catfish_Man
macrumors 68030
 
Catfish_Man's Avatar
 
Join Date: Sep 2001
Location: Portland, OR
Send a message via AIM to Catfish_Man
Quote:
Originally Posted by Sydde View Post
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.
Catfish_Man is offline   0 Reply With Quote
Old Apr 29, 2013, 09:39 PM   #3
lee1210
macrumors 68040
 
lee1210's Avatar
 
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
lee1210 is offline   0 Reply With Quote
Old 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 View Post
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?
softwareguy256 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Problem with Data usage recursive chezbob iPhone Tips, Help and Troubleshooting 2 Mar 27, 2014 06:18 PM
Solution for looping video presentation steveash Digital Video 3 Jan 22, 2014 08:51 AM
Mac startup sound looping hasenpfeffer Mac mini 2 May 15, 2013 01:38 PM
iPhone: Looping apple logo. Pez555 Jailbreaks and iOS Hacks 6 Feb 16, 2013 07:38 PM
Login looping, help! Maury MacBook 3 Jan 12, 2013 01:00 PM

Forum Jump

All times are GMT -5. The time now is 01:33 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC