📜 ⬆️ ⬇️

Prelude or how to fall in love with Haskell

Good day, dear habrovchane. It is always difficult to start. It’s hard to start writing an article, it’s hard to start a relationship with a person. It is very difficult, it happens, to start learning a new programming language, especially if this language destroys all the ideas and principles that you had, if it contradicts the usual picture of the world and tries to break your brain. An example of such a language is Haskell.

Why not just take and change to Haskell?

“You are all spoiled by the imperative style!” - this is what our first teacher of functional languages, a wonderful person, scientist and organizer Sergey Mikhailovich Abramov, told us. And he was right. How difficult it was to forget about variables, assignments, algorithms and start thinking functions. You can sit down and write a program in Haskell, no problem. You can build a nested loop, use variables and assignments, yes, easy! But then, you look at your code and say: “Who needs this functional area? Yes, I will write to C faster and better. ”

And this is all because you can not transfer the imperative programming style in a functional language. You can not force the fish to fly and the bird to swim. You can not be a duck, which does not really know how to swim and fly. It is necessary to descend in the function with the head, you need to feel their brain bones. Only in this way you will love this language will not be able to write on anything else. But how, how to do it?

Read the foreplay

The answer is simple. Just two words. Read the prelude! If you want to comprehend and love Haskell, if you want to learn pure functional style, read the prelude.
')
Prelude is a standard library of the most necessary functions, it is defined by the Haskell 98 standard. Each line, each point is verified and licked, there is nothing superfluous and imperative there. Reading the prelude, you feel emotions comparable to the emotions when reading an interesting fantastic book.

One of my favorite places in the prelude is the section where work with lists is described.
Take, for example, the map function.
map :: (a -> b) -> [a] -> [b] map f [] = [] map f (x:xs) = fx : map f xs 

You remember - we think functions.
What is the map function? At the input we have a certain function from the set A to the set B, and a list of elements from the set A. On the output we must obtain a list from the set B by applying the function to each element of the list.

This feature is a great example of one of Haskell’s basic working patterns. Map having the input parameters of the form f [] (function and empty list) will return an empty list. In other cases, f will be applied to the first element of the list, the resulting element will be added (colon) to the list formed by applying the map function to the tail of the list.
One of the main advantages of Haskell is that if you formulate a function in a mathematical language, it is usually a matter of a few minutes to program it.

More examples

And here's another miracle: the head and tail functions, well, just a pretty sight!
 head :: [a] -> a head (x:_) = x head [] = error "Prelude.head: empty list" 

 tail :: [a] -> [a] tail (_:xs) = xs tail [] = error "Prelude.tail: empty list" 

I think there is nothing to explain here. The underscore in Haskell patterns stands for "anything."
 null :: [a] -> Bool null [] = True null (_:_) = False 

Really, there are people who do not like it?

But the function length.
 length :: [a] -> Int length [] = 0 length (_:l) = 1 + length l 


And how is the class Eq defined in the prelude? You will laugh!
 class Eq a where (==), (/=) :: a -> a -> Bool -- Minimal complete definition: -- (==) or (/=) x /= y = not (x == y) x == y = not (x /= y) 

True dizzy beauty?

What am I talking about?

It would be possible to greet a lot of code samples, but why spoil your fun?
All beginners, everyone who wants to love Haskell one piece of advice - read the prelude. I read and try to write like in the foreplay. At least aspire. Reading the prelude, I understood Haskell philosophy and loved this language, which I wish you all!
Sorry, for the confusion, I guess I'm not a writer, I very badly described everything that was needed, but I really wanted to share with you a piece of love for Haskell and this small, but very useful advice. Read the prelude!

Source: https://habr.com/ru/post/203690/


All Articles