Seite 1 von 1

task 3 in Assignment 2

Verfasst: 17. Nov 2018 17:48
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?

Re: task 3 in Assignment 2

Verfasst: 18. Nov 2018 12:26
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.