0
votes

In an exam past paper I found online it asks us to rewrite the following higher order functions using list comprehension:

hofOne = map (*2) [1,2,3]

hofTwo = filter isSquare randomList

I'm not sure if I'm misunderstanding higher order functions, because I recognize that both the map and filter functions themselves are higher order functions, but I don't understand how that makes the hofOne and hofTwo functions higher order when neither of them take a function as a parameter, nor return a function as a result.

Is there something I'm missing?

1
No, you're right. They aren't functions either.FrownyFrog
Maybe the intention was to "Rewrite these applications of higher order functions using list comprehension"that other guy
try defining them in GHCI and then using the :t operator. You will easily see that neither of these are function, in that they have no "arrows" in their type signature. (I haven't tried, but the first looks like (Num a) => [a], while the second depends on the exact types of isSquare and randomList, but will definitely be of some type [a] with some possible restrictions on a. No -> arrows, so they're not functions. (And a higher-order function which takes a function as a parameter will have a bracketed part with one or more arrows in.)Robin Zigmond

1 Answers

0
votes

You are correct. The Wikipedia - Higher-order function article defines higher-order functions as either taking a function in one of its parameters or returning a function as its result. The intention is probably to rewrite the higher-order functions map and filter, which are higher order functions, as list comprehensions.

Without giving the answer to the question, here are two examples of how to use list comprehensions in the intended style.

-- map as list comprehension
[doSomething x | x <- [1..10]]

-- filter as list comprehension
[x | x <- [1..10], somethingThatReturnsBool x]

For anyone unfamiliar with Haskell list comprehension, take a look at this article.