Edit this page Backlinks This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. ====== 3. Higher-order functions ====== Objectives: * implement and use **higher-order** functions. A **higher-order** function takes other functions as parameter or returns them * implement **curry** and **uncurry** functions, and how they should be properly used (review lecture). **3.1.** Define the function ''foldRange'' which uses an operation ''op'' to reduce a range of integers to a value. For instance, given that ''op'' is addition (+), the result of folding the range 1, 3 will be 6. ''foldRange'' should be curried (it will take the operation and return another function which expects the bounds). <code scala> def foldWith (op: (Int,Int) => Int)(start: Int, stop: Int): Int = { def tail_fold(crt: Int, acc: Int): Int = ??? ?? } </code> **3.2.** Define the function ''foldConditional'' which extends ''foldWith'' by also adding a predicate ''p: Int => Int''. ''foldConditional'' will reduce only those elements of a range which satisfy the predicate. <code scala> def foldConditional(op: (Int,Int) => Int, p: Int => Int)(start: Int, stop: Int): Int = ??? <code> **3.3.** Let $math[count_k(n) = k + 2k + 3k + ... x*k], with $math[ x*l \leq n] be the sum of all multiples of $math[k] within the range 1,n. Write a function ''alldivs'' which computes the sum: $math[count_1(n) + count_2(n) + ... + count_k(n)]. (Hint, use ''foldConditional'').