Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The example in Haskell is great and sheds light on how you can "model" state-machines in a functional language. My concern was the suggestion by the article that state-machines are somehow the basis of all computation. Turing machines intuitively look like machines with state. Functional programs not so much.

Here's a simple example which I'm not saying proves my point but I have a question about:

I have a program which calculates the average temperature of the year so far. You input today's temperature into it and it calculates and outputs the new updated average. If it is implemented as a functional program I can see how it can be given a time-series as input and it outputs the average.

But today is a new day and I have a new temperature measurement which I give to my program. If it really is something I could call a "state-machine" it should be able to calculate the new average for me. But to do that it would need to have all the previous inputs held somewhere in its "state".

Is there a functional program that can do that and give me the updated average temperature? Examples in JavaScript please if possible. Not everybody understands Haskell. Or is it something that can be done only in Haskell?



  function updateAverage(averageAsOfYesterday, todayTemp, todayDayOfYear) {
      yesterdayDayOfYear = todayDayOfYear - 1
      sumOfAllTempsUpToAndIncludingYesterday = yesterdayDayOfYear * averageAsOfYesterday
      return (sumOfAllTempsUpToAndIncludingYesterday + todayTemp) / todayDayOfYear
  }




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: