I've been reading Out of the Tar Pit by Ben Moseley and Peter Marks and in section 5.2.3 they discuss state in functional languages compared to procedural languages. The procedural example is as follows:
procedure int getNextCounter()
// 'counter' is declared and initialized elsewhere in the code
counter := counter + 1
return counter
I can see there is internal state so this is fine. The functional example however is as follows:
function (int,int) getNextCounter(int oldCounter)
let int result = oldCounter + 1
let int newCounter = oldCounter + 1
return (newCounter, result)
I can understand that you avoid internal state by passing in oldCounter
, but why the two equal outputs?
Is this just to pass out the state
and the result
and both being equal is simply due to an overly simplistic example?
Isn't returning several values considered not that good of an idea?
Am I just not well versed enough in functional programming?
let in newCounter
belet in*t* newCounter
? – jscs Apr 4 '18 at 12:56