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.)
(!) = 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)...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!
Well said - this is very much my experience as well.
(->> (command) (grep "error") sort uniq (wc :-l)
Does anyone think (wc :-l (uniq (sort (grep "error" (command)))))
is a better option? And instead of typing that out manually, I just used the REPL. user=> (use 'clojure.walk)
nil
user=> (macroexpand-all '(->> (command) (grep "error") sort uniq (wc :-l)))
(wc :-l (uniq (sort (grep "error" (command)))))
Even without macros you can still setup nice-looking chaining with generators and coroutines. "Too much nesting" seems like the wrong complaint. "C:/tmp/adinst.log" utf16 file-lines [ "SEARCH" swap start ] filter members length
File is opened into a sequence of lines, then the sequence is filtered so that only lines containing "SEARCH" remain, then members removes duplicates, then length gets the number of elements left in the sequence. In the shell, "|" is the function composition operator, in haskell it's "." In Factor, any whitespace is function composition! x -: f = f x
and boom, a Unix style pipe.(Courtesy of Learn You a Haskell, http://learnyouahaskell.com/a-fistful-of-monads, but using no actual monads).
I've played around with Elixir a bit and while I don't quite have a nail to hit with that particular hammer, it has been really fun and mind-expanding (my first functional language!).
Funnily enough, of all things bash offers an alternative paradigm. Instead of nesting, you use filters:
For example, you can write: instead of: I find this paradigm of "do A, then do B, then do C" much easier to think about than the nested "do C on the result of doing B on A". I wish more programming languages would embrace that kind of coding, which is basically functional, but in imperative or data-flow order. You have aspects of that in C# (LINQ), SQL, and some others. I know you can achive something like that in Haskell though Monads and `do`, though it feels a bit bolted on as a concession to having to do imperative stuff in a purely functional language. (And as nice Haskell is, it will probably never have the use base that bash has...)