3 ms·
I really like this quote (from [0], an intro to Category Theory)- "[Category Theory] allows one to see the forest rather than the individual trees, and offers t
by mercuryrising 14y ago
I really like this quote (from [0], an intro to Category Theory)- "[Category Theory] allows one to see the forest rather than the individual trees, and offers the possibility for study of the structure of the entire forest, in preparation for the next stage of abstraction - comparing forests."
One of the things I've noticed when programming and dealing with abstractions is how your problem gets translated to the tools of your language (what feels natural to a language). Some problems are easier in one language than another. Imagine your language being a shadow puppet, and your problem you'd like to solve is a light source. When you put your language in front of the problem, can you still see a shadow? Today most programming languages will, but if instead of binary filter with your hands, imagine your hands were some weird grayscale thing (like a monochrome LCD). Wherever the light shines through the most, you need to put a bit more work into solving the problem.
Low level languages seem to be much better at watching and reacting to state changes and making decisions on those states (because of the speed of the language, you could do 100s of checks on pins and combinations of pins and get good performance on something like an Arduino). In something like Python, those little checks become more costly. You might need to rethink your problem to get to your solution. You need to abstract from the trees, and get to the forest. The valid solution you had before because of your perspective has changed, and now you need a new approach.
I think a lot of that has to do with finding a pattern in your problem. If you can find a pattern, you can probably make it recursive, write a couple steps and maybe a helper function, and you're done. Problem solved.
Take the fibonacci sequence in Haskell (from [1]):
fib = 0:1:[a+b | (a,b) <- zip fib (tail fib)]
Start with 0 and 1, this is the pattern you use to build the fibonacci sequence. You use the sequence itself to build itself (no extra variables, you get them all in a list just starting because it evaluates itself). Get the 100th element with fib!!100. It references itself in itself, there's no states to look at, just build the sequence, when you want a value it does the legwork. The Fibonacci sequence in other languages is a little more terse - you need to define some special cases (if 0, if 1), rather than really just building the sequence starting from 0 and 1 then apply thing your pattern.
Now take something like Fizzbuzz - Python makes this extremely easy, whereas in Haskell it seems a little unnatural (at least for me, although they're pretty similar solutions) [2].
fizzbuzzers = [(3,"fizz"), (5,"buzz"), (7, "baz")]
for x in range(1, 110):
string = ''
for num, word in fizzbuzzers:
if not x%num: string += word
print string if string else x
[0] - http://shuklan.com/haskell/lec12.html#/0/2 http://shuklan.com/haskell/lec12.html#/0/2
[1] - http://www.haskell.org/haskellwiki/Haskell_Tutorial_for_C_Programmers http://www.haskell.org/haskellwiki/Haskell_Tutorial_for_C_Pr...
[2] - http://www.haskell.org/haskellwiki/Haskell_Quiz/FizzBuzz/Solution_Acontorer http://www.haskell.org/haskellwiki/Haskell_Quiz/FizzBuzz/Sol...
- tel 14y agoA more Python-similar Haskell FizzBuzz is at http://www.haskell.org/haskellwiki/Fizzbuzz http://www.haskell.org/haskellwiki/Fizzbuzz I think that's the cleanest method.
- mercuryrising 14y agoThe thing I really like about the Haskell Fibonacci and that Python FizzBuzz is the ease of scale. Want the next value of fib? Grab it. Want to put more restrictions on fizzbuzz? Add them to this list. You build the sequence with only the necessary amount of starting information (0:1 for fib, ( (3, "fizz"), (7, "buzz") ), and generate the rest as you need. These are the (I think) most beautiful solutions to problems.
- tel 14y agoThat's more extensible. I think that's the perennial problem with FizzBuzz, whether decides the spec demands generalizability or simplicity.
- jules 14y agofizzbuzzers = [(3,"fizz"), (5,"buzz"), (7, "baz")] fizzbuzz k = let str = concat [s | (n,s) <- fizzbuzzers, k `mod` n == 0] if str == "" then show k else str putStrLn $ intercalate "\n" $ map fizzbuzz [1..110] It's not that bad.