views:

476

answers:

2

I had some experience in Haskell and currently learning Scala. Am wondering whether there is something equivalent to Monads in Scala??

+8  A: 

You probably want to check out scalaz; it's been strongly influenced by Haskell. Indeed, it has often been asked of one of the prime contributors why they aren't just using Haskell, as they seem to like it so much!

Scalaz makes heavy use of implicits in order to decorate structures with their monads. For example:

val fibs = (0, 1).iterate[Stream]( i => i._2 -> (i._2 + i._1) ).map(_._1)
println( fibs.take(10) )
oxbow_lakes
Actually the @VonC comment on my question gave me more details. But I can't accept that answer, also @oxbow_lakes showed me something new. Thank you guys for help
Teja Kantamneni
BTW: the authors of Scalaz *do* use Haskell. If you look at the sourcecode of Scalaz, you will see that they use Haskell for the build system instead of Ant, for example. Also, Tony Morris (the main author) *is* a pretty well known Hakell programmer. He is also the author of FunctionalJava, which is a library that implements Functors, Applicative Functors, Bifunctors, Monads, Comonads and Arrows for Java (just like Scalaz does for Scala). And as for why use Scala: Scala's type system and language is actually in several areas more powerful and expressive than Haskell's.
Jörg W Mittag
@Jorg - emphasis changed - I know Tony uses Haskell, I was implying that it had been asked as to exactly *why* he ever used scala!
oxbow_lakes
That snippet looks awesome. But how to work it? There is no iterate function on Tuple2. I've included scalaz on the classpath, but still no joy.
Synesso
Have a look at the scalaz examples. You need to import scalaz._ and be using 2.8. The Tuple2 is implicitly converted into the Identity monad
oxbow_lakes
+2  A: 

I think is worth noting that Scala's "for-comprehension" is equivalent to Haskell's monadic "do"

GClaramunt