(I hope this question is on-topic -- I tried searching for an answer but didn't find a definitive answer. If this happens to be off-topic or already answered, please moderate/remove it.)
I remember having heard/read the half-joking comment about Haskell being the best imperative language a few times, which of course sounds weird as Haskell is usually best known for its functional features.
So my question is, what qualities/features (if any) of Haskell give reason to justify Haskell being deemed the best imperative language -- or is it actually more of a joke?
I consider it a half-truth. Haskell has an amazing ability to abstract, and that includes abstraction over imperative ideas. For example, Haskell has no built-in imperative while loop, but we can just write it and now it does:
This level of abstraction is difficult for many imperative languages. This can be done in imperative languages that have closures; eg. Python and C#.
But Haskell also has the (highly unique) ability to characterize allowed side-effects, using the Monad classes. For example, if we have a function:
This can be an "imperative" function, but we know that it can only do two things:
It can't print to the console or establish network connections, etc. Combined with the abstraction ability, you can write functions which act on "any computation that produces a stream", etc.
It's really all about Haskell's abstraction abilities that makes it a very fine imperative language.
However, the false half is the syntax. I find Haskell pretty verbose and awkward to use in an imperative style. Here is an example imperative-style computation using the above
while
loop, which finds the last element of a linked list:All that IORef garbage, the double read, having to bind the result of a read, fmapping (
<$>
) to operate on the result of an inline computation... it's all just very complicated looking. It makes a whole lot of sense from a functional perspective, but imperative languages tend to sweep most of these details under the rug to make them easier to use.Admittedly, perhaps if we used a different
while
-style combinator it would be cleaner. But if you take that philosophy far enough (using a rich set of combinators to express yourself clearly), then you arrive at functional programming again. Imperative-style Haskell just doesn't "flow" like a well-designed imperative language, e.g. python.In conclusion, with a syntactic face-lift, Haskell might well be the best imperative language. But, by the nature of face lifts, it would be replacing something internally beautiful and real with something externally beautiful and fake.
EDIT: Contrast
lastElt
with this python transliteration:Same number of lines, but each line has quite a bit less noise.
EDIT 2
For what it's worth, this is how a pure replacement in Haskell looks like:
That's it. Or, if you forbid me from using
Prelude.last
:Or, if you want it to work on any
Foldable
data structure and recognize that you don't actually needIO
to handle errors:with
Map
, for example:The
(.)
operator is function composition.In addition to what other's have already mentioned, having side-effecting actions be first-class is sometimes useful. Here's a silly example to show the idea:
This example shows how you can build up computations with side-effects (in this example
print
) and then put the in data structures or manipulate them in other ways, before actually executing them.It's not a joke, and I believe it. I'll try to keep this accessible for those who don't know any Haskell. Haskell uses do-notation (among other things) to allow you to write imperative code (yes, it uses monads, but don't worry about that). Here's some of the advantages that Haskell gives you:
Easy creation of subroutines. Let's say that I want a function to print a value to stdout and stderr. I can write the following, defining the subroutine with one short line:
Easy to pass code around. Given that I've written the above, if I now want to use the
printBoth
function to print out all of a list of strings, that's easily done by passing my subroutine to themapM_
function:Another example, although not imperative, is sorting. Let's say you want to sort strings solely by length. You can write:
Which will give you ["b", "cc", "aaaa"]. (You can write it shorter than that, too, but never mind for now.)
Easy to re-use code. That
mapM_
function is used a lot, and replaces for-each loops in other languages. There's alsoforever
which acts like a while (true), and various other functions that can be passed code and execute it in different ways. So loops in other languages are replaced by these control functions in Haskell (which are not special -- you can define them yourself very easily). In general this makes it hard to get the loop condition wrong, just like for-each loops are harder to get wrong than the long-hand iterator equivalents (e.g. in Java), or array-indexing loops (e.g. in C).Contained side effects. Let's say that I want to read a line from stdin, and write it on stdout after applying some function to it (we'll call it foo). You can write:
I know immediately that
foo
doesn't have any unexpected side effects (like updating a global variable, or deallocating memory, or whatever), because it's type must be String -> String, which means it is a pure function; whatever value I pass, it must return the same result every time, without side effects. Haskell nicely separates the side-effecting code from the pure code. In something like C, or even Java, this is not obvious (does that getFoo() method change state? You'd hope not, but it might do...).There's probably a few more advantages besides, but those are the ones that come to mind.