task 3 in Assignment 2

Moderator: Konzepte der Programmiersprachen

Beiträge: 4
Registriert: 17. Nov 2018 17:44

task 3 in Assignment 2

Beitrag von tud_informatik »

Hi, I checked the solutions for assignment 2.

But I cannot understand task 3 "Zippop" code(especially else-sentence).

Could you plz explain it in detail?

Beiträge: 11
Registriert: 4. Nov 2012 13:53

Re: task 3 in Assignment 2

Beitrag von a10r »

It is a higher-order function, meaning that it takes a function f of type (A, B) => C and returns a function of type (List[A], List[B]) => List[C]) that is parameterized with the function f you provided.
The returned function takes two lists. If both of them are not empty, it takes the first element of each list (which will be an A and a B) and applies the provided function f to them (you'll get a C). It then does a recursive call with the tails of the lists. "::" is element-to-list concatenation.
The first task "zipadd" should make it clear how to use it. Here you parameterize it with a simple add function. So the function returned from zipop will add the first element of list 1 to the first element of list 2, second element of list 1 to the second element of list 2 and so forth.


Zurück zu „Konzepte der Programmiersprachen“