 1601344800

# An Introduction to Functional Programming in Haskell

This is a beginner’s tutorial to let them understand the fundamentals of Haskell.

Haskell is a widely used purely functional language.

_Functional programs are more concurrent, and they follow parallelism in execution to provide more accurate and better performance. In typical programming languages, we instruct a compiler to do something and how to do it. — _https://www.tutorialspoint.com/haskell/haskell_overview.htm

That’s not so in Haskell, where we ask what it is we do. Also, Haskell is a sluggish programming language, so the program doesn’t execute code that it thinks is not necessary.

A Haskell program is nothing more than a series of functions that execute.

_Haskell is a strictly typed language. By the term strictly typed language, we mean the Haskell compiler is intelligent enough to figure out the type of the variable declared. Hence we need not explicitly mention the style of the variable used.– _https://www.tutorialspoint.com/haskell/haskell_overview.htm

## Buddha Community  1601344800

## An Introduction to Functional Programming in Haskell

This is a beginner’s tutorial to let them understand the fundamentals of Haskell.

Haskell is a widely used purely functional language.

_Functional programs are more concurrent, and they follow parallelism in execution to provide more accurate and better performance. In typical programming languages, we instruct a compiler to do something and how to do it. — _https://www.tutorialspoint.com/haskell/haskell_overview.htm

That’s not so in Haskell, where we ask what it is we do. Also, Haskell is a sluggish programming language, so the program doesn’t execute code that it thinks is not necessary.

A Haskell program is nothing more than a series of functions that execute.

_Haskell is a strictly typed language. By the term strictly typed language, we mean the Haskell compiler is intelligent enough to figure out the type of the variable declared. Hence we need not explicitly mention the style of the variable used.– _https://www.tutorialspoint.com/haskell/haskell_overview.htm 1595593200

## Functional Programming

Functional Programming is a Declarative style of Programming Paradigm for writing computer programs.
But, What are Functions ?
Functions in general, applies computation on given input and returns the output. It relates input to an output.
f(x) = x + 2;
f(1) = 1 + 2 = 3;
f(2) = 2 + 2 = 4;
Above mentioned is a simple function that adds 2 to the input value and returns output. It relates value [1,2] => [3,4]. Similarly, a function in computer programming is a block of instruction that performs computation on given input and returns the output.
Functional Programming is such a style of writing computer programs using these tiny functions that works together and perform required computation.
Functions in Functional Programming
The philosophy of functional programming is to maintain certain characteristics while writing functions in a computer program. These characteristics are the fundamental nature of functional programming that describe what shall be the behaviour of a function. These are as follows :
Declarative
A function must be declarative, it simply tells what to compute, without specifying how to compute it.
f(x) = x * 4; 👍
Declarative function that tells to multiply input by 4;
f(x) = { y = x + x; z = x + x; return y + z;} 👎
Non-Declarative function that specify how to multiply input by 4;
Pure
A function must give the same output for a given input value, at any period of time. It is not dependent upon anything outside the function definition.
f(x) = It’s never too late; 👍
Pure function that will always return It’s never too late
f(x) = If today’s Friday or Saturday then It’s never too late else It’s late. 👎
Impure function that consider day for returning value. The value is not predictable. It can change. So a function that performs anything which is unpredictable is not a pure function. The condition or the execution is dynamic or unfixed in an impure function.

#development #functional-programming #software #software-programming #declarative-programming #function 1598093520

## Are functions from programming really functions?

If you are reading this, then most probably you already know quite well what functions are in programming. A function is quite a common and spread programming construct that is present in almost all programming languages.

Generally, a function is a block of code that takes some parameters from outside, executes some operations in which these parameters may be used, then it returns an output value. Actually, in many programming languages functions are allowed to not return something or to return multiple values, not only one. But these cases can be also be represented, for the sake of generality, as only one value. For the “no return” case we can use a special value to represent that (in Python that special value is None; whenever you don’t return something from a function it’s returned None). And for the case of more values, we can use a vector of multiple values as a single object.

For example, in Python a function definition looks like this:

``````def func_name(param1, param2, ...):

## do some stuff with input parameters
return output_value
``````

Now, the question I want to ask: Are these functions from programming true mathematical functions?

Well…, let’s first recall what a mathematical function is.

In mathematics, a function is just a mapping from a set A to a set B, in which any element from A has only one associated element in B.

#python #programming #function #mathematics #functional-programming 1597240860

## Functional Programming in Javascript

The mystic term of Functional Programming (FP) must be familiar to any JS developer. The first impression when we say “JS supports functional programming paradigm” or “JS is a Functional programming language”, could be, well I use functions in JS, so I’m already doing Functional programming. This is the myth that we’re going to bust together.

You might be using the FP unknowingly but there’s a lot more to the world of Functional programming which takes years of experience to master. It involves re-wiring your thinking process to observe and implement the FP patterns in your style of writing programs.

## Preface

Researching online about FP can leave you inundated with mathematical information that you have no awareness of. This is probably why it’s not that popular among the developer community (due to the steep learning curve involved). Even if you learn some of FP concepts in Javascript, like currying or composing, you may find yourself wondering “How the heck can I use it in my real-world projects?” That’s the same situation I found myself in when I learned about currying (which I wrote an article two years back here wherein I explained it with the help of a contrived example). Guess what, the most common question was, “How and when can I use it in the real world? And is that all Functional Programming is all about?” We’ll address all these questions here.

But before we go any further, we need to understand the basic pillars of FP and why is it worth learning FP in general.

## What is Functional Programming?

Functional Programming (FP) is a programming paradigm or a way/style of programming which has the following characteristics:

• We build programs by composing reusable “Pure functions”.
• Write declarative code instead of imperative.

It’s more of a way of adapting your brain to think of the FP patterns in your programs.

We’ll learn about all of this using practical examples in a moment.

## Why Functional Programming?

Below are the convincing reasons to learn FP:

### Imperative vs Declarative code

Consider this code snippet which gets the best products out of an array of products. It does the job, it gives the correct output. But we had to read through the whole code like a story or a series of steps we’re telling the computer to execute in order to make meaning out of it. A computer is excellent at this job of executing programs step by step as a sequence of instructions. And above is a classical example of Procedural programming. So, even though the keyword “function” is used here, it doesn’t make it qualify for Functional Programming because it violates the fundamental principles of FP like pure functions and no side-effects.

Let’s see a declarative version of the above snippet.

#javascript #react #haskell #functional-programming #curry #function 1596300660

## Functional Programming Series (2): What Is a Monoid?

For those interested in functional programming, I’ll talk about monoids and why they’re very important to understand ahead of time.

Don’t get confused: This isn’t monad — it’s monoid. I’m pretty sure you already know of monoids and you use them almost every day — you just didn’t know the term for them.

# Identity Function

Let’s assume there’s a function named `identity` that takes `A` and returns `A`.

``````const identity: <A>(a: A): A => a;

interface Student {
name: string;
age: number;
}
identity<number>(3) // 3
identity<string>('hello') // hello
identity<Student>({
name: 'Bincent',
age: 5
}); // { name: 'Bincent', age: 5 }
``````

In functional programming, this useless function (seems useless) is an important factor for many other concepts (such as monoids) that we’re about to talk about. Basically, a monoid is a set of elements that holds the rules of the semigroup and the identity-element rule.

If `S` is a set of elements, `a` is a member of `S`, and `·` is a proper binary operation, `a·e = e·a ∈ S` must be satisfied to be a monoid.

``````Identity: a ∈ S, a·e = e·a = a ∈ S
``````

Some documentation calls this using the number 1 and the any alphabet in subscript — for example, 1x referring to the identity on the variable x. Or some documentation uses just a single alphabet letter, such as or e.

That’s all there is to know about monoids, let’s practice with some simple examples.

#typescript #programming #functional-programming #javascript #coding #function