Does a language with first-class functions necessarily allow closures? [closed]
Asked Answered
M

1

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?

Morey answered 16/12, 2014 at 2:8 Comment(11)
This might be better suited on Programmers.SE, but still a good question.Lightless
I wasn't sure where to ask, but figured it'd be flagged for migration if it was in-appropriate.Morey
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).Lewiss
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.Dracula
@Sylwester could you please add that as an answer and some more explanation as to how it works?Morey
@Dracula 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.Morey
@Morey 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)Kenny
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.Morey
@Morey I'm unaware how to, but you can create a new question in CS and delete this one.Kenny
@Morey 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).Dracula
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.Morey
D
3

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.

Dracula answered 16/12, 2014 at 2:34 Comment(2)
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?"Morey
No, it's not possible. Having functions defined by the language as first-class citizens implies that you can make closures.Dracula

© 2022 - 2024 — McMap. All rights reserved.