I have written the following code. I know that a higher order function is a function that takes in another function and returns a function as a result. But I can’t clearly distinguish a function if it is higher order or not. Is sort a higher order function? How do I find out if a function is higher order or not?
sortString :: String -> [String]
sortString x = sort (convertStringIntoList x)
A higher-order function is a function that takes another function as an argument, produces another function as a result, or perhaps both.
The function in your question has a single argument that is a string, not a function. When applied, your function’s value is an array of strings, not a function. Therefore, it is an ordinary function.
A similar function that is higher order is
The power of higher-order functions is the flexibility they provide. Providing different functions produces different results:
In case you aren’t familiar,
compareprovides anOrderingrelative to its arguments, e.g.,As you may have noticed,
flipis itself another higher-order function that reorders or flips the arguments to another function. Usingflipas in the code above allows us to sort the list in descending rather than ascending order.