3

I am trying to get the concepts of imperative vs declarative styles through python.

From my understanding the definitions of imperative and declarative are

imperative - code all the steps in the desired outcome
declarative - code the desired outcome without the steps

For example:

would this be considered imperative?

L = []
for i in range(5):
    L.append(i*2)

and would this version be considered declarative?

L = list(map(lambda x: x*2, range(5)))
yoyoyoyo123
  • 2,362
  • 2
  • 22
  • 36
  • Declarative would be: `L = createList()`. They are both imperative to me! Your declarative example does explain it all step by step, like you imperative example does. – Elis Byberi Nov 29 '17 at 14:39
  • 1
    @ElisByberi, I don't really agree that that's declarative either -- at least not in Python, where it tells the interpreter to *immediately* run the function `createList` and assign its results to `L`; contrast to languages where that statement where `L = createList()` simply means that `L` needs to have some properties *at a later time, if and when it's evaluated*. The short form here is that Python simply *isn't* a declarative language -- one can build declarative languages on top of it, but those are separate languages, not Python. – Charles Duffy Nov 29 '17 at 15:13
  • @CharlesDuffy `L = createList()` is as declarative as it can get. All (pure) functional and logic-based programming languages are also declarative. Functions that have no side effects at all are called purely functional. However, Python is not a declarative **only** language like SQL. – Elis Byberi Nov 29 '17 at 16:22
  • @ElisByberi, as declarative as it can get *in Python*, but my point is that that's not actually meaningfully declarative at all (compared to Haskell, Prolog, CLIPS, etc). That said, one can certainly use Python to build an interpreter for a different language that *is* declarative, but that seems rather far afield from what the OP is asking. – Charles Duffy Nov 29 '17 at 16:29
  • @CharlesDuffy Yes, I do agree with you! I did provide the example to show to OP what declarative would look like (no steps at all, I just want a list). – Elis Byberi Nov 29 '17 at 16:46
  • @ElisByberi I'm sorry to say that if your example was valid, every procedural language would be declarative. – Manu Valdés May 16 '19 at 06:25
  • 1
    @CharlesDuffy Indeed! As you rightly say, Python can be used to create declarative solvers, planners or frameworks (Django views for instance), but the language itself has no declarative features. – Manu Valdés May 16 '19 at 06:28
  • @ManuValdés Common declarative languages include those of database query languages (e.g., SQL, XQuery), regular expressions, logic programming, functional programming, and configuration management systems. Python supports multiple programming paradigms, including procedural, object-oriented, and functional programming. Therefore, the example I did provide is as declarative as it can get. – Elis Byberi May 16 '19 at 12:04
  • @ElisByberi, ...however, if "as declarative as it can get" (in Python) is not something that would be widely recognized *as* declarative by people who work in declarative languages, describing it simply as "declarative" is liable to be misleading to folks coming in without prior background to understand the implications of the "as it can get" qualifier in the context where it's used. – Charles Duffy May 16 '19 at 13:18
  • @ElisByberi, it's just a call to a function. Judging by your comment I don't think you understand what declarative means. – Manu Valdés May 16 '19 at 13:46
  • @ManuValdés You may need to read (or re-read) this article [Declarative programming](https://en.wikipedia.org/wiki/Declarative_programming). It's just that simple! Remember, Haskell is a purely functional programming language but it does not make it more functional than Python. Again, Python supports multiple programming paradigms, including procedural, object-oriented, and functional programming. E.g. Java is a general-purpose programming language that is class-based, object-oriented. Also, Java is not more nor less class based programming language than Python. I'm out! – Elis Byberi May 17 '19 at 12:45
  • @CharlesDuffy Common declarative languages include those of database query languages (e.g., SQL, XQuery), regular expressions, logic programming, functional programming, and configuration management systems. That says it all. I'm out! – Elis Byberi May 17 '19 at 12:51
  • @ElisByberi, you're preaching to the choir; I'm a regular practitioner with XQuery, Datalog, Clojure (not declarative, but functional, and certainly amenable to building declarative DSLs), and an occasional dabbler with Haskell, so I don't know why you felt the need to recite dictionary definitions at me. – Charles Duffy May 17 '19 at 12:54
  • @CharlesDuffy "as declarative as it can get": You may not think it is declarative but it isn't going to be less nor more declarative than other declarative languages." Have a nice day! – Elis Byberi May 17 '19 at 13:00
  • For a countervaling argument, see http://michaelrbernste.in/2013/06/20/what-is-declarative-programming.html, describing a continuum between "less declarative" and "more declarative" languages. – Charles Duffy May 17 '19 at 13:02
  • @CharlesDuffy and other readers, https://stackoverflow.com/q/129628/2430448 – Elis Byberi May 17 '19 at 13:12
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/193528/discussion-between-elis-byberi-and-charles-duffy). – Elis Byberi May 17 '19 at 13:32

0 Answers0