Haskell Journey: Higher Order Functions

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)! …


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 🗃

Photo by Artem Kniaz on Unsplash

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:

Haskell coding example: using “Function”.

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:

Haskell coding example: using “Lambda”.

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.
Haskell coding example: using “HOF”.

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:

Haskell coding example: using “Currying”.

(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.

Haskell coding example: using “Filter”.

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:

Haskell coding example: using “Composition”.

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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » Haskell Journey: Higher Order Functions." Jennifer Takagi | Sciencx - Sunday December 19, 2021, https://www.scien.cx/2021/12/19/haskell-journey-higher-order-functions/
HARVARD
Jennifer Takagi | Sciencx Sunday December 19, 2021 » Haskell Journey: Higher Order Functions., viewed ,<https://www.scien.cx/2021/12/19/haskell-journey-higher-order-functions/>
VANCOUVER
Jennifer Takagi | Sciencx - » Haskell Journey: Higher Order Functions. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/19/haskell-journey-higher-order-functions/
CHICAGO
" » Haskell Journey: Higher Order Functions." Jennifer Takagi | Sciencx - Accessed . https://www.scien.cx/2021/12/19/haskell-journey-higher-order-functions/
IEEE
" » Haskell Journey: Higher Order Functions." Jennifer Takagi | Sciencx [Online]. Available: https://www.scien.cx/2021/12/19/haskell-journey-higher-order-functions/. [Accessed: ]
rf:citation
» Haskell Journey: Higher Order Functions | Jennifer Takagi | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.