News for hackers. Stuff that matters.
Re:Misconceptions about loops in C by agumonkey on Thursday June 27, 2024 @04:54PM (#40814989)
It's a complex topic. The thing is that sequential mutable state doesn't scale, and you'll quickly have to introduce routines, modules, objects of whatever to have some control over side effects. The natural aspect of a list of instruction is exhilarating for many but is also rope to hang your self because you won't try to separate concepts and scopes since everything is accessible. So much stuff just doesn't happen when you don't use this paradigm. Instead of drowning in state variable improperly synchronised, you suddenly rise above and start rewriting trees.
Re:Misconceptions about loops in C by knome on Thursday June 27, 2024 @09:28PM (#40816939)
>you'll quickly have to introduce routines, modules, objects of whatever to have some control over side effects

that claim seems absurd. all the functional languages I've come across also frequently use routines, modules, and 'objects of whatever' to manage the complexity of huge amounts of code.

objects/entities are just a pattern for hiding complexity behind a simple interface. they can be useful in any language, regardless of mutability.

Re:Misconceptions about loops in C by zogrodea on Friday June 28, 2024 @12:52AM (#40817836)
I think, because of the mention about controlling mutability/side-effects, "object" in that post means OOP-style objects which mutable state. It's not an issue with immutable objects/structs (or I guess "records" which is the term I'm used to with functional languages).
Re:Misconceptions about loops in C by knome on Friday June 28, 2024 @01:32AM (#40818025)
functional languages still have to deal with storing and manipulating changing value and state, and it is still often advantageous to hide the complexities of that storage and manipulation behind simple interfaces throughout the codebase, using opaque values with helper functions to manipulate them.

OOP as a pattern, rather than a language construct.

the great advantage here is that all manipulations are local. there is no spooky action at a distance in an immutable language.

as long as you stay out of the IO monad, anyways.

Re:Misconceptions about loops in C by zogrodea on Friday June 28, 2024 @04:15PM (#40824799)
I just use simple tail-recursion in an event loop to help manage state.

let rec eventLoop state = let input = getInput () let newState = update input state drawState state; eventLoop newState

The update function takes an existing state and an input record (key events, etc.), returning a new state (which may have nested records within itself but it is all immutable).

New state is kept by the recursion (passing new state to the same function). The only IO is getting input and drawing, which are inherently side-effecting operations.

I do have nested records, but (at each stage) they get joined into a new parent record. No mutability there.