9
  • I was wondering: can infinite loops be done in functional programming?

example: when using the windows API to get windows messages, it is usually implemented in a loop.

I know it is possible to make a function that will keep going into recursion indefinitely. I expect that this will result in a stack overflow.

  • are infinite loop the wrong mind-set for functional programming ?

  • is the interface of the operating system or the hardware the problem ?

it doesn't seem to me like a functional program/o.s. could keep running by itself

I have a tiny bit of experience writing functional programs but this has always bothered me.please share your thoughts/insights about these issues

Dario's user avatar
Dario
49.4k8 gold badges99 silver badges131 bronze badges
askedAug 14, 2010 at 15:03
symbiont's user avatar
1
  • 2
    Try googling 'tail recursion'.CommentedAug 14, 2010 at 15:08

4 Answers4

5

As others have posted, an infinite loopis possible throughtail recursions.

E.g.loop() will effectively run as an infinite loop (in constant stack space) since the compiler can optimize out the tail-recursive call ofloop at the end.

let loop() = do {  println("foo")  loop()}

But

are infinite loop the wrong mind-set for functional programming ?

still got a point. Consider your Windows-API example with the infinite loop. That's anything but functional. Remember - functional means thinking invalues (andwhat they mean). Therefore, one would rather go a reactive/event-based approach like that [Pseudo-functional code]

  (onClick form1)|> Event.subscribe (\pt-> do { print $ "I was clicked at " ++ (show pt) })

So

it doesn't seem to me like a functional program/o.s. could keep running by itself

istechnically wrong - youcan implement infinite loops - but there is often no (functional) point in doing so. Why should one need that except for some kind of IO polling? Transforming values in a purely functional way should terminate to be meaningful.

answeredAug 14, 2010 at 15:20
Dario's user avatar
Sign up to request clarification or add additional context in comments.

2 Comments

interesting. i didn't think functional programming could get away with infinite recursions (good to know). thanks for the information and your insights. i like the idea of reactive programming.
As an example of why you'd do this: The core of a program is often best represented as a potentially infinite sequence of transformations in the world's state (e.g. Pac-Man is about to eat the pill, Pac-Man is one step up and has eaten the pill, etc). This is also the most common case for infinite loops in my experience.
5

If you usetail recursion, you effectively have an iteration, like a for/while loop. Therefore, I guess you can have an infinite loop without getting stack overflow.

To your question:"are infinite loop the wrong mind-set for functional programming?" maybe this will help:-While or Tail Recursion in F#, what to use when?

answeredAug 14, 2010 at 15:07
JohnB's user avatar

Comments

1

You can have infinitetail recursion if your compiler recognizes it. Some languages, e.g. scheme, require that compilers recognize tail recursion and allocate no stack space for it.

Edit I do not mean to disagree with the other answers, but "infinite" tail recursive loops with are a common idiom for dealing with the outside world. The following example is taken fromReal World Haskell, and is representative of the idiom.

mainloop :: Handle -> Handle -> IO ()mainloop inh outh =     do ineof <- hIsEOF inh       if ineof           then return ()           else do inpStr <- hGetLine inh                   hPutStrLn outh (map toUpper inpStr)                   mainloop inh outh

We are essentially considering the outside world as astream.

answeredAug 14, 2010 at 15:07
deinst's user avatar

Comments

1

Most if not all uses of "infinite loops" in functional programming can be modeled byco-recursion. Other answers so far have pointed to general recursion, but unrestricted use of recursion is arguably a poor approach as it can lead to poorly structured code.

The vast majority of code in a purely functional program should be written in the total subset, i.e. use patterns such as structural recursion or co-recursion (which ensure termination and progress) rather than falling back to general recursion. Hopefully, future versions of GHC will include direct support for detecting some total subset of Haskell and emitting warnings for code which cannot be proven to terminate or progress.

answeredAug 16, 2010 at 4:29
func's user avatar

Comments

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.