# The Craft of Functional Programming

Learn functional programming as a noob.

I’m assigned by my lecturer to do some works related functional programming. Here’s some exercises and the **codes**.

# Define the length function using `map` and `sum`.

The first time I learn writing codes in Haskell, I have to make sure that I understand how structure data in Haskell works, how to make a function in Haskell, and etc.

The first thing that I learnt is:

- Haskell is a language which has a lot of functions that’s built in its module, and this function’s built
**recursive-ly!** - Two-built functions that will be used in this task are
**map**and**sum**

First, let’s see how both of this functions work:

**Map Function**

“Map takes a function and a list and applies that function to every element in the list, producing a new list” — Learnyouahaskell.com

by the definition, map could be written like this:

`map :: (a -> b) -> [a] -> [b]`

1.1 map _ [] = []

1.2 map f (x:xs) = f x : map f xs

let’s think about this: think about I have a function which I can replace all the element in the array with a number that I desire sum by each element in array, in this case, I want to change it the number I desire with 17.

`2.1 func_change_to_seventeen y = 17 + y`

So, I want a function that can change each elements in my array to the desired result. Now, let’s build the function that return the result when we call the function

`3.1 res_plus_17 [] = []`

3.2 res_plus_17 x = map func_change_to_seventeen x

now let’s see what’s happened to this function

In the 3.1 line, we must check if user gives an empty array or not when this function’s called, if it gives an empty array, then we should return an empty array

In the 3.2 line, we catch the array that must be **not empty** and put it in a variable called x. And then we use map function to change each element of the array. Then, how exactly map function works?

By the code in line 1.1, there are two variables: **_, []** which means **_ **is used to catch any data types likes number, char, etc and **[] **accepts array. If we give an empty array to this function then it will return empty array

By the code in line 1.2, **f **stands for any function that’ll be used in map function. **x** is the first element in array and **xs **each element in array unless the first element. So, if we have array [1,4,3,8] then the **x **is 1 and **xs **is [4,3,8]. Then next **f x **means that we call a function f and pass x as a parameter. Then the next **map f xs **means we call map function (again, yes, it’s a recursive) and pass the function and the rest of the array.

Then let’s jump to our example:

In line 3.2 we see that there’s **map** function that calls **func_change_to_seventeen **that accept parameter x (x is an array) and will be loop-ed in map function (if you still don’t get how map works, read it from top again). Now let’s see what’s happened in **func_change_to_seventeen**. In this function, we use parameter **y** that will accept every number that we pass to **func_change_to_seventeen**. Example, if we call the function with

`func_change_to_seventeen 10`

>>> 27

it will return 27 as a result.

**Sum Function**Sum is a function that haskell has built. It will return a result with type like this:

`Num a => [a] -> a`

and will be functioned to compute a sum of all elements in the array, example:**input: sum [1,2,3,4]output: 10**

## So how to count the length of an array with `map`

and `sum`

?

**The idea:** change all the elements in an array with 1. Then we get every array with length n have n 1s.

Example: [3, 4, 5, 1] —> [1, 1, 1, 1]

**Then, what’s next?**

With sum function, we can sum all the elements in array that we have (has been converted to array of 1) and then will return result which tells the length of the array itself. Here’s the function:

`res_ [] = 0`

res_ x = sum (map convert_to_one x)

convert_to_one x = 1

As you can see, first we must convert all the array with 1 by calling **convert_to_one** function that will return array. And then we just need to put **sum **function that will sum all the elements in the array.

TADA. And that’s how it works.