This content originally appeared on Level Up Coding - Medium and was authored by Jennifer Takagi
A lot of time has passed since my last post đ˘ But Iâm back and now writing directly from Madrid! đŞđ¸
Here we go to the fourth post in the Haskell Highlights series! đ
In this one, Iâm going to show you a little bit about the Higher Order Functions (HOF)! Maybe you already know these guysâââIâve already written about some HOF on this previous post â, but keep reading and discover some new topics regarding functionsâŚ
Some useful links Iâve been using in my studies:
(1) Learning the concepts based on these books: Haskell an introduction to functional programming and Learn You a Haskell for Great Good! đ
(2) Taking notes on this Notion document đ
(3) Coding some exercises on my GitHub repository đ
The first concept to remember is that âfunctions are defined as a reusable block code that can be used many times on the same programâ, in Haskell functions it looks like:

The functions can have an extra shine: âthey can take other functions as parameters and also return other functions as their valueââââmaybe it looks a little confusing now, but hopefully by the end of this post you can really understand that concept.
Therefore, I decided to divide the subject into 4 parts to facilitate the understanding of this important character in programming. đ
Lambda
Iâve never understood Lambdas so well, but after studying more about HOF, I found a simple way to explain it:
Lambas are functions that are used once, do not have a contextâââanonymous functionâââand are only used as values
Letâs rewrite the âfunction sumTenâ as a Lamba:

Understanding the syntax:
(1) The â\â indicates a Lambda function.
(2) It receives an âxâ parameter and adds â10â.
(3) You can see that weâve just declared and used the function immediately by sending it an argument, â5â. The return of this Lamba is â15â.
Higher Order Functions
I ended up talking about the characteristics of HOF at the beginning of this post, so Iâll leave a super-summary definition and an example below.
Higher Order Functions are functions that take functions as parameters or return other functions as their value.

Diving deep into the example:
(1) The function âhofâ has been declared and takes another function as a parameter (âfnâ).
(2) Into the âhofâ body, the âfn functionâ is called with an integer argument (â20â).
(3) Finally, âhof functionâ duplicates the âfn functionâ value returned.
(4) To use the âhof functionâ, we call it and send the âsumTen functionâ, or even send a Lamba functionâââin both cases the result was 60.
An interesting observation is that the HindleyâMilner type shows that the âhof functionâ takes a function fn that takes and returns an âIntâ type, and the âhof functionâ itself also returns an âIntâ.
Currying
The curious thing is that Haskellâs functions only take one parameterâââofficially speakingâââhowever, we can send more parametersâŚand to support this, the language uses a technique called Currying. đ¤
Currying is a technique where a function takes multiplication arguments and then returns a functionâs string evaluationâââif a parameter was not passed, it returns a function with it
It seems complex, but take a look at the following code:

(1) The âjoinWordsâ function was basically declared to join 3 String taken as parameters and return them together into a String.
(2) The âjoinOneMoreWordâ function has been declared to receive a String and return one as well. But the important point here is that this function calls the âjoinWords functionâ passing only 2 arguments, in other words: leaving an empty slot argument.
(3) When the âjoinOneMoreWord functionâ is executed we have to send one more argumentâââotherwise, an error will be generated.
(4) Something like (joinWords (âhelloâ) (âworldâ) (â!â) is happening inside this function, a sequence of the main function was generated to each parameter.
You may know about functions as âmap, filter, foldlâ. These functions take 2 parameters: a function and a list, so they are Higher Order Functions and use the Currying technique to apply these functions to each item in the listâââsee the code below for an example of usage.

Composition
Composition applied in Functional Programming can be understood as a âchain of functionsâ or being more specific:
Compositing is the technique of composing functions and passing the result of the current function to the next functionâââthe order of execution is right to left.
Letâs use the âsumTen functionâ and declare a new function called âdoubleâ to apply the Composition in Haskell in the code below:

Catching up :
(1) We composed the functions âsunTenâ and âdoubleâ with the argument â10â. As you can deduce to use Composition in Haskell you only need the syntax (fn1Â . fn2Â . fn3) arg.
(2) And as said before regarding the order of the Composition is from right to left. In the example, first, the number 10 is multiplied by 2, then added 10 to it, the result is 30.
Hope this post has helped you understand more about HOF in any languageâââin JS weâre always using map, filter, reduce, etc, but we often donât understand how they work behind the scenes
Thanks for reading it and in the next post I want to cover the Creation of Types in Haskell!
See yah and stay safe! đ¤đŠđťâđťđ˝
Haskell Journey: Higher Order Functions was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Jennifer Takagi

Jennifer Takagi | Sciencx (2021-12-19T19:41:50+00:00) Haskell Journey: Higher Order Functions. Retrieved from https://www.scien.cx/2021/12/19/haskell-journey-higher-order-functions/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.