Hacker News new | ask | show | jobs
by hackinthebochs 4829 days ago
I'm not sure I agree with some of the examples in the article. The examples used paint declarative programming as basically abstractions over details. The problem is that there is no line where an abstraction crosses the boundary into declarative programming. It's not really about abstractions but about control flow. If your code has a specific order it has to run in, then its imperative as you're still describing the steps needed to perform the action. SQL is declarative because you're describing the output set rather than the steps to generate it. Functional languages are considered declarative because of the fact that pure functions can be rewritten, optimized, lazy evaluated, etc by the runtime. I have a hard time considering map/reduce/etc in isolation as examples of declarative programming, as they're usually used in conjuction with an algorithm that most definitely has a defined execution order.
2 comments

I agree. I don't recognize the examples here as declarative programming.

SQL and Prolog are both examples of declarative programming, so is Make to an extent. Using a map function doesn't make JavaScript a declarative programming language - it's a functional programming concept, not a declarative one.

it's a functional programming concept

That doesn't keep it from being a declarative thing.

so what would be declarative programming ? does it even exist? i mean, at some point you need to write some logic , and logic is imperative. Let's take a html file. It is declarative. but the underlying logic is written somewhere else. so you cant really have pure declarative programming? if it is possible how ?
I think the important distinction is what level of detail is the logic being passed off to the environment for translation into machine instructions. If the environment has freedom to decide exactly how to fulfill your request, then its declarative. Imperative is when your logic is specified such that there is little room for the environment to make implementation decisions.

In the case of map/reduce, if these functions are implemented in your language (say in jquery) then its not declarative as your implementation is still being specified. If you are actually talking to your environment in terms of map/reduce, then those methods are declarative. On the other hand, if we consider jquery as a part of our environment, perhaps it does make sense to consider it declarataive?

I think you're mistaking the program for the implementation. A program can be purely declarative; the fact that we need to create an imperative representation of it to run it on a computer doesn't change that.
An evaluation strategy is imperative but the logic that the strategy operates on doesn't have to be.
great point !
Yes. Read up on Prolog.