Earlier quoted context omitted.
> I get the enthusiasm for functional programming and persistant structures, etc., but at the end of the day it's just an engineering approach. One of many, and there's room to do the work to choose between them. I don't disagree with this but that's not what you said initially. You said "this is why FP sucks". I definitely think that if your work is doing tight-loops where every micro-second matters, an imperative l…
> You said "this is why FP sucks". No, I did not.
Ask HN: What is the most beautiful piece of code you've ever read?
321–330 of 394 posts
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#322Something super simple but that really entertained me when learning lisp: (loop(print(eval(read))) to have a REPL. (Just reverse the letters, easy enough to remember). That to me is elegance. It's simple yet powerful, and just 4 words really.
This is the first example I've seen in this thread that I would consider beautiful. The rest are neat tricks, but quite difficult to understand, so I wouldn't be happy to see them in production code.
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#323Re: Ask HN: What is the most beautiful piece of code you've ever read?
#324https://norvig.com/spell-correct.html Changed the way I think about code
Thank you for the share! This was excellent. Could you leave a sentence or two about how it made you "change the way [you] think about code"?
This code passes that test. Before reading it I would have never thought that could be a goal— I couldn't imagine it was possible to know what a program does without comments and documentation.
When I heard the word "readable" from others, I understood it as "parsable". From an OO context, I thought readable meant neatly formatted lines that let you say "ah, yes, this is an if statement", "this is a constructor", and so on, but with no idea what the code actually does or means to do.
Norvig's spelling corrector is better read without comments. The names of the functions tell you what you need to know— their purpose, and their implementations tell you exactly what the author thinks they mean.
It's "declarative": "def correction(word):..." means exactly "the correction of a word is ___", "def candidates(word):..." means exactly "the candidate corrections of a word are ___". There's a strong functional/LISP influence here, which I only learned later.
Because this code demonstrated it was possible, my standard for all code is that it should tell you what it does from one reading. Most code (including my own!) doesn't come close, but aiming for that goal has tons of good effects on difficult code.
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#325For me it's a BASIC one-liner which generates mazes 10 PRINT CHR$ (205.5 + RND (1)); : GOTO 10 I found this specific one via slashdot[1], but something similar, which I've never managed to find/replicate, was used to generate mazes on the Atari 800 XL at my school when I was a kid. [1] https://developers.slashdot.org/story/12/12/01/1847244/how-d...
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#326For me it's a BASIC one-liner which generates mazes 10 PRINT CHR$ (205.5 + RND (1)); : GOTO 10 I found this specific one via slashdot[1], but something similar, which I've never managed to find/replicate, was used to generate mazes on the Atari 800 XL at my school when I was a kid. [1] https://developers.slashdot.org/story/12/12/01/1847244/how-d...
There’s actually an entire book on this at: https://10print.org/
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#327Earlier quoted context omitted.
I say this as a die hard FP fan: I agree with your parent comment. I think FP is generally good enough or sometimes even quite close. But I don't know how many times I ended up writing C in Haskell or scheme to make an algorithm really fast (once all other algorithms have been tried or discarded). The quicksort example is a shitty quicksort, because it will be slow as molasses. A proper quicksort in Haskell will degr…
I'm genuinely curious; how often do you actually end up writing sorts in Haskell? I just use the built-in sort function, which is fast enough. Maybe your use-case is different than mine, but I typically use Haskell (or another functional language) for network applications, which typically don't benefit from tight-loops since the network is almost always the bottleneck anyway. For these kinds of applications, having p…
The lower level mucking around when you have to do those kinds of optimizations is, IMO, much more pleasant in imperative languages.
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#328via here: https://dfns.dyalog.com/c_life.htm
I figure that when we finally figure out the answer to the universe it will be something akin to this parsimoniousness.
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#329Bash fork bomb (do not run, this will crash your system if it's not configured properly): :(){ :|:& };:
I like the Windows one, too. It looks a lot simpler as well. %0|%0