172 points · by barrenko · 1y ago · 70 comments
I implemented the same thing myself in F#. [1]
[0]: https://citeseerx.ist.psu.edu/document?repid=rep1&type=pdf&d...
The math is a bit over my head, but this formulation seems more difficult than the one I'm familiar with. For example, x^2 is represented as 0::0::2 instead of 0::0::1 (because 2! = 2) and x^3 is represented as 0::0::0::6 instead of 0::0::0::1 (because 3! = 6). Is there a benefit to that?
f = x -> raise if
x :: int => IndexError x
otherwise => ValueError x
Complete with pipelines, of course: "> {}: {}".format "Author" "stop using stale memes"
|> printWhen working with other engineers, I’ve learned to be careful: sometimes it’s better to just materialize things into a list for clarity, even if it’s less “elegant” on paper.
There’s a real balance between cleverness and maintainability here.
Actually, it's even simpler than that: the $ operator is nothing but a function that applies its left argument to its right one! The full definition is
f $ x = f x
(plus a directive that sets its precedence and association)As to searchability, this should be covered in whatever learn Haskell material you are using. And if it isn't, then you can literally just search for it in the Haskell search engine [0].
[0] https://hoogle.haskell.org/?hoogle=%24&scope=set%3Astackage
In general, after so many decades programming, I've come to dislike languages with optional periods (a.foo) and optional parenthesis for function calls - little gain to a confusion of precedence, what's a field vs method. Seems that the whole DSL craze of 15 years ago was a mistake after all.
Having said all that, I think haskell is awesome, in the original sense of the word. I became a better programmer after working with it for a bit.
Here's one that made the front page, same time as your comment: https://news.ycombinator.com/item?id=43753381
For me, this isn't intuitive. It works; however, it doesn't scream recursion to me.
def ints():
yield 1
yield from map(lambda x: x + 1, ints())
I preferred def ints():
cnt = 1
while True:
yield cnt
cnt += 1https://math.stackexchange.com/questions/505367/collection-o...
def ints():
yield 1
yield from map(lambda x: x + 1, ints())
Surely it would always yield a stream of `1`s? Seems very weird to my brain. "As simple as that" it is not!For the second item, we grab the first item from ints(), and then apply the map operation, and 1+1 is 2.
For the third item, we grab the second item from ints(), and then apply the map operation, and 1+2 is 3.
The reason this all works is that generators plus memoization is "just" an implementation of the lazy sequences that Haskell has built in.
But there’s something beautiful in the way that a Taylor expansion or a trigonometric identity emerge from the function definition. Also, it teaches an interesting concept in lazy evaluation.
I mean, why not write straight up assembler? That would be even more efficient…
Alternatives that aren't dumb:
for x in range(2**256): # not infinite but go ahead and run it to the end and get back to me
from itertools import repeat
for x, _ in enumerate(repeat(None)): # slightly more annoying but does work infinitely
Granted these aren't clever or non-performant enough to excite functional code fanboys.If you want to code it Haskell, have you considered using Haskell?