News for hackers. Stuff that matters.
Re:I learned Haskell in just 15 years by elbear on Tuesday June 18, 2024 @12:32AM (#40714094)
I gave it some more thought.

I now believe that learning a language like Haskell (or Elm or PureScript) forces you to see your program as pipes that you fuse together.

It's not just functions. Haskell has only expressions and declarations. That means, for example, that you are forced to provide an `else`, when you use `if`. The idea is that you have to keep the data flowing. If a function doesn't provide a meaningful value (so it returns nil, None), you have to handle that explicitly.

And, btw

> Your version differs from mine on that aspect. It passes two unrelated objects.

Those two objects are not unrelated. They have the exact same structure (an attribute named "x"). So they could be considered two values of the same type.

Re:I learned Haskell in just 15 years by Dylan16807 on Tuesday June 18, 2024 @01:05AM (#40714228)
I mean that the identity is unrelated. Yes, you can say they're the same type. But I'm actually passing the same object in. If f evaluated lazily, it could return 2 from both calls. Something like:

  define f(o): return o.x
  let a = {x=1}
  n = f(a) // n is not evaluated yet
  a.x = 2
  m = f(a)
  return n + m // returns 4
Re:I learned Haskell in just 15 years by elbear on Tuesday June 18, 2024 @02:15AM (#40714570)
Ok, you're probably proving the point that purity also requires immutability. I'm not sure, as I haven't considered all the implications of Haskell's design.

My two rules about inputs and outputs are more like heuristics. They can improve code organisation and probably also decrease the likelihood of some errors, but they don't guarantee correctness, as you're pointing out. They're shortcuts, so they're not perfect.

Edit: If I remember right, it's laziness that requires immutability. I think I read something about this in the Haskell subreddit as an explanation for Haskell's design.

Re:I learned Haskell in just 15 years by Dylan16807 on Tuesday June 18, 2024 @12:03PM (#40719374)
Even without laziness, you can get similar problems if f creates a closure or returns something that includes the parameter object.
Re:I learned Haskell in just 15 years by trealira on Tuesday June 18, 2024 @12:09PM (#40719433)
Yeah, this is a common source of confusion with closures in Python. Example on Stack Overflow:

https://stackoverflow.com/questions/233673/how-do-lexical-cl...

Re:I learned Haskell in just 15 years by elbear on Wednesday June 19, 2024 @11:35AM (#40729243)
Doesn't that example also show a kind of laziness?

I say this because the second solution to that question offers the solution of using `i` as a default argument when defining the function. That forces its evaluation and fixes the problem.

Re:I learned Haskell in just 15 years by trealira on Wednesday June 19, 2024 @06:07PM (#40732940)
It's just a name shadowing.

Copying the code they wrote:

  for i in xrange(3):
      def func(x, i=i): # the *value* of i is copied in func() environment
          return x * i
      flist.append(func)
That could also be written "def func(x, foo=i): return x * foo". It's just copying i's value to another variable. In the next line, i's value is still 1, 2, or 3, so when the function is called during the next line of the body of the loop, the value held by i is bound to foo.

It's not evaluating a thunk representing i, which is how lazy variables are evaluated in Haskell.

Re:I learned Haskell in just 15 years by elbear on Thursday June 20, 2024 @01:51AM (#40735356)
Ok, I had a better look at the code and I realised that it doesn't follow the rule I was talking about, namely having the function only work on values it receives as inputs.

I think that's why I don't use closures, because they read values from the environment. Their only use case (that comes to mind) can be solved with partial application, which is safer.

Oh, and I wasn't using laziness in the Haskell sense, but more in the general sense of deferring evaluation.

Re:I learned Haskell in just 15 years by elbear on Wednesday June 19, 2024 @11:41AM (#40729311)
Ok, the Wikipedia definition of pure function is more strict and than what I was saying and I think it covers the issues you mentioned:

https://en.wikipedia.org/wiki/Pure_function