Tuesday, May 3, 2011

Learning Scala or Haskell

I'm considering dipping my toe in the functional programming world, and wondering if it would be better to start with Scala or Haskell. I'm coming at this primarily as a Python programmer. My only real functional programming experience with functional programming is using Scheme in an intro comp-sci class over a decade ago.

Some of the comments in Podcast #50 about Scala being more pragmatic than Haskell tend to push me towards Scala. While this is primarily a learning exercise, I'd still like to be able to put it to some practical use. However, I'm interested in hearing other people's opinions.

Edited to add: Including suggestions for other languages beyond Scala and Haskell.

From stackoverflow
  • The benefit of Scala is that it is a object-oriented and functional language which runs on the Java Virtual Machine.

    I believe that the primary audience which would benefit the most from Scala would be Java developers, as Scala is targeted at the JVM, it can interoperate well with Java libraries.

    Haskell on the other hand is a purely functional language, so it would probably be a better choice for learning very strict functional language. Since you say you're a Python programmer, maybe Haskell would be a good choice.

    However, if your goal is to leverage existing libraries and platforms and get the benefits of functional programming at the same time, maybe Scala would be a better choice.

    Valentin Jacquemin : I stumbled upon a nice advocation for Scala on Artima yesterday, take a look: http://www.artima.com/scalazine/articles/programming_style.html
  • As for the practical use argument, you might also be interested in Clojure.

    Toby Hede : Weird ... this is a question I just posed through twitter: scala or clojure?
  • I am thinking about learning Scala myself.

    I think it has much more "real-world" potential as:

    • it runs on the JVM (and the JVM is one of the best platforms around)
    • it is getting some real uptake through orgs like Twitter
  • Ok from a totally non technical/academic(==religious) point of view all I can say is:

    Now that Twitter has began using Scala, it seems that Legion has become less satisfied with Ruby and Legion is learning Scala now. This means that Scala will have the mindshare of Legion which is great if you want to get webapps done.

    Robert Gould : @*Legion* Sure downvote, but we know it's true
  • Don't limit yourself to Scala or Haskell. You should probably have both Erlang and Scheme on your list of choices. I'd actually recommend you actually picking at least two of the languages and try them out (solving a few Project Euler problems with them, for example).

    Hynek -Pichi- Vychodil : I recommend Erlang. It is really simple to learn (especially functional-sequential part of language) and big joy.
  • While Scala may be more immediately pragmatic, as it runs on the JVM, It's worth noting that Haskell is a major influence in languages such as Clojure and F#, as well as for some of the newer features of Python and C#. Ultimately, it depends on your goals. If your aspirations are purely academic, purely functional Haskell might be the better choice, later moving into something like F#, Scala, or Clojure as it fits your problem. If your focus is pragmatism, particularly if you're targeting the JVM, it might be better to start off with Scala or Clojure.

    You may want to see what Gabriel has to say.

    TrayMan : Haskell may have influenced F#, but the language is almost entirely based on Objective Caml. Standard ML is worth mentioning in the same context, as O'Caml and SML are closely related.
  • If your aim is to use the language you are learning in everyday tasks soon, you will probably want to choose Scala. You can use elements of functional, procedural and OO languages. You'll probably start out with a more procedural style and move from there. You will be able to interface with Java code and run your code wherever you have a JVM.

    If you really want to learn functional programming, choose Haskell. It's a pure and very mature FP language. You'll be forced to think and act the FP-way. For a beginner i'd recommend starting using an interpreter first (Hugs or, as suggested by commenters, ghci), instead of compilation. That way, you can start out by defining simple computations (factorial, fibonacci numbers, sorting etc.) and interactively calling them in the interpreter, moving to more complex stuff along the way, without worrying about "real" IO too soon (it's a bit more complex in Haskell).

    You can learn almost any FP concept in Haskell (algebraic types, type classes, monads), and infinite streams are supported out-of-the-box (just using the definition of lists and lazy evaluation). There's an excellent testing tool (quickchek) and you'll get a lot of support from the type system. Furthermore, there's the community report (http://haskell.org/communities/), which is basically a single place where you can find almost every project, tool and activity related to Haskell.

    shapr : I agree with everyday tasks vs really learning FP, but I think ghc is better than Hugs in almost every way. And yes, QuickCheck is amazing!
    rtperson : I haven't used Hugs, but GHCi is a perfectly fine interpreter -- I've been happily using it for months now.
    extempore : There are things you can do in haskell you can't do in scala, but infinite streams are not among them. scala> Stream from 1 take 100 sum res0: Int = 5050
  • Hi, Scala is supported by Java technlogy too (the creator - Martin Odersky - worked in Java compiler).

    If you code in Java too, you can see Jaskell: it a scripting-functional language that I think similar to Haskell (Neal Ford speak about this very well in him book The Productive Programmer).

    I never coded in Haskell but I read that it's very academic; Scala is a language built for Java world.

  • What about F#? Really pragmatic language with all the cool functional and OO features targeting the CLR

  • I was in the same dilemma few months ago . I choose haskell because it has great book available to you for learning, which for me was really important . Also, there is a good book club group . Since your goal is to learn functional programming , why not go all the way and choose haskell instead of some confusing hybrid. Also, there is wealth of libraries in haskell too, if you want to get something practial out of it.

  • Haskell will probably teach you more about functional programming, because it forces you to think in a functional way. With Scala it's easier to interface with other JVM languages and libraries, so it might be better for production work, but with it it's also easier to think in a non-FP way.

    Myself I'm right now starting to learn Scala, because I'm planning on using that soon in some real projects. I'll learn it by following a TDD tutorial which I created, so that I have the tests in Java but I write the production code in Scala. That should give me fast feedback on whether I have understood Scala's concepts correctly.

  • Depends on what are your goals, I really like Scala (I think is an awesome language) and I find it very practical: you can do OO, you can do FP, or a mix of both, you can deploy in the JVM and use the thousands of libraries available for it. I feel very comfortable in Scala but I suspect that is mainly because my main experience is with Java. After I got comfortable with the basics of FP using Scala, I started to learn Haskell (but you can easily go straight to it). The main difference from the FP (learner) point of view is in Scala, things like immutability and lazy evaluation are optional, and you can always revert to the "old ways" of doing things, in Haskell you don't have any safety net and that forces you to learn (with the associated pains). If you want to learn something practical but with really advanced features, Scala would be my choice, if you want to be FORCED to learn new stuff that can blow your mind, Haskell is the choice. But, why no both? I know learning time is a scarce resource but, for me at least, is easy to switch back and forth form Scala to Haskell. Learning Scala helps me to learn Haskell and learning Haskell helps me to learn Scala :)

  • You talk about

    dipping my toe in the functional-programming world

    These days, Haskell is the 800-pound gorilla of the functional-programming world. Other languages mentioned are often specializations or hybridizations of functional programming with other worlds.

    • Scala is a hybrid of functional and OO, with interesting mixins and module constructs.

    • F# is a hybrid of functional and .NET.

    These languages have hybrid vigour and are very interesting, but they don't meet your stated goal of entering the functional-programming world. Other languages to consider are Standard ML or Scheme, but neither has total functional purity checked by the compiler.

    A good toe-dipping paper is Why Functional Programming Matters by John Hughes, who is also an author of the excellent QuickCheck tool mentioned in other answers.

  • If you want to learn functional programming, personally, I would recommend Clojure. However, Haskell will teach you the deep and dirty concepts of functional programming. Scala indeed allows you to program in the functional /style/, but it also allows you to program in the imperative style. I don't think Scala is a very good language to teach functional programming itself, but it's a great language and I would recommend it to anyone who just wants to learn a cool new language.

    Clojure and Scala both run on the JVM and have access to any Java library in existence, and that is a factor you must consider if you intend to do any real, big projects with either language.

    Some people will say Haskell isn't a practical language, and that real-world projects are next to impossible with it. Personally, in the hands of an experienced Haskell programmer, I've seen magic worked with GHC as a wand.

    Short Form: If you want to learn either languages /just/ to learn functional programming, go with Clojure or Haskell. If you want to learn a language for fun, or any other reason, I recommend Scala or Clojure.

  • I would recommend learning Scheme first, as it is simple to learn,, its more important to learn how to think about programming a problem using a functional approach, there http://www.scheme.com/tspl3/ is a good resource to learn scheme.

    Rayne : That's why I recommended Clojure. :D
  • I was in a similar position to yourself about a year ago and took the decision to learn Scheme after having my eyes opened from my Java induced slumber by Ruby. Over the course of the year I have become quite proficient in Scheme and have learned to love it partly because of it's functional bias e.g. higher order functions but also because of it's Lisp like macros.

    I'm now in a position though that I understand functional programming and how to benefit from it but find that there are no jobs and few libraries for Scheme so I've evaluated potential successors. From a shortlist of Ocaml and Haskell (both of which have job opportunities and plentiful libraries), I've decided to go for Haskell because at this point in time, it looks like a more practical option than OCaml - specifically GHC supports true multithreading and parallel processing with FFI libraries whereas Ocaml doesn't. It also produces faster code and forces a pure functional approach whereas OCaml (as with Scheme) allow a mixed approach. OCaml also has disgusting syntax.

    Having said that Haskell is an order of magnitude more functional than Scheme is so whilst I thought I was being clever in Scheme, it turns out that that was just the beginning of what can be done with Haskell so it could be a pretty steep learning curve as a first functional language.

0 comments:

Post a Comment