Live data from Hacker News

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

news.ycombinator.com

221–230 of 394 posts

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

#221
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 we need to encode that 0 and 1 are not prime numbers, but 2 is a prime number, to provide the base cases for the recursion. Furthermore, `isPrime` uses that each non-prime number has at least one prime factor less than or equal to its square root, to ensure that it only needs to look at a finite number of possible prime factors.

If you have GHC in your repo, you can test this by putting it in a file and running `ghci` with the file as the only argument. It will give you a REPL where `primes` and `isPrime` are in scope:

  *Primes> isPrime 200
  False
  *Primes> take 10 primes
  [2,3,5,7,11,13,17,19,23,29]

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

#223
post #6

For me, the answer is - The code that never existed. Not to sound cheeky but eliminating code, is a beautiful thing. Less code is easier to maintain, understand, and faster to run. So the less code you can achieve, the better overall the software will be.

Reminds me of a saying I read from moderngpu library's wiki page, a highly optimized yet highly readable GPU basic primitive library: "Software is an asset, code a liability. On good days I'd add 200 or 300 lines to this repository. On great days I'd subtract 500." source: https://github.com/moderngpu/moderngpu/wiki/Introduction

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

#224

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.

> One could argue that if the user perceives a X, then it produces a X.

This can be stretched to turn anything into anything else. For example, why can't this comment be a maze? In that case 10 REM; 20 END is my even shorter, more elegant maze program because I see a maze in it.

Some constraints that are typically implied when people say "computer generated mazes":

- They are solvable (have a start and end)

- OR they loop endlessly with no dead ends.

- Walls and spaces consume 1 element on the grid.

- No space on the grid is surrounded by all 4 NWSE walls.

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

#225
From HAKMEM, Item 172:

  CONS: EXCH A,[EXCH A,[...[PUSHJ P,GC]]]
        EXCH A,CONS
This allocates a cons cell for Lisp on PDP-10, by using the first instruction in the routine as the head pointer of the free list. The first instruction puts the first word of the free list into A (and then clobbers that word with the original contents of A, initializing the cons cell). The second then swaps the first instruction with the first item in the free list.

The free list is simply a linked list of first instructions, which could be done because the PDP-10 was a 36-bit machine with an 18-bit address space, and the address field of an instruction was the entire right half: the same part of a word that was used as a pointer. So, when interpreted as a pointer, each of these instructions was just the pointer to the next cell.

The beautiful part to me is that, once you ran out of free list, the last word was a call to the garbage collector, which would build a free list of unreferenced cells and return a pointer to the second cell (with the correct opcode field) in A; the second instruction would then finish the cons operation, leaving the address of newly allocated cons cell in A.

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

#226

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.

Jeopardy this: Only then can you realize, there is no maze.

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

#227

Earlier quoted context omitted.

> In almost every other language this code would look messy or use some terrible recursion. Nah, lots of other languages can do this. Python: def factorial(i): return reduce(operator.mul, range(2, i+1), 1) Ruby: def factorial(i) (2..i).reduce(1, :*) end Haskell: factorial n = foldl (*) 1 [2..n]

For Python, we don't need to supply the optional initializer 1. And reduce resides in the functools namespace (much like mul is in operator). I assume we're not talking about that peculiar dialect of Python that is no longer supported a few months from now.

Not only that but in python 3.8, it can be written:

    def factorial(n):
        return math.prod(range(1,n+1))

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

#228

Earlier quoted context omitted.

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

> One could argue that if the user perceives a X, then it produces a X. This can be stretched to turn anything into anything else. For example, why can't this comment be a maze? In that case 10 REM; 20 END is my even shorter, more elegant maze program because I see a maze in it. Some constraints that are typically implied when people say "computer generated mazes": - They are solvable (have a start and end) - OR they…

So boring.

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

#229
post #207

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.

A maze does not have to be solvable.

Ah, you mean a Unsolvable Maze: https://www.sickchirpse.com/janitor-creates-completely-unsol...

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

#230
One of the first recursive algorithms I learned was the solver for the Tower of Hanoi problem. I was stunned about its simplicity, solving a problem most people couldn't with just a few lines of code:

- https://en.wikipedia.org/wiki/Tower_of_Hanoi#Recursive_solut...

Post reply on HN