← Back to context

Comment by Dylan16807

12 days ago

Is there a benefit if you're already familiar with writing functions like that? Is it wrong for me to expect that most programmers are already familiar with functions that only use their inputs, but treat that style as significantly more optional?

I wrote pure functions for a minute there but that's not the same, a function that only uses its inputs can modify an object while a pure function would have to return a new object. But, similarly, I bet that a lot more people know about pure functions than have any working knowledge of Haskell.

It seems you only focused on one of the conditions I mentioned.

You have to follow both rules: the one about inputs and the one about outputs.

This is like a contract. If you enforce it throughout your program, you gain some guarantees about your program as a whole.

  • I was looking at both rules, and specifically I was using the long version where you said "it doesn't assign them to some other global variable that you have to track down". If you pass in a mutable object then that's not "some other global variable".

    If I interpret "It only outputs its results" in a very strict way, that still allows having output and in/out parameters. The latter of which can break purity.

    Though you can break purity with just inputs:

      define f(o): return o.x
      let a = {x=1}
      f(a)
      a.x = 2
      f(a)
    

    If you meant to describe pure functions then that's fine, that's why I addressed pure functions too, but I don't think your original description was a description of pure functions.

    • So, another definition of a pure function is that, for a particular input it will always return the same output.

      Your example respects the rule:

          f({x=1}) == 1
          f({x=2}) == 2
      

      But it's true that the two rules I gave are not enough to make a function pure. Because I didn't say anything about I/O. So, a function that follows the rules about inputs and outputs, could still do I/O and change its outputs based on that.

      Starting from the question that gave birth to this whole thread: "What's the benefit of learning a PURE functional programming language..."

      The other benefit is that such a language forces you to be explicit about I/O. It does it in such a way that even functions that do I/O are pure. The good part is that, if you use it long enough, it can teach you the discipline to be explicit about I/O and you can use this discipline in other languages.

      For example, this is how I see this principles being used in Python:

      https://elbear.com/functional-programming-principles-you-can...

      11 replies →

> Is it wrong for me to expect that most programmers are already familiar with functions that only use their inputs

They'll experience no friction when using Haskell then. Haskell only refuses to compile when you declare "Oh yeah I know functions from other languages this is easy" but then do some mutation in your implementation.

  • > They'll experience no friction when using Haskell then.

    The question was what benefit you'd get from learning a functional language, though. Existing knowledge making it easier to switch to a functional language is the inverse of that.

    And there's no assumption they'll actually be making things in Haskell, so easy switching isn't by itself a benefit.

    • Yeah I can't really follow these threads.

      I saw:

      > What's the benefit of learning a PURE functional programming language, opposed to just using a language which has adapted the best bits and pieces from the functional programming paradigm?

      I also saw:

        Though you can break purity with just inputs:
      
        define f(o): return o.x
        let a = {x=1}
        f(a)
        a.x = 2
        f(a)
      

      I don't know if that's the tail-end of a reductio ad absurdum which is trying to demonstrate the opposite of what it stated. Either way, to be clear, the above would be rejected by Haskell (if declared as a pure function.)

      I guess if you learn a functional language "which has adapted the best bits and pieces from the functional programming paradigm" then you might think that the above is broken purity, but if you learn a "PURE functional programming language" then you wouldn't.

      1 reply →