Live data from Hacker News

Ask HN: What is the most beautiful piece of code you've ever read?

news.ycombinator.com

351–360 of 394 posts

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#352
post #82
post #24

The one that blew my mind when I was in college was a simplified version of quicksort in Haskell. It's just so elegant and clean. quicksort :: Ord a => [a] -> [a] quicksort [] = [] quicksort (p:xs) = (quicksort lesser) ++ [p] ++ (quicksort greater) where lesser = filter ( = p) xs Now surely someone may come along and point out how this isn't a true quicksort[0] because it doesn't partition the elements in place, but…

Quick sort comes with a steep penalty. Worst case is O(n^(2)). The reason quicksort is good is because it's in place. Once you throw away the in place aspect of quick sort, it's straight up bad. This implementation of quicksort is actually a great example of why functional programming sucks. It silently transforms an O(1) space algorithm into an O(n) space one, and adds an enormous constant time overhead. Algorithms…

This is why Python uses Timsort: https://en.wikipedia.org/wiki/Timsort

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#353
post #282
post #247

Earlier quoted context omitted.

It is also dirt slow. This is just trial division, done in a lazy fashion. At least it is better than the one some haskellers call the sieve of erathostenes (it isn't the sieve of erathostenes), which is so god-awful that I almost vomit every time I see it. The real lazy sieve of erathostenes is a thing of wonder! There is a paper describing it called "the genuine sieve of erathostenes" and it can be found by googlin…

this? sieve :: Integral a => a -> [a] sieve l = sieve' [2..l] [] where sieve' (p:ns) ps = sieve' (filter (\x -> rem x p /= 0) ns) (p : ps) sieve' [] ps = reverse ps

The lazy sieve i have seen, taken from the paper mentioned in my post:

    primes = sieve [2..]
    sieve (p : xs) = p : sieve [x | x  0]

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#355
post #24

The one that blew my mind when I was in college was a simplified version of quicksort in Haskell. It's just so elegant and clean. quicksort :: Ord a => [a] -> [a] quicksort [] = [] quicksort (p:xs) = (quicksort lesser) ++ [p] ++ (quicksort greater) where lesser = filter ( = p) xs Now surely someone may come along and point out how this isn't a true quicksort[0] because it doesn't partition the elements in place, but…

When it comes to Haskell, this function that generates all the fibonacci numbers did it for me: fibs = 0 : scanl (+) 1 fibs

Another beaut!

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#356

If you create an executable file with the following contents, and run it, it will delete itself. #!/bin/rm

There is a better example - original /bin/true source. Presented to you below.

Compare that with the version in GNU Coreutils[1] that is 50+ lines long so that false[2] can be written as:

#define EXIT_STATUS EXIT_FAILURE

#include "true.c"

[1] https://github.com/coreutils/coreutils/blob/master/src/true....

[2] https://github.com/coreutils/coreutils/blob/master/src/false...

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#357
post #348

Earlier quoted context omitted.

Nice to see confusing a joke reference to a regional fact (as to things that can seem X but be Y) with transphobia. Anybody told you that merely referencing trans women in Thailand is opposing them? Projecting much?

I'm amazed that you're defending that comment.

Well, I am amazed that one can be so rude.

First, you come blazing with accusations of transphobia, because of a joking mention of Thailand's ladyboy scene, to challenge the parents' notion that everything is WYSIWYG. Do you even know me? Or you have a hobby of randomly assuming things about people you don't know?

Second, when I don't take that quietly, you add the above comment about having the gal to defend myself, going for the "This animal is extremely vicious. When attacked, it defends itself" angle...

Perhaps you can go even lower against some stranger on the internet, but you wont be getting any replies from me in this thread.

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#358
post #327

Earlier quoted context omitted.

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…

I have had a couple of times where the overhead of idiomatic Haskell warranted rewriting it in a mutable imperative way. Sorting was never a problem, but things like functional heaps (leftist or pairing heaps) will never be as efficient as mutable, cache aware ones. The lower level mucking around when you have to do those kinds of optimizations is, IMO, much more pleasant in imperative languages.

Sure, I won't argue with that. I've never used Haskell for that low-level of work, so I can't speak with any kind of expertise on that; I've never found Haskell's FFI to be terribly hard to use though, so you could conceivably get the best of both worlds.

Also, have you tried Liquid Haskell? It uses refinement types to let you use the "unsafe" and fast versions of functions to guarantee correctness while also increasing performance.

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#360

https://norvig.com/spell-correct.html Changed the way I think about code

I completely forgot about this. This code is very beautiful too. Norvig doesn't take a bruteforce approach, his codes are worth studying, his sudoku solver is just as good too.
Post reply on HN