Assignment 3 - Task 3 (and 4)

Jannis
Mausschubser
Mausschubser
Beiträge: 63
Registriert: 15. Apr 2015 17:10

Assignment 3 - Task 3 (and 4)

Beitrag von Jannis » 24. Nov 2017 12:04

Hello!

1)
Is it allowed to implement common higher-order functions, or do we have to create more specific higher-order functions?

2)
Most higher-order functions abstract over computations of a given function. Does "[...] could not be written nicely with first-order functions only [...]" therefore refer to the combination of the definition of the higher-order function and its function application? For higher-order functions which take a function as an argument, one could just paste the given function inside the definition and get a first-order function which is written "as nice" as the higher-order function. From my understanding, the advantage of higher-order functions is that we gain a layer of abstraction and can reuse this function in different ways. This means that the function application is the part, where higher-order functions provide a "prettier" code. But the definition itself actually does not get "prettier".

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

Re: Assignment 3 - Task 3 (and 4)

Beitrag von Ragnar » 24. Nov 2017 14:38

Hi,

1) You are not limited in which functions you choose, other than they should make use of first class functions. I would however recommend to choose the same functions for task 3 and 4, as that will make task 4 easier.

2) I am not sure if I correctly understand the question.
Having the layer of abstraction is meant in the task when it says that the function could not be written nicely. Having to copy&paste function definitions is definitely not „nice“. :-)

Extra) „Most higher-order functions abstract over computations of a given function.“ Try to give an example of a higher-order function, which does not abstract over computations.

spilot
Neuling
Neuling
Beiträge: 7
Registriert: 28. Nov 2015 18:28

Re: Assignment 3 - Task 3 (and 4)

Beitrag von spilot » 24. Nov 2017 14:46

Jannis hat geschrieben:
24. Nov 2017 12:04
Is it allowed to implement common higher-order functions, or do we have to create more specific higher-order functions?
Could you please elaborate what you mean by "common" and "specific"?
Jannis hat geschrieben:
24. Nov 2017 12:04
For higher-order functions which take a function as an argument, one could just paste the given function inside the definition and get a first-order function which is written "as nice" as the higher-order function.
I don't think this is "as nice". It means that using the construct equivalent to the higher-order function requires copying and pasting a verbose code template instead of just calling the higher-order function. Using goto statements and conditional branches is not "as nice" as using loops, even if the computation result is the same.
Jannis hat geschrieben:
24. Nov 2017 12:04
This means that the function application is the part, where higher-order functions provide a "prettier" code. But the definition itself actually does not get "prettier".
I agree that the definition of the higher-order function itself is not prettier. However, it must only be defined once instead of everywhere you want to use it.

Jannis
Mausschubser
Mausschubser
Beiträge: 63
Registriert: 15. Apr 2015 17:10

Re: Assignment 3 - Task 3 (and 4)

Beitrag von Jannis » 25. Nov 2017 15:25

Thank you for your answers!
Ragnar hat geschrieben:
24. Nov 2017 14:38
Extra) „Most higher-order functions abstract over computations of a given function.“ Try to give an example of a higher-order function, which does not abstract over computations.
A simple example would be a function which returns a function, but does not take a function as an argument. Or a function which takes a function but never applies it.
spilot hat geschrieben:
24. Nov 2017 14:46
Could you please elaborate what you mean by "common" and "specific"?
For me, "common" higher-order functions are functions like map, fold and filter. More "specific" higher-order functions would be functions which have a more complex body and cannot be applied for a lot of different problems.

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

Re: Assignment 3 - Task 3 (and 4)

Beitrag von Ragnar » 26. Nov 2017 21:27

Jannis hat geschrieben:
25. Nov 2017 15:25
Ragnar hat geschrieben:
24. Nov 2017 14:38
Extra) „Most higher-order functions abstract over computations of a given function.“ Try to give an example of a higher-order function, which does not abstract over computations.
A simple example would be a function which returns a function, but does not take a function as an argument. Or a function which takes a function but never applies it.
Those are actually good examples :D
The point I wanted to make, and which still holds is this: as functions are an abstraction for computation, if you pass them around, you almost certainly are always „abstracting over computations“.
Even for the cases you mentioned. Okay, not executing the parameter may be a bit degenerate, so its hard to argue it does any sensible abstractions at all, but returning functions – while not abstracting over some computation of some input function – is a different kind of abstraction: i.e., all the possible returned functions belong to some abstract family of functions, which share some common functionality, but differ depending on the other parameters of the constructing functions.

In the end, one of the goals of this lecture is that there are many many different techniques (or abstractions), which one can have in a language. Some of them are useful for all kinds of purposes (like closures, recursion, state, objects, pattern matching, …) others are more special purpose (such as the volatile keyword in java, or perl variables which track that they are "tainted" (aquired from user input) disallowing you to eval those to prohibit code execution exploits), others are super domain specific (maybe your language has a keyword, such that some code is only executed on thursdays?).
The point here is twofold, knowing which languages (with which features) you need for a given task is good, and you can always just invent your own abstractions, if the existing ones are too limited.

or maybe the point is just me getting old and liking to talk :D. in any case, have the appropriate amount of fun :-)

Antworten

Zurück zu „Archiv“