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.
Ask HN: What is the most beautiful piece of code you've ever read?
281–290 of 394 posts
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#282Haskell 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…
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 psRe: Ask HN: What is the most beautiful piece of code you've ever read?
#283This 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"
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#284This was the first artistic use of code I had ever stumbled upon (not including LOGO programs).
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#285Something 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…
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?
#286 powerSet = filterM (const [True, False])Re: Ask HN: What is the most beautiful piece of code you've ever read?
#287The simple regexp matcher by Rob Pike: https://www.cs.princeton.edu/courses/archive/spr09/cos333/be...
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#288Earlier 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.
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#289John 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
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#290Earlier 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)…