

What kind of things are easy in Haskell and hard in Scala, and vice versa? - gtani
http://programmers.stackexchange.com/questions/51245/what-kind-of-things-are-easy-in-haskell-and-hard-in-scala-and-vice-versa

======
esmooov
If you want some exact tasks that seem to be easier in Scala or Haskell, I
think that if you take a truly skilled and experienced programmer in each
language most tasks can be accomplished quickly, easily and efficiently. For
what it's worth, here are some less deep tasks that seem easier in one
language than the other:

Pattern matching/data extraction: Both Haskell and Scala have powerful pattern
matching capabilities for extracting data from complex types in a simple and
clear fashion. However, do to some of Scala's OO features such as case classes
and extractors, elaborate pattern matching can be set up quite elegantly. A
good example of this in action is the lift-json library that allows users to
query a large JSON object in a variety of ways (LINQ,Xpath,etc) and decompose
it swiftly. Haskell's pattern matching is accomplished through it's data
constructors. This leads to simpler pattern matching on small structures but
can quickly devolve into spaghetti with increasingly complex structures.

2\. Laziness: Haskell wins this round as most of Haskell's types are lazy by
default and you have to work hard to make them strict. In Scala, laziness is
accomplished through either the Stream type of Collection views. Say for
instance you have a 1000 item list, map 2 functions in succession that can't
be composited and then return some arbitrary numbers of initial elements
(perhaps a convoluted example) this is how you would do it in Haskell (this is
not how you should ever write this but say, for the sake of argument you had
to):

take 3 $ map (\a -> a * 10)­ $ map (\a -> a+1) [1..]

As you can see, this automatically had to happen lazily as it was performed
against an infinite list. Compare the same thing in Scala:

List.range(1,1000000).view.map(_+1).map(_ * 10).take(3).force

For the sake of presentation, we just used a long list for the Scala example.
If you want to check the laziness, try do this with the view and force and
watch how long it takes. We could have also done this with Streams but we will
look at this in the next point.

3\. Infinite lists. Once you work with infinite lists in Haskell, you will
want to use them all the time. In Haskell you just write, say,

take 3 [1..]

to make an list of all positive integers and take the first 3.

In Scala:

def posints(a:Int): Stream[Int] = a #:: posints(a+1)

posints(1).take(3).force

Now, why these three examples of things easier in one or the other? I think
that they exemplify the real difference between Haskell and Scala: In Scala,
complex things are easier and easy things are more complex. In Haskell, easy
things are easy and complex things are complex. This is, of course, a gross
oversimplification and there are many caveats: Haskell does functional stuff
easier, Scala does mixed paradigm stuff well, etc. Whatever. Every explanation
is an oversimplifcation of something more organic.

I would be happy to post more example of differences between Haskell and
Scala. They are both so rad.

