Live data from Hacker News

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

news.ycombinator.com

281–290 of 394 posts

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

#281

Earlier quoted context omitted.

What is a maze? One could argue that if the user perceives a maze, then it produces a maze.

Maze has a starting point and a goal. The goal must be reachable from the start.

So this would be more of a labyrinth than a maze?

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

#282
post #247

Haskell allows you to define the full list of prime numbers in just two lines: module Primes where primes = 2 : filter isPrime [3..] isPrime x = all (\y -> mod x y /= 0) $ takeWhile (\y -> y * y The beauty of this is that it's self-referential. The list `primes` is built by filtering all the natural numbers for prime numbers, using the predicate `isPrime` which itself uses the list `primes`. The only caveat is that w…

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

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

#283
post #264

This is very common in Elm but it blew my mind after years of programming in Python. In Python it is very easy to raise an IndexError by getting an element from a list by index that doesn't exist. eg. # python names = [] names[0] # In Elm you are forced to always consider this possibility. # Elm names = [] case List.head names of Just name -> name Nothing -> "empty"

Idiomatic python: v = names[0] if names else "empty"

Yes, but the point is you can forget the check in python but not in Elm.

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

#285
post #266

Something 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.

Nice. I immediately had to try the same in PHP. To make a working repl with newlines in the output etc, this is what I a came up with: while(1) {eval(fgets(STDIN));echo "\n";}; I then tried it on the command line like this: php -r 'while(1) {eval(fgets(STDIN));echo "\n";};'; Hurray, it prompted me for input! So I typed: for ($i=0;$i Which got me: 0123456789 So far so good. I wondered: Can we now run the repl in the r…

It is that simple! It has practical benefits. For example, if you can call that code from somewhere specific in your program, which is handy for debugging. If your `eval` function takes a lexical environment, you can also implement a debugger easily.

To test whether your logic is working properly, make your repl print out a different character when it prompts for input. For example, the toplevel repl can print out "> " whereas the inner repl prints out "repl> "

For bonus points, your repl should exit if you type Ctrl-D. That way you can go from the inner repl to the outer repl, and from there it should exit your program.

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

#288

Earlier quoted context omitted.

No, it does not generate a maze. It generates a random sequence of \s and /s, that can trigger a maze being generated in a typical user's mind. This is the art of illusion.

What is a maze? One could argue that if the user perceives a maze, then it produces a maze.

I perceive a maze in http://tromp.github.io/pearls.html#maze

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

#289
post #7

John Carmack's Fast Inverse Square Root: https://en.wikipedia.org/wiki/Fast_inverse_square_root#Overv... . The first time I really and truly felt that people approach problems differently from how I, by default, go about them.

CPUs have much faster (2-5 cycles latency) and much more precise ( https://www.felixcloutier.com/x86/rsqrtps

I wonder which one used less energy in 1999?

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

#290
post #90
post #82

Earlier quoted context omitted.

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…

> It silently transforms an O(1) space algorithm into an O(n) space one, and adds an enormous constant time overhead. Please tell me what imperative quicksort algorithm has O(1) space. All versions I've seen and could recall use recursion; although each recursive call uses O(1) space, in the worst case of bad pivot element selection each recursive call would only really sort one element resulting in a worst-case O(n)…

The path towards O(1) space instead of O(log n) space is fraught with technicalities. At the bottom, you realize that no matter what algorithm you use, you always store the array size in lg(n) bits! So it makes more sense to say “less memory” rather than O(1) memory for sorts.
Post reply on HN