Ex07 Task 2.1

Fabian U.
Neuling
Neuling
Beiträge: 4
Registriert: 1. Mär 2017 12:48

Ex07 Task 2.1

Beitrag von Fabian U. » 1. Mär 2017 14:39

Hi,

when re-doing the exercises as an exam preparation we came across a problem in Ex07 Task 2.1 .
In the solution you show an example on how to implement first-class functions with our programming laguage. Since the language doesn't support first-class classes, this should't be possible. When looking at the example it seems like it is a higher-order function, not a first-class function (you even define it on 'compiletime').

Can you please clarify this?

Regards

Ragnar
Mausschubser
Mausschubser
Beiträge: 63
Registriert: 21. Okt 2009 19:15

Re: Ex07 Task 2.1

Beitrag von Ragnar » 1. Mär 2017 19:21

Hi, the language has no syntax to define new classes inside of expressions.
However, you can define a class which will behave like a function, and you can pass instances of that class around, because objects are first class. So the encoded function also counts as being first class.
Writing functions inline is no requirement for them being first class.

Fabian U.
Neuling
Neuling
Beiträge: 4
Registriert: 1. Mär 2017 12:48

Re: Ex07 Task 2.1

Beitrag von Fabian U. » 2. Mär 2017 11:20

Hi,

okay I understand this, but now I am struggeling with understanding any difference between first-class functions and higher-order functions.

From the lecture, V03 Slide 5:

No first-class functions => functions can only be defined in designated
portions of the program, where they are given names for use in the rest
of the program

Doesn't this apply for our language? We can only define functions in designated portions of the program?

Additionally, can you maybe give an example for a higher-order and a first-class function (exclusively).

Regards

0b101101101
Endlosschleifenbastler
Endlosschleifenbastler
Beiträge: 179
Registriert: 15. Apr 2015 18:24

Re: Ex07 Task 2.1

Beitrag von 0b101101101 » 2. Mär 2017 21:28

I believe for the purpose of this lecture the definition of first-class functions seems to be: "The ability to pass functions as arguments".

[Sidenote: I would however add, that it seems sensible to include the ability to construct new values at runtime, for a value to be truly a first-class citizen of a language. Because you can create numbers at runtime, so you should be able to create functions at runtime, if both are to be called first-class equally. Actually I believe, something beeing a first-class citizen of a language is probably not an clear yes-or-no decision, because the bigger the language gets, the more things a value has to be able to do, to be first-class.
If your language supports variables, than you should be able to put a value into a variable to consider it first-class. If you have functions you should be able to pass a value as an argument. If you have pointers you should be able to create a pointer to them, etc... ]

For example operators like + - * / == are in many languages (Python, Java, ...) not first-class, because you have to wrap them inside a function to be able to pass them to a function. I think, methods/lambdas are not first-class in Java8, because they are called differently than normal functions ( f.call(x) vs f(x) ).

There is nothing special about a function to make this specific function first-class in contrast to the other ones. As it is the ability to do something with a function that makes the language have first-class functions.

So you could say:
A function is of higher-order if it does take or return functions.
A language (not the function) is first-class-functionable if you can write higher-order functions. :)

Antworten

Zurück zu „Archiv“