News for hackers. Stuff that matters.
Re:Why study functional programming? (2012) by nine_k on Tuesday August 02, 2022 @03:00PM (#32322871)
Your global variables were / are shared with yourself, even in no-concurrency case. With typical control flow being far from linear, it's easy to surprise yourself with unexpected state mutation.
Re:Why study functional programming? (2012) by MaxBarraclough on Tuesday August 02, 2022 @04:43PM (#32324041)
Right. Pure functions make it considerably easier to reason about code, even in the absence of concurrency.

It's a shame D's pure modifier (to statically guarantee purity) isn't available in many other languages. [0] SPARK Ada has something similar in that functions must always be pure, forcing procedures to be used for impure functionality. [1] edit I think that's mistaken, SPARK appears to permit functions to read globals but not to modify them.

edit John Carmack has written about this kind of thing. From [2]:

> if a function only references a piece or two of global state, it is probably wise to consider passing it in as a variable. It would be kind of nice if C had a "functional" keyword to enforce no global references.

[0] https://dlang.org/spec/function.html#pure-functions

[1] https://learn.adacore.com/courses/intro-to-spark/chapters/01...

[2] https://cbarrete.com/carmack.html

Re:Why study functional programming? (2012) by Jtsummers on Tuesday August 02, 2022 @03:11PM (#32323011)
Giving me flashbacks to inherited codebases from old-school FORTRAN coders...

  // 500 variables shared by everything
  main loop
    "task" 1
    "task" 2
    "task" 3
    ...
    "task" n
So t121 is now supposed to only be used by task 3, but "t" as a prefix means it is temp. However, we don't want to change all the references. So just don't use it everyone else, please?
Re:Why study functional programming? (2012) by Scarbutt on Tuesday August 02, 2022 @03:06PM (#32322948)
Hence the value of immutability even for single-threaded code.