## Everything you need to know to get started with Haskell

We look at Haskell, one of the most popular functional languages out there.

Haskell is the new kid in the programming language block. It also follows the computing paradigm of ‘functional programming’, that is, to approach a problem in terms of what the solution should look like rather than what steps should be taken to get the solution.

Although Haskell has grown in popularity in recent years, the language itself is more than 20 years old. Until 1987, at least a dozen implementations of a purely functional language existed, and at the Portland Programming Language and Computer Architecture (FPLCA) conference in Portland, it was decided to publish an open standard for functional programming to promote it. As an alternative style of programming.

**What is effective programming?**

One example that is often cited is the formulas in Microsoft Excel. When a formula enters a cell, the final value is always expressed in terms of other existing cell values. No attempt is made to determine the order of evaluation of these cells; Instead, we hope that Excel will find its own reliance. Similarly, no attempt is made to manage the memory used by the expression. Finally, we program using expressions instead of instructions – an expression simply establishes a relationship between input and output without explicitly listing the steps needed to fulfill that relationship.

As the name implies, functions of a functional programming language (including Haskell) are first class citizens. This means that they can not only be called and called as their obligatory counterparts, but the functions can be transmitted as arguments of other functions and even defined in the scope of other (parent) functions. This feature allows you to express almost all programming structures in the form of functions. A feature of all functional programming languages is that they are called ‘lazy’. So it only evaluates an expression when it is needed, and not before. This is especially convenient when working with infinitely large data structures such as compound expressions (expressions where the operands themselves are expressions) or infinite lists. Therefore, these languages have far more memory than their ‘rigorous evaluation’ counterparts. Furthermore, lazy evaluation reduces the number of counts by storing the results of all function calls as opposed to their parameters in the form of a lookup table which is used without the hassle of recalculating it when calling the same function the next time.

Take the example of computing the whole Fibonacci sequence. In Haskele, the order can be calculated using the following one-liners:

`fibs = 0 : 1 : zipWith (+) fibs (tail fibs)`

Here, ‘fibs’ is the name of the list that will store the sequence, ‘0: 1’ is the initialization of the list, and ‘zipWith’ is followed by an operation (in this case, add) on the respective components of the operator followed by two operators. The first operator is List Fives itself, while the tail operator returns the list without the first component. Notice how no attempt is made to limit the list. Since we have not actually attempted to retrieve a value from this list, it will not be counted at all. If we call an element in the list (fibs !!

**Haskell Hello World:**

Place the following line in the file named `hello.hs`

And play it with ghc (included on dvd)

`main = putStrLn “Hello, World!”`

Run it with the command GHC -O hello hello. HS

**Characteristics of Haskell**

Haskell defines a function in two ways, one where a single function takes more than one parameter (or argument) and returns a value, and the other where a function works on a single argument, which returns a function that accepts the second. As an argument, and so on. The second approach is a more ‘effective’ way of approaching programming and the result is more compact than before. Curry is named after the American mathematician Haskell Curry.

An example is given here to illustrate the difference between the two methods – the following function ‘hyp’ calculates the value of the angle of a right triangle given the length of two small arms.

**Curry:**

`hyp :: Float -> Float -> Float`

`hyp`

` x y = sqrt (x*x + y*y)`

**Non-vegetable:**

`hyp :: (Float, Float) -> Float`

hyp (x,y) = sqrt (x*x + y*y)

As can be seen, the first method is more compact (using fewer characters), and is therefore used conventively. Similarly, Haskell supports anonymous or lambda functions using the ‘\’ operator.

List understanding is a way of synthesizing a list using an expression, where each element of the list is logically related to a parameter. For example, to create a list of all numbers from 1 to 100, the following Haskell one-liner is valid:

`[ f | f <- [1..100]]`

We can include strong arguments to change our list during runtime. For example, a list where all the numbers in a list have squares l,

`[f*f | f <- l]`

Similarly, a list that stores all the factors of a number n can be expressed as,

`[f | f <- [1..n], n mod f == 0]`

(Here mode operator returns the remainder after dividing n by f)

Haskell’s comments are of three types – starting with single line comments – and ending with a new line character. Multi-line comments start with {- and end with – এবং and may include nested comments or code. The signature comment, a third type, aims to limit the code instead of the comment and start with \ begin {code করার and end with \ end {code যেমন as is commonly found in LaTeX. This allows Haskell programs to easily type in LaTeX documents.

Monads are one of the most important features of Haskell, and can be considered as the programmatic equivalent of an assembly line. The data input into a monad changes periodically as if it were in an assembly line. Formally, a monad is a way of describing a calculation as a sequence of steps. Monads are used to describe how a particular data type should behave when multiple activities are chained together in it, or how functions nested within other functions return values. Monads can be built for calculations that conduct I / O, change the status of their resident variables, or provide multiple values.

**Haskell’s implementation**

The most used (and fully featured) Haskell compiler is GHC (Glasgow Haskell Compiler) and is available for Windows, Mac and Linux (see it here). Other Haskell implementations such as lbc, Gofer and Yale Haskell are also available, but GHC is the de-facto standard for Haskell compilers. You can find the latest version of GHC on the DVD accompanying this month’s Fasttrack issue. Supports text editors such as Sublimetext, VIM, Intelligence and Eclipse Haskell.

**Haskell in the Wild**

Many real-world projects are partially or completely written off. Darkus is a source code management system that relies on a sophisticated system instead of snapshots and supports spontaneous branching. Large corporations such as Google, Facebook, NVIDIA, Bank of America and AT&T use Haskell internally for a number of support tools. Chordify is a popular chord transcribing service that takes a song from YouTube, SoundCloud or an uploaded audio file and displays the chords used in the song. Haskell is used to model the similarity of music between jags in the functional domain.

The New York Times used Haskell’s parallel array library to process images for 2013 New York Fashion Week. Silk is a tool used to express data visualization in a beautiful way, using Haskell to create infographics from their data.

**Haskell learning**

There are plenty of Haskell tutorials on the web and in print. The most recommended is Learn You A Haskell For Greater Good, available as a free HTML ebook here. edX, a popular online courseware site, offers a course called Introduction to Functional Programming that uses Haskell to illustrate effective programming concepts (see it here). Haskell’s School is a series of interactive web tutorials in the Haskell programming language (see it here), try Haskell allows you to run interactive haskel commands without having to install anything inside your browser

Visit here for tutorials on the other 15 hot programming languages.