6 ms·
What was actually difficult to understand about the code? I know the chained function style of haskell: let list_usa = fmap (\row -> row^._4) $ filter
by KirinDave 14y ago
What was actually difficult to understand about the code?
I know the chained function style of haskell:
let list_usa = fmap (\row -> row^._4) $ filter (\row -> (row^._1)=="USA" ) rawdata
Is a little long but this is basically the same sort of thing you see in OO languages with object.chaining.methods(with,arguments).together.forever(for(succintness { forever }).
Monoids are not complicated. They're the _easiest way to talk about combinable groups of things generically_ and lots of programs use echos of that concept when abstracting over collection objects. Monoids just finish the job by saying, "Anything which can be empty and combined associatively (think: adding integers, which also forms a monoid).
These abstractions make your life easier, not harder. But they can be a bit confusing at first because they're so universal and it can be so surprising how much milage the same code can get just by swapping monoids.
I'm porting some code from Haskell to Clojure right now for various reasons, and man do I miss monoids and the Writer monad. Producing machine-readable concatenated outputs as you go through a computation with ease is so powerful, and a lot of my time is spent writing less-powerful re-implementations of this feature.
- jlarocco 14y ago> What was actually difficult to understand about the code? Really? How about the CSV reading, from the article: import Data.Csv import qualified Data.Vector as V import qualified Data.ByteString.Lazy.Char8 as BS main = do Right rawdata <- fmap (fmap V.toList . decode True) $ BS.readFile "nukes-list.csv" :: IO (Either String [(String, String, String, Int)]) let list_usa = fmap (\row -> row^._4) $ filter (\row -> (row^._1)=="USA" ) rawdata let list_uk = fmap (\row -> row^._4) $ filter (\row -> (row^._1)=="UK" ) rawdata let list_france = fmap (\row -> row^._4) $ filter (\row -> (row^._1)=="France") rawdata let list_russia = fmap (\row -> row^._4) $ filter (\row -> (row^._1)=="Russia") rawdata let list_china = fmap (\row -> row^._4) $ filter (\row -> (row^._1)=="China" ) rawdata putStrLn $ "List of American nuclear weapon sizes = " ++ show list_usa Here's how I'd do it in Python (note I haven't tried this code, but it'd be pretty close to this): import csv countries = dict() with open("nukes-list.csv", 'rb') as f: csvfile = csv.reader(f) for row in csvfile: tmp = countries.get(row[0], list()) tmp.append(int(row[4])) countries[row[0]] = tmp print("List of American nuclear weapon sizes =", countries['USA']) How can anybody argue the Haskell version is easier to understand? The rest of the code is basically calling sum and filter on the data or calling library functions, and it would be almost exactly the same in Python, or even C++.
- jackpirate 14y agoI'm pretty sure this isn't the idomatic way to parse a CSV file in Haskell. It's just what I found from a quick Google search. I'm sure someone else could do better. The rest of the code is basically calling sum and filter on the data, and it would be almost exactly the same in Python, or even C++. This is definitely NOT true. The rest of the code is about using algebraic manipulations, like group operations on the data structures. This is certainly possible in other languages, it's just never done and it's not idiomatic.
- jlarocco 14y ago> I'm pretty sure this isn't the idomatic way to parse a CSV file in Haskell. It's just what I found from a quick Google search. I'm sure someone else could do better. Isn't that the point, though? In Haskell it was so difficult to read a CSV file that you had to Google it. In Python it was immediately obvious how it should be done.
- jackpirate 14y agoIn Python it was immediately obvious how it should be done What? I'd have to Google it in Python too.
- politician 14y agoFWIW, I really enjoyed the article, and appreciated the example dataset -- usually this literature is quite dry. Do you know if there is any monoid documentation that enumerates the various properties as something like C# interfaces and then explains what the derived types are? I think that would help me and a lot of other people who don't know as much abstract algebra or find it hard to connect with their day to day work.
- jackpirate 14y agoThanks for the kind words. Sorry, I'm not familiar with any non-functional languages that take advantage of algebra for programming.