11 May 2014, 00:00

My thoughts about why Haskell is not popular

Despite Haskell programming language is not young language, it has a really great and helpful community, it has big amount of libraries, unfortunately Haskell is not popular programming language. I’m not against this programming language, opposite, haskell’s unpopularity makes me sad. I will not write about haskell popularity in industry, I’ll try to explain my thoughts about “Why Haskell is not popular” not as Haskell expert, not as professional Haskell developer (i don’t get payment for Haskell programming), but from position of usual developer who started to learn/experiment with Haskell some months ago and already don’t afraid monads :). I can’t So I will try to explain my opinion about Why Haskell is not popular in this post.

Myths

I’m not long time with Haskell and I don’t know all myths/stories/jokes and other funny things about Haskell, I know one popular myth (for my look it’s a myth) that Haskell is a difficult. Difficult to learn, difficult to use, not important, it is difficult. I think it’s old myth, I remember it before I started to learn Haskell. I think that it is really myth and Haskell can be not so hard as you can think about it. It is not difficult, it is different. If you’re using python,ruby,C++,javascript,java,something else… Haskell will be really different. Why it is different? There are many reasons for this like: another programming paradigm, lazy evaluations, different concepts and etc… But of course it is not a big problem and relatively easy to solve. Developer just need to spend more time to learn it than with another programming languages like python,javascript and etc… (Except C++ of course :))

Lack of documentation

The Lack of documentation is a first problem in this list that related with practical side of Haskell usage. I don’t mean that Haskell as language has bad documentation, it is not true. I’m speaking about Haskell libraries documentation. It’s the one of big problem that stands on the road to Haskell popularity. Sooner or later after start of Haskell learning developer will want to use libraries written in Haskell. We can easily find library by name, or functions/data types name with Hoogle, it is good, but how to use this libraries if developer doesn’t program in Haskell a couple years and library has no or has but bad documentation. For example some time ago I played with WAI and i was need in websockets. Fortunately WAI has wai-websockets package but let’s look on it’s documentation. How to use it? I don’t know how about you, but I don’t understand. I see only one way out of this: to read wai-websockets source code or examples. Is it good? I’m not sure.

Standard Library

Now let’s talk about Haskell’s standard library. On my look it has many really useful things for haskell and has really little amount things for Real World play. What it means when I am telling about Real World. It’s simple, i mean that Haskell standard library has many things like Control.Category, Control.Arrow, Data.Typeable and etc… Again, I don’t know how is it for you, but for me and I think other newbie Haskell developers: Category, Arrow and other magic words are just words without any meaning. Not, i know Arrows, Monads are very useful in Haskell, but where is the something like Network.TcpClient, Network.HttpServer and etc… I know that Haskell has separate libraries for TCP, HTTP and other network and not only network things. But imagine, for exmaple I just started with Haskell and I want to write simple example like sending HTTP request and getting response, i need to understand where to find library for this, how to install it and etc… I don’t speak that Haskell standard library must have all things for all case, but things like http client in stdlib is a standard case. Or i’m wrong? Let’s look on golang for example. I think that it has a perfect standard library. Look on it and haskell standard library, do you feel difference? Golang is only five years and Haskell is 24, so big difference. Of course I have no statistic, but I see that golang is much popular for this moment. I don’t think that it is main reason but one of.

Why to learn

I think it’s not only Haskell problem, but other languages too. I see only one answer for this question: To get/improve knowledges in functional programming and look on your working programming language with other eyes after it. Yes, getting new knowledges it is very good. But what about practical side? I know that somewhere developers who get money for Haskell programming, but I don’t see tons of vacancies for Haskell developers like for javascript, ruby or other tools and it is problem. Let’s look in another side. For example i want to start learn Haskell for my super-cool pet project. But why Haskell in this case? For web development I can take habitual ruby/python/php, for system programming I can take C/C++ or maybe Rust, for concurrent programming I will choose erlang. So why i need to learn new programming language and in addition so different from my standard tools?

Lazyness

When we start to learn Haskell we can read something like this: Haskell - general purpose programming language with non-strict evaluation. I think that many developers know about lazy evaluations, but I am really not sure that all of they knows how it works, how to correctly use it and etc… In this way, I as beginner in Haskell must learn not only another programming paradigm, but also another evaluation order. It is much harder, because it is much implicit. For example let’s take a look at popular ByteString library. It provides two implementations lazy strings and strict. But I still don’t know where to use first and where to use second.

Different abstractions

It is problem of Haskell learning. Haskell uses different abstractions than other programming languages. And if you know python for example it will be much easy to learn ruby than Haskell. Let’s look on simple echo example. You need to read input from stdin and prtin this string again. How we do it with python language:

import sys
data = sys.stdin.readlines()
print data

All is transparent enough. We do this task with all imperative programming languages in this way. First we reading from stdin and put result to a variable and than pass this variable to the printing function. Let’s look at the same Haskell example:

main = do
  getLine >>= putStrLn

Ok. Developer can guess about getLine and putStrLn, but what is it >>=. If we open documentation we will read something like this: >>= combine two monadic values… “combine two monadic values…”. What is it Monad, How to use Monad and many many different questions with not easy answers sometimes. And it is only monads (concept which standard developer could not hear never), but there are many different concepts like Functors, Comonads and many many others which you can’t meet in standard programming languages.

Conclusion

So it was a short list of my thoughts why Haskell is not popular. I am very interesting what do you think about Haskell popularity. In the end I want to remind that all from this post only my opinion and if you’re agree or disagree with me write me a comment.

13 Jan 2014, 00:00

Get function execution time in Haskell

Some times ago I encountered with question: How to get function execution time in Haskell program? I asked this question at StackOverflow, and got some useful answers. Here i will try to describe how to do it. For example we have simple haskell program which will calculate sum of prime numbers which are between 0 and 10000. Something like this:

module TimingTest where

main :: IO()
main = do
	putStrLn "Start"
	putStrLn ("Result: " ++ show primesSum)
	putStrLn "Done"


--
-- Returns True if `n` is prime
--
isPrime :: Int -> Bool
isPrime n = null [ x | x <- [2..n - 1], n `mod` x  == 0]

primesSum :: Int
primesSum = sum [x | x <- [2..10000], isPrime x == True]

Yes, it’s not the best implementation of prime numbers, but it’s not important at the current moment. Let’s see what we have for checking execution time.

Time

First of all, the simplest method to get execution time is time command. Compile our source code and execute:

$ time ./TimingTest

We must get something like this:

real 0m3.503s
user 0m3.492s
sys 0m0.004s

GHCI

The second method is just add :set +s in ghci before the function execution. Of course it’s not the best method, because functions run much slower in `ghci.

TimeIt

The third method is to use TimeIt haskell library by Lennart Augustsson. Very little, but useful library with simple API. It consist only from two functions:

timeIt :: IO a -> IO a -- | Wrap an IO computation so that it prints out the execution time

and

timeItT :: IO a -> IO (Double, a)Source -- | Wrap an IO computation so that it returns execution time is seconds as well as the real value.

Let’s remake our main function as:

import System.TimeIt

main :: IO()
main = do
 	putStrLn "Start"
	timeIt $ putStrLn ("Result: " ++ show primesSum)
	putStrLn "End"

and will get something like this:

Start
Result: 5736396
CPU time: 8.22s

Criterion

Criterion library provides a powerful but simple way to measure software performance by Bryan O’Sullivan. For using it, will remake again our main function as:

module Main where

import Criterion.Main

main :: IO()
main = defaultMain [
       bgroup "Prime numbers." [bench "prime numbers benchmark" $ whnfIO (putStrLn $ show primesSum)]
       ]

and as a result we will get:

estimating clock resolution...
mean is 3.760062 us (160001 iterations)
found 3006 outliers among 159999 samples (1.9%)
2461 (1.5%) high severe

estimating cost of a clock call...
mean is 98.08812 ns (28 iterations)
found 4 outliers among 28 samples (14.3%)
4 (14.3%) low severe