Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Introduction to Haskell

Welcome to the world of Haskell. Before going deep into the language let me explain what is Haskell, What are the key features of Functional Programming languages. Haskell is a pure functional programming language developed in late 1980s, it is named after Haskell Curry, was an American mathematician and logician. It incorporated many recent innovations in programming language design. It provides a  framework of elegant libraries to build complex and maintainable systems. First let me try to explain, what is functional programming. Following are the core concepts of functional programming.

a. Functions are the first class citizens in functional programming
Functions are the basic building blocks in functional programming, unlike object oriented programming languages; a function is not enclosed inside a class. A function can be passed as an argument to other function, a function can return result as a function, You can assign a function to a variable etc., In summary, you can use a function like variable.


For Example,
let sum x y = (x + y)


Above statement define a function named sum, which takes two number arguments and return the sum of two numbers. In Haskell, let key word is used to define variables, functions in Haskell interpreter.
Prelude> let sum x y = (x + y)
Prelude>
Prelude> sum 10 20
30
Prelude>
Prelude> let name = "Krishna"
Prelude> name
"Krishna"
Prelude>

You can learn Haskell basics without installing. Haskell home page
https://www.haskell.org/ provides user interface to communicate with Haskell interpreter.

b. Pure functional programming is side affect free
A Pure functional programming language has no side affects. A function is said to have side affect, if the result is not completely depend on its arguments. For example, a particular function might modify a global variable or static variable, modify one of its arguments, raise an exception, write data to a display or file, read data, or call other side-effecting functions. In the presence of side effects, a program's behavior may depend on history.


For example, observe following code.
public class ProcessData {
private static int x = 10;

public static int f(int a) {
x = x + a;
return x;
}

public static int g(int a) {
x = x * a;
return x;
}
}


Result of functions f, g is depend on the global variable x, and result is also depend on the order of execution of functions f, g.

If you call function f(5) first followed by g(5), then f(5) return 15, g(5) return 75. If you call function g(5) first followed by f(5), then f(5) return 55, g(5) return 50.

A side affect free function always returns same value for same input, i.e., it doesn’t depend on any global variable, external data, and order of executions.

Note
Side affects (Such as writing data to external systems, read data from netwroks etc.,) are still possible in Haskell, but Haskell is designed like that, it separates the pure code from impure(side-affect) code.

c. Functions can take other functions as input and return a function as output
In functional programming a function can take other functions as input.


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Introduction to Haskell

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×