Hacker News new | past | comments | ask | show | jobs | submit login

A particularly opaque example of density: http://haskell.org/haskellwiki/Euler_problems/11_to_20#Probl...

  problem_18 = head $ foldr1 g tri 
  where
    f x y z = x + max y z
    g xs ys = zipWith3 f xs ys $ tail ys
    tri = [
        [75],
        [95,64],
        [17,47,82],
        ... snip ...



Compare this with two python solutions. The first was when I was learning python and is similar to how I would solve it in C. The second is a copy of that haskell solution:

  tri = [[75],[95, 64]...]
  maxes = [x[:] for x in tri]
  for y in range(len(maxes)-2, -1, -1):
    for x in range(len(maxes[y])-1, -1, -1):
      maxes[y][x] = maxes[y][x] + max(maxes[y+1][x], maxes[y+1][x+1])
  print maxes[0][0]

  print reduce(
    lambda y, x: map(
      lambda(t): t[0] + max(t[1], t[2]),
      zip(x, y, y[1:])),
    tri[::-1])[0]
I find the first one much easier to comprehend even though they do the same thing and use the same general algorithm of reduce()ing the rows. In general, I find procedural code much easier to read when I take a look at it months down the line.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: