Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala equivalent to Haskell monads

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

like image 586
Teja Kantamneni Avatar asked Feb 10 '10 13:02

Teja Kantamneni


People also ask

What are monads in Scala?

Monad in scala are a category of data types. Informally, anything that has a type parameter, a constructor that takes an element of that type, and a flatMap method (which has to obey certain laws) is a monad. A monad is a mechanism for sequencing computations.

Is Scala Option A monad?

We've used Option as an example above because Scala's Option type is a monad, which we will cover in more detail soon.

Is Scala future a monad?

Futures can be considered monads if you never construct them with effectful blocks (pure, in-memory computation), or if any effects generated are not considered as part of semantic equivalence (like logging messages).

Is Scala better than Haskell?

MORE POWERFUL TYPE SYSTEM. Both Scala and Haskell are statically typed languages, but Haskell's type system is arguably more powerful, in the sense that it provides more guarantees that are checked by the compiler. It also has superior type inference, which means that it places a lower burden on the programmer.


2 Answers

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) )
like image 157
oxbow_lakes Avatar answered Sep 28 '22 00:09

oxbow_lakes


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

like image 21
GClaramunt Avatar answered Sep 28 '22 00:09

GClaramunt