5 ms·
> … valof clauses (basically where clauses) I so wish Python had valof/where already. And it’s the one feature I was surprised not to find in OCaml after seein
by asplake 5y ago
> … valof clauses (basically where clauses)
I so wish Python had valof/where already. And it’s the one feature I was surprised not to find in OCaml after seeing it in Haskell – why isn’t it more common?
- globular-toast 5y agoWhat is it?
- Joker_vD 5y agoIt's the syntax that allows you to write x + y where y = g(x, 17) where x = f(42) instead of let x = f(42) in let y = g(x, 17) in x + y
- chriswarbo 5y agoSimilar to 'let' (or 'letrec' in lisps), but written after the block it applies to. For example: average xs = total / count where total = sum xs count = length xs
- globular-toast 5y agoAh, I see, it's the "where" that is natural in mathematics.
- asplake 5y agoRight. Bindings instead of (procedural) assignments. Perhaps: valof(<expr>) where(<bindings>)
- DarylZero 5y agobest define `average []` first
- chriswarbo 5y agoHeh; better to use a non-empty list (AKA a tuple) I suppose. It's also wildly inefficient compared to: average (x, xs) = total / count where (total, count) = Data.List.foldl' go (x, 1) xs go (total, count) x = (total + x, count + 1) In a naive interpreter, this will do one pass rather than two. A smart compiler like GHC can probably optimise away the entire list, getting a tight numeric loop (e.g. https://dl.acm.org/doi/10.1145/1291220.1291199 https://dl.acm.org/doi/10.1145/1291220.1291199 )
- c-cube 5y agoIt's not very natural in langages with strict evaluation. Haskell is a good fit because, with lazyness, the order matters less. Same thing goes with purity : what if some side effects hide in "where"?