If I use map or filter inside another function, does that function become a higher-order function? For example:
removeSpaces :: String -> String
removeSpaces xs = filter (not . isSpace) xs
Is removeSpaces a higher-order function?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No,
removeSpacesis not a higher-order function.A higher-order function is one that takes a function as an argument.
removeSpacesdoesn’t, so it isn’t.Higher-orderness is a property of a function’s interface, not its implementation, so we can tell that
removeSpacesisn’t higher-order just by looking at its type: it doesn’t take any functions as arguments, so it’s not higher-order. It is implemented with the use of a higher-order function, but that’s another matter entirely.For example,
filteris a higher-order function, because it is declared as taking a function as a parameter:But
concatisn’t, because there aren’t any function types (a -> b) as arguments:Polymorphism poses a tricky edge-case: you can pass a function to
id—id concat [[1, 2], [3, 4]]is[1, 2, 3, 4]— but its type does not declare it as taking any functions as arguments:In this case,
idis not higher-order. A function must explicitly have a function argument in its type to be higher-order.