
A brief introduction to Haskell, and why it matters - DanielRibeiro
https://github.com/pchiusano/fpinscala/wiki/A-brief-introduction-to-Haskell,-and-why-it-matters
======
pdpi
> Haskell is in some ways a nicer language for functional programming than
> Scala, and if you are serious about learning more FP, we recommend learning
> it.

I'd take it as far as to say that, if you're a developer of any sort, you
should learn at least some Haskell at some point. It does things in such a
completely different way from more mainstream languages that the sheer mind-
opening effect of learning it is worthwhile.

~~~
JimmyM
I have about a half a year's worth of Racket (acquired over around a year and
half) under my belt, and intend to learn Haskell in the coming two years. Is
there anything startlingly different in Haskell compared to the LisPs?

EDIT: Thank you for the responses, I have bookmarked the relevant github page
and feel like my question has been well-answered :) Also, even more interested
by Haskell than I was before.

~~~
pdpi
The single biggest thing you'll notice is how much time you'll devote to
thinking about types, and how that makes your code much more declarative.

Even casually skimming through the GetOpts implementation in Cabal should give
you and idea of what I mean:

[https://github.com/haskell/cabal/blob/master/Cabal/Distribut...](https://github.com/haskell/cabal/blob/master/Cabal/Distribution/GetOpt.hs)

~~~
duncanawoods
I am not a Haskell programmer so I look at this with interest.

The biggest thing I notice is that type specifiers have comments next to them
akin to variable names describing what they are for.

This worries me. Comments are just so awful compared to clear meaningful
variable/function naming that propagate through the code when used. Does
Haskell discourage and even prevent clear naming or am I missing something? It
would be a tradeoff I couldn't accept.

~~~
mightybyte
Like quchen said, the comments are for the API documentation. They are
documenting that function's use of a type. The type alone frequently will not
communicate any semantic meaning. Consider this type signature.

    
    
        exp :: Double -> Double -> Double
    

You don't inherently know from the type which argument is the base and which
is the exponent. Double is not a name that you have control over, so we write
a comment for it that will automatically show up in the API documentation.
They only get names in the implementation.

    
    
        exp base exponent = ...
    

But these names are for the programmer, not the API documentation. You
actually _want_ these names to be separate from what is shown in the API docs.
Consider this function:

    
    
        map :: (a -> b) -> [a] -> [b]
        map _ [] = []
        map f (x:xs) = f x : map f xs
    

This code is very concise and easy to read. It is easy to read because the
names are small and the important thing is the pattern, not the actual meaning
of the names. This function is also extremely general, which means that
there's not much use in names. Position in the function means more than a
name. Here's what it would look at written with "clear" OO-style names:

    
    
        map :: (a -> b) -> [a] -> [b]
        map function [] = []
        map function (firstElement:restOfList) = function firstElement : map function restOfList
    

First of all, we see that sometimes we want to pattern match instead of using
a name. GHC would actually give a warning with this code saying that the name
"function" in the first case is never used. This is actually a very useful
warning that has helped me catch bugs on multiple occasions. Secondly, the
"clear" names here completely obscure our ability to understand the code. It's
just too much noise. Now, I'm not trying to get into the whole naming debate
here. The point I want to make is that there can be good reasons to not show
the parameter names in the API documentation, which is why you'll see comments
on type signatures for the purpose of auto-generated documentation.

~~~
dllthomas
You can relabel Double with a type alias. Really, adding a newtype gives you a
sort of partial equivalent of tagged arguments (requires tags, doesn't permit
reordering), and might be best practice when there's more semantic content and
no clear typical order (eg, you're passing price and quantity to makeOrder,
rather than base and exponent to exp). I make a habit of doing this with my C
(where a single element struct adds no overhead, much like a Haskell newtype).

~~~
mightybyte
Yeah, type aliases can be really useful for some kinds of type documentation.
But they seem a bit on the heavy side for one-off things like the exp and map
examples.

~~~
dllthomas
I agree that it doesn't make sense for exp or map, but not because they are
one-off; rather because they are already clear enough that it's not going to
add much.

For map in particular what are you going to rename? You can name unbound
parameters without a newtype:

    
    
        map :: (input -> output) -> [input] -> [output]
    

_might_ add a bit;

    
    
        map :: MapFunction a b -> [a] -> [b]
    

takes more away than it adds I think.

------
erjiang
Don't forget Hoogle[1], the type-based search engine! Let's say that I want to
combine a couple of my lists into one list. I know what my inputs look like,
and I know what I want my output to look like.

So I go to Hoogle, and search for "[a] -> [b] -> [(a, b)]". First result is
"zip".

Or I want to get only the things in a list that satisfy a predicate, but I'm
suffering from nominal amnesia. I type in "(a -> Bool) -> [a] -> [a]" and
Hoogle lets me know that I meant to write "filter".

[1]
[http://www.haskell.org/hoogle/?hoogle=%28a+-%3E+Bool%29+-%3E...](http://www.haskell.org/hoogle/?hoogle=%28a+-%3E+Bool%29+-%3E+\[a\]+-%3E+\[a\])

~~~
garretraziel
Squeak Smalltalk is better in it - you can actually type in arguments of
method and a result and it can find you method that, given these arguments,
returns this result.

So, for example you can type: 'hello'. 'HELLO' and it finds you 'asUppercase'
method of String object.

~~~
pkinsky
How does it perform this search? I can't imagine running all functions with a
matching type signature scaling to more complex examples.

~~~
kencausey
Well, in fact it does something like this. But what messages it tries is
actually limited. It has been a while since I looked at it and so don't
remember the details.

This is a very useful function of Squeak and I presume Pharo Smalltalk but I
think users of it should have some awareness that it has limitations and not
to simply assume that because a result is not found that it does not exist.

------
melling
Anyone have a practical guide to Haskell? For instance, how can I open a file,
split a line on a separator, then do something on the third column? Also, how
can I talk to MySql or Sqlite? Load a table then perform operations on the
loaded data? I've found the best way to learn anything is to find real uses in
my day to day work and start small.

In short, find some tasks for which I would use Perl|Python|Ruby and solve
them with Haskell.

~~~
pdpi
You're probably going to have a bad time trying to do it like that.
Perl/Python/Ruby are brilliant at what they do, and the problems they're best
at aren't necessarily problems that Haskell is very good at.

If you're interested in learning the language, I suggest starting with Learn
You a Haskell For Great Good[1] (first programming book I've ever considered
to be a page-turner), then following it up with Real World Haskell[2] (going
through that one now myself)

[1] [http://learnyouahaskell.com/](http://learnyouahaskell.com/)

[2]
[http://book.realworldhaskell.org/read/](http://book.realworldhaskell.org/read/)

~~~
polymatter
To expand on this, Haskell is excellent to make your own parser. Take special
note of say Chapter 16 Parsec of Real World Haskell[1]. This is the sort of
application where you would choose Haskell over Perl/Python/Ruby.

Of course parsing is something thats very important if you are making your own
DSL. And this is another application where Haskell really shines. This is why
the guys who do programming language research produce papers with code in
Haskell. Its orders of magnitude easier to make your toy language for
exploring evaluation order in Haskell than it is to do the same in Java.

Its easier to learn a language if you're building something you're interested
in though. Personally, I learnt a lot of Haskell implementing a web app with
Yesod[2].

Good luck.

[1] [http://book.realworldhaskell.org/read/using-
parsec.html](http://book.realworldhaskell.org/read/using-parsec.html) [2]
[http://www.yesodweb.com/](http://www.yesodweb.com/)

~~~
jlarocco
I don't really agree.

Python, at least, has the PLY library, which makes parsing (IMO) easier than
with Parsec.

Other languages, like OCaml, also make writing parsers very easy.

Haskell with Parsec might beat C with lex/yacc, but I don't think it's that
great compared to what else is available.

~~~
tel
Could you elaborate why you feel this way? I could also just vote for one or
the other, but I'd like to see discussion about the relative merits of PLY or
Camlp4 w.r.t. Parsec.

~~~
jlarocco
I can't deny part of the reason is that I'm simply more comfortable with
Python.

I think my main contention was the "orders of magnitude" claim in the comment
I replied to. Parsec is nice, but it's not "orders of magnitude" better than
PLY or camlp4 or other parsing tools in other languages.

------
boothead
Haskell also matters from a business point of view! I can tell you that hiring
Haskellers immediately connects you with the top of the food chain in terms of
hiring.

~~~
namelezz
Can you give me a list of companies that are looking for Haskllers?

~~~
boothead
I'm CTO of a team of Haskellers in London. We're not hiring at the moment, but
if you ping me on ben <at> dlstartup dot com I'll keep your CV around.
Otherwise there are 5-6 haskell startups and probably others on the finance
world.

Whereabouts are you?

In addition to mightybyte's list there's also

[http://www.borde.rs/](http://www.borde.rs/)

Barclays Capital

[http://www.sqreamtech.com/](http://www.sqreamtech.com/)

Tsuru

~~~
boothead
Also I heard that [http://pusher.com/](http://pusher.com/) have or will have a
bit of Haskell in production:

[http://pusher.com/jobs/platform_engineer](http://pusher.com/jobs/platform_engineer)

------
progman
Haskell's power is impressive but the "Cabal hell" is a serious problem.

Recently I tried to get into Haskell. I've installed EclipseFP and the first
sessions looked nice. But when I started to use external cabal modules I
always ran into serious trouble with versioning. Even recompilation doesn't
help a lot because there is always a complaining module.

Do I something wrong? I am thankful for advice.

~~~
m0a0t0
You may want to try cabal sandboxes. I think it's a similar idea to virtualenv
in python

~~~
progman
Thanks, I will take a closer look at it.

I hope this is a hierarchical solution so that I won't need to download the
whole cabal stack for any new project.

~~~
codygman
It is, you won't have to download the entire stack. You might want to try this
tutorial out:

[http://yannesposito.com/Scratch/en/blog/Holy-Haskell-
Starter...](http://yannesposito.com/Scratch/en/blog/Holy-Haskell-Starter/)

------
dschiptsov
Much more carefully written no-nonsense overview
[http://en.wikipedia.org/wiki/Haskell_features](http://en.wikipedia.org/wiki/Haskell_features)

------
stewbrew
Now if somebody could tell me how to install the latest haskell platform on
Ubuntu 12.04 and why cabal (with the haskell platform for 12.04) always gives
me those incompatibility warnings, I'd be happy to try it out.

------
general_failure
Can someone give examples of successful/popular Haskell programs?

~~~
pmahoney
Here's a list compiled by someone else:

[http://haskell-news.blogspot.com/2008/01/top-10-most-
popular...](http://haskell-news.blogspot.com/2008/01/top-10-most-popular-
haskell-programs.html)

Here's a GitHub search. Mostly libraries, but a few end-user programs:

[https://github.com/search?p=1&q=language%3Ahaskell+stars%3A%...](https://github.com/search?p=1&q=language%3Ahaskell+stars%3A%3E200&ref=searchresults&type=Repositories)

I didn't realize Pandoc was written in Haskell. Darcs of course, and xmonad
window manager are the ones that come to my mind.

~~~
taeric
It is amusing to see the numbers placed for "registered downloads." Those seem
low enough that they may as well just be left off.

------
tsax
So it fits what ESR says about Lisp?

"Lisp is worth learning for the profound enlightenment experience you will
have when you finally get it; that experience will make you a better
programmer for the rest of your days, even if you never actually use Lisp
itself a lot."

Is that right? As Haskell seems to be far less intimidating to approach than
Lisp, I may actually end up doing some. I fondly remember my junior level
programming languages class and doing assignments in Standard ML. Haskell
looks similar.

~~~
dllthomas
Most people find Haskell (especially the laziness, the type system and its
error messages, and the typeclass hierarchy) more intimidating than Lisp. If
you're familiar with Standard ML, you already basically know the syntax,
though, and the types and pattern matching will make you feel more at home.

------
Kiro
I'm a JS and PHP developer who wants to learn Haskell. I would prefer to build
something rather than just doing a tutorial or reading a book. Do you have any
recommendations on projects suitable for a beginner?

~~~
jasonwocky
My step 1 to understanding a new language has always been to build a simple,
file-serving HTTP server without using any HTTP-specific libraries.

~~~
arms
I really like this suggestion. I'm in the process of learning Haskell now, and
plan on using this as a practical learning exercise.

Out of curiosity, how difficult did you find this to complete?

------
drblast
One cool thing about Haskell that really struck me and that usually isn't
emphasized is partial application of function arguments.

[http://www.haskell.org/haskellwiki/Partial_application](http://www.haskell.org/haskellwiki/Partial_application)

I'm not sure if it's unique to Haskell, but it's such a great feature that
I've wished for in other languages since learning about it. You can replicate
it by manually writing curried functions, but having the language do that for
you is incredibly useful.

~~~
Dewie
You get this kind of thing "for free" in a concatenative language, e.g. a
stack based language. Though having to mostly rely on stack-combinators (this
depends on the language and/or idioms of that language, not something that is
inherent in concatenative languages) for writing functions might feel too
limiting.

------
brlewis
What's with using "strict" for the opposite of lazy evaluation? Has the term
"eager" fallen out of fashion? Strict seems overused to me.

------
ExpiredLink

         main :: IO ()
         main = putStr "Hello world!!"
    

A side-effect in the very first program. I'm disappointed!

~~~
dllthomas
That's not a side effect - it's _the_ effect!

------
catilac
I've been wanting to make time to learn Haskell. But I have this problem, when
learning any language, where I don't know what to build, but want to take a
learn by doing approach.

What are some good things to build when getting into a new language? Data
structures? Chat server?

~~~
jberryman
I really love writing libraries. Haskell has a type system that supports
defining really solid abstractions (I.e. code that doesn't even permit wrong
usage). Creating APIs like that is extremely satisfying. Maybe take a look on
hackage and see if there's something missing that you'd like to see and make
it.

------
misnome
The problem with learning Haskell is I just can't shake the "Research"
feeling. Learning Erlang looks a harder slog, but with the benefit of a "Real
world" feeling, whether correct or not.

~~~
Dewie
It's funny to me how 'research' has become a put down. aka "research toy" vs
"getting REAL work done".

~~~
codygman
Most of the time it's because those people can't make the connection between
the research and the "real world". The worlds are very blurred in my
experience.

------
jbeja
Why don't consider Clojure as an available option to Haskell or Scala?

~~~
namelezz
.syntax LISP with comfortable is everyone Not

~~~
metaphorm
LISP is prefix notation, not just reversed order. An expression should begin
with a function (a verb) and be followed by a list of its arguments (the
nouns).

(not (comfortable everyone LISP-syntax))

~~~
dllthomas
I'd expect the quantifier to take the predicate as an argument, rather than
the other way around.

(not (everyone comfortable LISP-syntax))

~~~
metaphorm
probably better. I was thinking everyone would just be data rather (a list of
people or something) rather than a quantifier function.

~~~
dllthomas
Right, but then you'd presumably want (every (map ...)).

I'm nitpicking, of course... just taking the opportunity to think lispy.

------
jayvanguard
The language that has more lines of tutorials and articles written about it
that actual lines of code.

~~~
quchen
Just like C++! (Yes, I too made that up on the spot.)

------
pekk
It doesn't really matter, it's yet another language that can be used, that
some people like and in this case really want to shove down everyone's
throats.

I guess it's not getting enough uptake organically, so once more to HN for the
throat-shoving.

~~~
codygman
Name one language that got popular with no one posting/talking about it.

