Filter haskell6/2/2023 ![]() And this function, finally, just takes an a and returns an a. Similarly, this function takes an a and returns a function of type (Num a) => a -> a. So our function takes an a and returns a function of type (Num a) => a -> (a -> a). The thing before the -> is the parameter that a function takes and the thing after it is what it returns. Remember that this function's type could also be written as multThree :: (Num a) => a -> (a -> (a -> a)). 9 is applied to that function and the result is 135 or something. So then 5 is applied to that, which creates a function that will take a parameter and multiply it by 15. That creates a function that takes one parameter and returns a function. What really happens when we do multThree 3 5 9 or ((multThree 3) 5) 9? First, 3 is applied to multThree, because they're separated by a space. Take a look at this offensively simple function: Using partial application (calling functions with too few parameters, if you will) is a neat way to create functions on the fly so we can pass them to another function or to seed them with some data. So how is that beneficial to us? Simply speaking, if we call a function with too few parameters, we get back a partially applied function, meaning a function that takes as many parameters as we left out. That's why the return type and the parameters of functions are all simply separated with arrows. That could be read as: max takes an a and returns (that's the ->) a function that takes an a and returns an a. That can also be written as max :: (Ord a) => a -> (a -> a). The space is sort of like an operator and it has the highest precedence. Putting a space between two things is simply function application. That sounds like a mouthful but it's actually a really cool concept. ![]() Then, 5 is applied to that function and that function produces our desired result. Doing max 4 5 first creates a function that takes a parameter and returns either 4 or that parameter, depending on which is bigger. ![]() It looks like it takes two parameters and returns the one that's bigger. Let's take our good friend, the max function. What does that mean? You'll understand it best on an example. So how is it possible that we defined and used several functions that take more than one parameter so far? Well, it's a clever trick! All the functions that accepted several parameters so far have been curried functions. Curried functionsĮvery function in Haskell officially only takes one parameter. They're a really powerful way of solving problems and thinking about programs. It turns out that if you want to define computations by defining what stuff is instead of defining steps that change some state and maybe looping them, higher order functions are indispensable. Higher order functions aren't just a part of the Haskell experience, they pretty much are the Haskell experience. A function that does either of those is called a higher order function. Haskell functions can take functions as parameters and return functions as return values.
0 Comments
Leave a Reply. |