3 ms·
Similar to 'let' (or 'letrec' in lisps), but written after the block it applies to. For example: average xs = total / count where total = sum xs
by chriswarbo 5y ago
Similar 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 )