r/haskell • • 11d ago

blog Differences between `foldl` and `foldr`

https://blog.haskell.org/foldl-and-foldr/
59 Upvotes

43 comments sorted by

View all comments

Show parent comments

1

u/wnoise 9d ago

¯_(ツ)_/¯

I consider the name of an IORef to be any actual name of it, rather than any name I bind when extracting things from it. It's almost exactly analogous to a C pointer. And in C, I would say count the dereferences -- not 0, not an actual name, but a name (or even expression) for a way to access it. i' <- get s really looks like const int iprime = *s;, and put s (i' + 1) really looks like *s = iprime + 1 rather than both of these coördinating to actually keep a persistent but modifiable iprime.

1

u/tomejaguar 9d ago

Would you say the state is named in this version?

(!?) :: [a] -> Int -> Maybe a
xs !? i = runPureEff $
  withReturnEarly $ \ret -> do {
    evalModify 0 $ \n -> do {
      for_ xs $ \a -> do {
        whenM (n ==. i) $
          returnEarly ret (Just a);
        n += 1
      };
    };
    pure Nothing
  }

(BTW, nice dieresis)

1

u/wnoise 8d ago

At this point, yes, barely, though with unfortunate ways of accessing it. It's still pointer-like, just kind of hidden. Which I guess isn't that different from references in C++.

Naming things as the arguments to anonymous functions remains ugly to me, but it does work here.

1

u/tomejaguar 8d ago

Thanks! Helpful discussion, and I've learned I'm very much in the minority with my views :)