Hacker News new | ask | show | jobs
by bnegreve 4446 days ago
According to Wikipedia, functional programming is an example of declarative programming [1], which is mentioned in the article.

This is not immediately obvious to me, here's why (from the same wikipedia article [1]):

While functional languages typically do appear to specify "how", a compiler for a purely functional programming language is free to extensively rewrite the operational behavior of a function, so long as the same result is returned for the same inputs. This can be used to, for example, make a function compute its result in parallel, or to perform substantial optimizations (such as deforestation) that a compiler may not be able to safely apply to a language with side effects.

[1] http://en.wikipedia.org/wiki/Declarative_programming

1 comments

Seems pretty obvious to me, since the above description could also apply to your typical declarative languages such as SQL, where you also rely on the database to do optimizations based on a description of the result you're looking for, rather than procedures for calculating it.
Just to clarify, it wasn't obvious to me before I read the part of the article that I have quoted.

But I still think that functional programming goes beyond simply specifying "what you want". Take the problem of sorting an array for example. A declarative specification of the sorting problem would be: Given an array T, compute a permutation of T such that for all i, it's true that T[i] < T[i+1] (some languages support this type of specification e.g. [1]).

Clearly here, you don't describe "how to do it". In functional programming you can't do this, you have to explain "how", implementing quicksort, for example.

[1] http://www.minizinc.org/

IMO the way that functional programming is declarative is that you can spell functions out like definitions. Like "fac(x) is

¤ 1 if x == 1

¤ x * fac(x-1) otherwise"

This is at least more declarative than describing the function with a loop. With an imperative program you kind of have to describe as a series of steps, because the order matters so much.

Personally I prefer this kind of declarative programming over something like Prolog since I get a simple way of describing what/how the program is, while still feeling declarative. With Prolog I still have to learn how it works, and the way that it works is more removed from imperative and functional programming (while functional programming can be done (maybe with, but in principle) in most imperative languages as long as you restrict your use of certain features). Plus Prolog has un-declarative things like the cut operator. I don't really have much experience with declarative programming outside of these two.

Something more declarative might indeed just be to give invariants and let the program find an implementation for it.

> Something more declarative might indeed just be to give invariants and let the program find an implementation for it.

EDIT: turns out that there kind of is:

http://nautilus.cs.miyazaki-u.ac.jp/cgi-bin/MagicHaskeller.c...