Imperative vs. Declarative
latentflip.com
Imperative vs. Declarative
1–10 of 53 posts
Re: Imperative vs. Declarative
#2Re: Imperative vs. Declarative
#3In imperative style, most of your mistakes or carelessness will usually mean that the machine makes a wrong result or crashes in the process - a bad 'what'.
In declarative style, most of your mistakes or carelessness will usually mean that the machine will take a bazillion times less efficient way trying to make that result, possibly taking 'forever' or running out of memory - i.e. a bad 'how'.
Re: Imperative vs. Declarative
#4>But we also get to think and operate at a higher level, up in the clouds of what we want to happen, and not down in the dirty of how it should happen.
The author mentions this at the end, but I feel it should be stressed more strongly: The dirty of how is important. The author presents a big "if" here, which is: if the function we've built to abstract away some ugliness performs in the best, most efficient way possible, with no drawbacks, then, yes, abstracting that functionality away and forgetting it is okay.
But to me that's a big if. It is just as important to me to understand and recognize that map is fast, efficient, and to understand why it's fast and efficient, so that someday, if you come across a situation where map does not apply, you will know why, and you'll be able to use something better.
Being up in the clouds all the time is, to me, a pipe dream -- we must always be cognisant of the ground on which we built this tower of abstraction layers.
Re: Imperative vs. Declarative
#5On the flip side, it also drastically changes the typical errors. In imperative style, most of your mistakes or carelessness will usually mean that the machine makes a wrong result or crashes in the process - a bad 'what'. In declarative style, most of your mistakes or carelessness will usually mean that the machine will take a bazillion times less efficient way trying to make that result, possibly taking 'forever' o…
That said, the "why did it choose that terrible implementation?" problem does occasionally come up in declarative programming, and inherently never comes up in imperative.
Re: Imperative vs. Declarative
#6In Prolog you ask questions. For example: subset([1,2],[2]).
then it goes away and says "yes". Or you want to know if any subsets exist: subset([1,2],B).
B = [] B = [1] B = [2]
This makes it really really nice for some surprising tasks (Windows NT used to ship with a prolog interpreter for setting up the network)
Re: Imperative vs. Declarative
#7Prolog is declarative programming take to the maximum (excluding things like Answer Set Programming/clingo etc). In Prolog you ask questions. For example: subset([1,2],[2]). then it goes away and says "yes". Or you want to know if any subsets exist: subset([1,2],B). B = [] B = [1] B = [2] This makes it really really nice for some surprising tasks (Windows NT used to ship with a prolog interpreter for setting up the n…
I worked through the 7 languages in 7 weeks book, and solving a sudoku with prolog blew my mind. I think the first "real" programming I did was a sudoku solver in Excel and VBScript (yeuch).
Re: Imperative vs. Declarative
#8Re: Imperative vs. Declarative
#9Prolog is declarative programming take to the maximum (excluding things like Answer Set Programming/clingo etc). In Prolog you ask questions. For example: subset([1,2],[2]). then it goes away and says "yes". Or you want to know if any subsets exist: subset([1,2],B). B = [] B = [1] B = [2] This makes it really really nice for some surprising tasks (Windows NT used to ship with a prolog interpreter for setting up the n…
double([], []).
double(H | T, H2 | T2) :- H2 is H * 2, double(T, T2).Re: Imperative vs. Declarative
#10The fact that, with unification and backtracking, you can not only get a result for a query, but also "pass a variable" as an argument and get a possible value makes it seem much more like a mathematical expression and less like a computation.
For example, I can define some relations:
parent_of(john, mary).
parent_of(mary, eve).
grandparent_of(X, Y) :- parent_child(X, Z), parent_child(Z, Y).
And then I can simply run a query: ?- grandparent_of(john, eve).
Yes
But I can also make it fill in the value for me: ?- grandparent_of(john, X).
X = eve
'grandparent_of' is not some piece of code, it's an actual declaration of a relation between the terms.Of course, you can do unification and backtracking in other languages, but Prolog is designed for it.