4 ms·
compare http://haskell.org/haskellwiki/Euler_problems http://haskell.org/haskellwiki/Euler_problems - you're just unfamiliar with the functional idiom
by isomorph 16y ago
compare http://haskell.org/haskellwiki/Euler_problems http://haskell.org/haskellwiki/Euler_problems - you're just unfamiliar with the functional idiom
- Groxx 16y agoA particularly opaque example of density: http://haskell.org/haskellwiki/Euler_problems/11_to_20#Problem_18 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 ...
- sigmaxipi 16y agoCompare 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.
- deleted 16y ago[deleted]