foldr f LightOff input_stream
foldr f LightOff input_stream
Where are the previous inputs and results (= state) of the calculation stored which allow the next calculation depend on previous results? In some variable?
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
}# foldl
state = LightOff
while data {
input = get_next(data)
state = f(state, input)
}I want you to translate the above state machine to a Turing Machine, or demonstrate in any way how a TM is a tool or model which brings any kind of clarity or simplicity to any modelling, design, compilation or coding activity.