News for hackers. Stuff that matters.
Re:Misconceptions about loops in C by bowsamic on Thursday June 27, 2024 @04:39PM (#40814826)
There seems to be this weird notion going around that the only reason imperative languages seem easier to people is because they're taught them first, and that if we teach Haskell or Scheme to everyone first then they would find imperative programming unintuitive. I honestly think it's a load of rubbish. The fact that humans easily understand "a list of instructions executed one after the other" over monads and tail recursion is not because they are taught C first, but because that's just the basic instructions we communicate actions with. Recipes, directions, instructions for how to tie your shoe laces, etc. They're all imperative
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.

Re:Misconceptions about loops in C by xigoi on Friday June 28, 2024 @04:03AM (#40818641)
Functional programming is not just about monads. I bet it’s easier to explain

    numbers.map(x => x * x)
than

    let result = [];
    for (let i = 0; i < numbers.length; ++i) {
      result.push(numbers[i] * numbers[i]);
    }
Re:Misconceptions about loops in C by bowsamic on Friday June 28, 2024 @04:50AM (#40818844)
Perhaps, but this is a somewhat contrived example. No side effects, no complicated data structures, etc. Sure, in the specific case of "parallelised" operations on an array it is simple. But anything more complicated and suddenly you're in a bad situation.

For example, what if you now want to write to a log before each multiplication, or you want to time how long each multiplication takes? Suddenly, the latter seems far preferable.

Re:Misconceptions about loops in C by xigoi on Friday June 28, 2024 @05:49AM (#40819114)

    numbers.map(x => {
      console.log(x);
      return x * x;
    })
Still easier to understand than the for loop.
Re:Misconceptions about loops in C by bowsamic on Friday June 28, 2024 @06:27AM (#40819281)
Okay but now this is just imperative lol, it's no longer functional at all, since your function is now just a sequence of instructions, i.e. it's a procedure rather than a pure function. You've just proved my point
Re:Misconceptions about loops in C by xigoi on Friday June 28, 2024 @10:17AM (#40820834)
It’s a mix of functional and imperative style. Most functional languages (other than Haskell) allow you to easily write code like this.
Re:Misconceptions about loops in C by bowsamic on Friday June 28, 2024 @01:11PM (#40822694)
Yes I know but that's not the point of my original post, which is about people who claim that a pure functional style would be considered easier if it were taught first. I'm saying that falling back on imperative is almost always easier than sticking with pure functional
Re:Misconceptions about loops in C by Akronymus on Friday June 28, 2024 @03:15PM (#40824098)
Falling back on imperative is orthogonal to what to teach first. My original argument was that, when just starting programming, it is better to go with FP, as it is easier to learn PP or OOP additionally.

While, when starting with PP or OOP, it is incredibly hard to then learn FP.

Re:Misconceptions about loops in C by bowsamic on Friday June 28, 2024 @03:46PM (#40824502)
I wasn't responding to your specific point. I was addressing this general idea that the only reason people find imperative programming easier is because they were taught it first
Re:Misconceptions about loops in C by tome on Saturday June 29, 2024 @02:57AM (#40828392)
Haskell also allows you to easily write like this

    for numbers $ \x -> do
        Console.log x
        return (x * x)
Re:Misconceptions about loops in C by bowsamic on Saturday June 29, 2024 @12:02PM (#40831512)
Okay and now you’re using monads which I pointed out are not easy to understand
Re:Misconceptions about loops in C by nequo on Saturday June 29, 2024 @06:04PM (#40833615)
Monads are not hard though. You use them every time you put a ? on something in Rust.
Re:Misconceptions about loops in C by bowsamic on Sunday June 30, 2024 @08:42AM (#40836846)
I'm not sure if this is a joke but no using something that might fulfil the laws of a monad doesn't mean you need to know what a monad is generally. Many features of imperative languages can be described using category theory, but only in Haskell do you need to actually understand the generalisation. Why is that? Why can't you just "use monads and not worry about it"? Well the problem is that in Haskell there is a huge leveraging of very general functions that apply to these entities generally, for example "fmap" ">>=" ">>" "<>", etc. and those are simple examples. Therefore understanding the rules and definition of the generalisation* is required.

It's not enough to just point at an example of a monad and say "look, the example is simple, therefore the generalisation is simple". As many philosophers have pointed out, particulars are easier to point out than universals. The latter requires a lot of thought.

Re:Misconceptions about loops in C by nequo on Sunday June 30, 2024 @09:28AM (#40837071)
I meant it sincerely, not as a joke.

> As many philosophers have pointed out, particulars are easier to point out than universals. The latter requires a lot of thought.

Fair enough, that’s a good point.

You don’t have to use >>= and >> if you don’t want to though. Use the do notation instead and the code will look much more familiar. You also mention fmap; that’s `.map` in Rust, etc. in other languages.

All I’m trying to say is, there are many parallels between Haskell and other languages at the level of particulars. The concepts behind >>= and >> are not unique to it anymore. So there is hope if you want to reconstruct the universal concepts in your mind from those particulars, especially if you use fitting abstractions like the do notation.

Re:Misconceptions about loops in C by bowsamic on Sunday June 30, 2024 @09:52AM (#40837165)
> All I’m trying to say is, there are many parallels between Haskell and other languages at the level of particulars.

Of course there are parallels, there must be for Haskell to be able to do what imperative languages can do. However that doesn't mean the abstractions are simple. "do notation" is another complicated generalisation and there's no way any Haskell programmer it going to be able to simply pretend that their do notation is a normal imperative language.

You seem to be asserting that I'm claiming that Haskell simply can't do the things that imperative languages can do, and I'm not at all and I don't understand how you inferred that.

Re:Misconceptions about loops in C by nequo on Sunday June 30, 2024 @06:08PM (#40840783)
> You seem to be asserting that I'm claiming that Haskell simply can't do the things that imperative languages can do

No, that’s not my intention. I responded to your view that monads are hard to understand by saying that they show up in other languages too in non-trivial ways.

I also don’t mean to say that Haskell is like imperative languages. Just that monads by themselves show up in imperative languages too, so they are not as esoteric as the operator names (>>=, >>) may make them seem.

I appreciate that you followed up in good faith. The point of view that you expressed makes sense to me.

Re:Misconceptions about loops in C by Iceland_jack on Saturday June 29, 2024 @04:29AM (#40828701)
I write this in my Haskell day job every day.