-2

I have read some sources where the Haskell's paradigm is described as functional but imperative paradigm. The main source where this is said is Wikipedia. How is possible a functional and imperative paradigm at the same time, or is this a mistake?

Iván Cortés
  • 581
  • 1
  • 9
  • 22
  • 1
    That was [only quite recently added to Wikipedia](https://en.wikipedia.org/w/index.php?title=Haskell_(programming_language)&diff=733757128&oldid=732884393), and the reason given is that “the official wiki states it”... but without a concrete reference. [The HaskellWiki main article says no such thing](https://wiki.haskell.org/Introduction). I think this addition is just wrong and the Wikipedia article should be backrolled, any opinions? – leftaroundabout Apr 15 '17 at 13:14
  • 1
    Things are never black and white. Your question reminds me of [this great cartoon by Larsson](http://68.media.tumblr.com/tumblr_lyiqu5VFK71qz6f4bo2_500.jpg). –  Apr 15 '17 at 13:20
  • 1
    @leftaroundabout It would be very disingenuous to claim that Haskell is **definitely not** an imperative programming language, don't you think? –  Apr 15 '17 at 13:22
  • @Boris I am learning functional languages, said Lisp or Erlang. Maybe I am thinking as you noticed with cartoon. But, how should I enrolled this paradigm, if almost each languages can have an _imperative_ order in its sentences? – Iván Cortés Apr 15 '17 at 13:34
  • 1
    Your English is a bit unconventional; anyway, keep on reading and learning and very importantly _writing code_, and things will slowly become less absolute. –  Apr 15 '17 at 13:36
  • Sorry and thanks for your advice! – Iván Cortés Apr 15 '17 at 13:46
  • @Boris "Imperative" and "declarative" are just terms used to classify languages, more by a general feel than by any really hard precise meaning. If you end up calling Haskell "an imperative language" (as opposed to calling some particular Haskell programs imperative), you've pretty much broadened the term so much it applies to any language, and so isn't a very useful classification. – Ben Apr 15 '17 at 14:21
  • @Boris no, I don't think that would be disingenuous, since the Haskell language itself includes _nothing_ that actually can be used for doing anything imperative. What's true is, the Haskell base library includes a standard type (`IO`) whose values can be understood as imperative programs... but then, C includes a `char` type and arrays of chars can be understood Prolog programs. Still it wouldn't be disingenuous to say that C is _definitely not a logic programming language_. – leftaroundabout Apr 15 '17 at 14:32
  • @leftaroundabout Indeed. I have just undone the change at Wikipedia. – duplode Apr 15 '17 at 14:51
  • @leftaroundabout I know that Haskellers get quite excited when it comes to classifying things. It might have to do something with the type system; I can't know this for sure of course. –  Apr 15 '17 at 15:47
  • 3
    I think an important question is... "why is this question important?"! That's to say, what is the questioner really asking here, and why do they want to know it? Haskell is declarative. Computer programs are, though, by definition imperative. The Haskell language has some things that look like sequenced commands, but they're really declarations of expressions *about* commands, and only get turned into imperative code by the compiler later. An action like `putStrLn "hi"` is not a command, and doesn't *do* anything. This might be helpful: http://www.happylearnhaskelltutorial.com/1/main_road.html – Julian Leviston Apr 15 '17 at 23:26
  • @JulianLeviston when you talk to a compiler about printing, you still intend for stuff to get printed. If you talk about a mutable cell getting a new value, you intend for it to get a new value. C code can be viewed as us talking to a C compiler just as well. Monads are *embedded* DSLs, so any monadic code *is* part of Haskell by definition, especially for "standard" monads. Haskell is really two languages, functional and imperative at the same time, they are just well separated. And if Haskell *were* declarative, `null` would be *The Same* as `((==0).length)`. But it isn't. – Will Ness Jul 14 '17 at 09:22
  • @WillNess I'm not sure why you think they'd be the same thing. Haskell **is** declarative. You write programs by declaring definitions for terms, and Haskell builds an executable based on your declarations. That its type system doesn't capture the shape of the computation fully is largely irrelevant. The semantics you encode when writing programs **are**, *nevertheless* encoded in declarative definitions. – Julian Leviston Jul 14 '17 at 10:31

3 Answers3

0

Functional languages generally use a declarative methodology when describing their programs, and Haskell is definitely one of these languages; Haskell programs are described as a set of declarations of values (including functions, because functions are first class values).

However, in order for a computer to execute a program, it has to follow a sequence of instructions. To that end, Haskell has a way you can describe what can end up as sequencing when it's executed, in a special syntax (called "do syntax"). Each line of this syntax looks like the next "statement" in a normal imperative language, and kind of behaves that way, too. Really, though, it's just syntactic sugar for ordinary Haskell expressions of function application.

Because it's still written in Haskell, though, and Haskell values and expressions are for the most part typesafe, Haskell is often called "the best imperative language".

Julian Leviston
  • 1,646
  • 10
  • 21
  • Having this, is difficult for me thinking in a language programming which has no a imperative way. Despite this, wikipedia describe Haskell as `purely functional programming`, how this goes with your answer? – Iván Cortés Apr 15 '17 at 13:17
  • 1
    There is indeed some truth to “Haskell is the best imperative language”, but it's obviously meant ironically. I'd say Haskell itself is declarative through and through, not imperative at all; only `IO` and `ST` are imperative eDSLs which happen to be easily described (declaratively!) in Haskell. – leftaroundabout Apr 15 '17 at 13:21
  • I don't think it's difficult. Haskell is both. When you assign a value to `main` (the entry point of a Haskell program), you're describing — declaratively — what the program should do when executed. You're not giving the compiler statements, you're giving it declarations. However, those declarations can be written in a sequenced way. If you use `do` syntax to connect up and "sequence" three `printLn` expressions, then you're telling the Haskell compiler to build a single expression out of them, in sequence. While actually declarative in truth, it's imperative in effect. – Julian Leviston Apr 15 '17 at 13:32
  • The difference is like between say me giving you a list of instructions where you've agreed to perform each instruction one after another, versus giving you a list of values and expressions in any order some of which use other values and expressions, and a single instruction saying "do this thing" (which happens to use all the other expressions). Haskell is not imperative. However, to the end programmer programming with `IO`, it can sure seem like it sometimes (and it really helps to know your program is mostly type-safe, and using immutable data because it keeps lots of simple errors out). – Julian Leviston Apr 15 '17 at 13:39
0

As long as changing the order of the atomic parts of a program can change its output, you can safely smack "imperative" on the language. Almost any high-level language is "declarative" in some way, and there are languages that support "declarative programming" better than Haskell does.

In C, for example, enums and even the switch can be "declarative", if you use it in a certain way. Much further in the "declarative" direction is Prolog, and you could write programs in a declarative style, but again, you can also use Prolog to write an imperative program.

0

Declarative, but there is some caveat to that.

Alonso Church created lambda calculus which Haskell is based on, and Alan Turing created the turing machine. Lambda calculus is purely functions, a turing machine changes the state of a program. Sounds familiar?

Except, it has been shown that these two are equivalent. You can build a turing machine from lambda calculus and vice versa. Which means everything inside Haskell is really just functions, but from them you can build an imperative style of programming.

So yeah, declarative. You don't tell Haskell how to do things, you tell it what a thing is and then build from it.