7

I understand the broad concept of a closure (functions are stored along with a snapshot of environment at the time they were defined), and functions as first class citizens means that functions can be passed and returned like any other data type in the language.

Interestingly all languages that I've worked with that have functions as first class citizens, for example Python, Javascript, Scheme seem to always have closures as well.

Indeed passing and returning closures to and from a function is one way of implementing functions as first class citizens of the language, but I'm unsure if being able to write them is a direct and inevitable consequence of functions being first class citizens.

To put in more specific terms:

Can you provide an actual example of a language which has first-class functions but where it is not possible to write closures?

ffledgling
  • 11,502
  • 8
  • 47
  • 69
  • This might be better suited on Programmers.SE, but still a good question. –  Dec 16 '14 at 02:56
  • I wasn't sure where to ask, but figured it'd be flagged for migration if it was in-appropriate. – ffledgling Dec 16 '14 at 02:58
  • There's also a Computer Science SE. Some questions are good questions in more than on of those in different ways, and I think this might be one of them, though I don't know what people think of cross-posting (not being active enough on enough of the sites to ever spot it). – Jon Hanna Dec 16 '14 at 03:05
  • I think it's not harmful to have questions like this here on SO because a vast number of people never read CS or Programmers SE. So, having some questions like this one helps programmers become better programmers, which is after all the purpose of this site. – Jivan Dec 16 '14 at 03:11
  • @Sylwester could you please add that as an answer and some more explanation as to how it works? – ffledgling Dec 16 '14 at 21:31
  • @Jivan Could you please revert your edits to the question? They subtly change the meaning of what I asked. I asked if it's possible to have A without B, you said if you have B, A is implied. They're not exactly the same thing. – ffledgling Dec 16 '14 at 21:35
  • @ffledgling I would, but the question is put on hold so there is no way to do so. I'm afraid it's not possible to make it on topic here so you should perhaps take the advice and move it to cs (I don't think it's on topic for programmers) – Sylwester Dec 16 '14 at 21:35
  • I don't know how to migrate it, If I knew how, I would, do I need to flag for migration? Also, I don't think it's on hold because it's not appropriate for the SO, it's on hold because it's "too broad", although I think it definitely should have a definite answer. – ffledgling Dec 16 '14 at 21:35
  • @ffledgling I'm unaware how to, but you can create a new question in [CS](http://cs.stackexchange.com/) and delete this one. – Sylwester Dec 16 '14 at 21:41
  • @ffledgling I just edited the question to reflect the reformulation you made in your comment to my post. So I don't see the point here. The edit seems clearer and have already brought new answers to the questions. And it asks precisely if A implies B (is it possible to have A without B). – Jivan Dec 16 '14 at 22:46
  • All right, then I guess it's just me that it changes the underlying meaning. I'm happy to let the edit stay if it helps discussion. – ffledgling Dec 17 '14 at 00:52

1 Answers1

4

Such languages are languages in which functions are first-class objects.

When a function is defined inside another function, the nested function is called higher-order function.

Functions being first-class objects means that functions do not differ from other objects such as numbers, strings, classes etc. Thus, you can pass them as arguments, or return them, just like any other object, without calling them. For instance in Python, you can return the function in itself without calling it, by omitting the parenthesis.

Being able to write a higher-order function which another wrapping function can return is precisely the definition of a closure, and is a consequence of having functions as first-class objects.

Thus, the answer to your question is: yes, closures are a necessity for having functions as first-class objects, in the sense that they are a consequence of that. To put it more directly, you can't have first-class functions and not be able to write closures.

Note that some languages without first-class functions (Pascal, Algol) have sort of closures which are called lexical closures. But they are far less powerful than actual closures.

Jivan
  • 21,522
  • 15
  • 80
  • 131
  • I do understand what you're saying, but my question is slightly different, rephrased it can put forth as "Is it possible to have a language with functions as first class objects, but no closures?" – ffledgling Dec 16 '14 at 02:39
  • 2
    No, it's not possible. Having functions defined by the language as first-class citizens implies that you can make closures. – Jivan Dec 16 '14 at 02:44