Lurker News
Preferably an elegant snippet rather than an entire (well engineered) codebase.

Comments (380) 116 threads

Earlier comments
_xnmw#
And to think that I once got a job interview question that asked me to invent a spell-checker in 45 minutes.
yboris1 reply#
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"?

jpochtar1 reply#
Because of Norvig's spelling corrector, my measure for good code is whether or not I can understand what it's supposed to be doing, and how it's supposed to work, from reading it in one pass.

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.

yboris#
Thank you very much for your response!
segmondy#
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.
majewsky1 reply#
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 < x) primes
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]
bjoli1 reply#
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 googling.

ahakki1 reply#
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
bjoli#
The lazy sieve i have seen, taken from the paper mentioned in my post:

    primes = sieve [2..]
    sieve (p : xs) = p : sieve [x | x <− xs, x ‘mod‘ p > 0]
kmote00#
tl;dr: "Generally, whenever you need the derived class information in the base class, you are probably looking for CRTP."
yakshaving_jgt1 reply#
If you create an executable file with the following contents, and run it, it will delete itself.

    #!/bin/rm
Iwan-Zotow1 reply#
There is a better example - original /bin/true source. Presented to you below.

Load more comments