4 ms·
"I know you can achive something like that in Haskell though Monads and `do`" The closer equivalent to UNIX pipelining in Haskell is actually the simple "." op
by jerf 13y ago
"I know you can achive something like that in Haskell though Monads and `do`"
The closer equivalent to UNIX pipelining in Haskell is actually the simple "." operator, function composition. That pipeline would generally look more like
countLines = wc LineCount
countLines . uniq . sort . grep "error" $ mycommand
It's not a direct translation, because that is exactly neither legal Haskell (I'm munging program vs. some sort of shell here), nor exactly how you'd really do that in Haskell, however, pipelines of several functions composed together is idiomatic and frequently-used.
"though it feels a bit bolted on as a concession to having to do imperative stuff in a purely functional language"
It really isn't. IO is only one particular monadic thing; check out Parsec for some more fun. It only looks bolted on if you look very superficially, and you darned near have to bring the preconception in the first place to even briefly come to that conclusion. That idea basically comes from the misconception that "Haskell can't do side effects", then "Oh, well, this way of doing side effects must be a bad idea or something"... the first statement is false. Honestly, I miss the way Haskell does IO in other languages far more than I miss the way other languages do IO in Haskell. (To be clear, I do have both feelings, but the former is way more common.)
- thinkpad20 13y agoFor what it's worth, I often do something like this: (!) = flip ($) Which then allows me to do things like mycommand ! grep "error" ! sort ! uniq ! wc lineCount Which is nice because it visually resembles `|` (which is a reserved symbol in Haskell). Similarly for fmap: (<!>) = flip (<$>) result <- get <!> table <!> lookup "foo" print result ! liftIO Or for function composition: (~>) = flip (.) doStuff = reverse ~> head ~> negate ~> print doStuff [1,2,3] That being said, lisp does seem far more suited to shell programming than Haskell, because every function is variadic. What would probably be nice (and probably has been done) is a Lisp with syntax extended to include a few symbols to allow bash commands like |, >, etc: $ def nth-line (n) (read a | head -(n) | tail -n 1) $ ssh -i .ssh/some.pem (python show_servers.py | grep 'cool server' | cut -d ' ' -f 3 | nth-line 1)
- mcguire 13y ago"It only looks bolted on if you look very superficially, and you darned near have to bring the preconception in the first place to even briefly come to that conclusion." ...or read any of the older Haskell books and tutorials, which all seemed to start in pure expression-land and attempt to stay there as long as possible (which is good programming style, don't get me wrong; but learning is different). IO and other monadic things are treated like huge and scary monsters that must be left until the very last possible moment, if treated at all, because they're HUGE AND SCARY!!!!1! Aaaaahhhhhh!
- dllthomas 13y ago"Honestly, I miss the way Haskell does IO in other languages far more than I miss the way other languages do IO in Haskell. (To be clear, I do have both feelings, but the former is way more common.)" Well said - this is very much my experience as well.