Skip to content

Instantly share code, notes, and snippets.

@barambani
Forked from tpolecat/listt.scala
Created January 9, 2019 21:31
Show Gist options
  • Save barambani/fe0ba8b1f875b8addb5be148b45e0095 to your computer and use it in GitHub Desktop.
Save barambani/fe0ba8b1f875b8addb5be148b45e0095 to your computer and use it in GitHub Desktop.
A demonstration in Scala of failed associativity of ListT. Example taken from https://wiki.haskell.org/ListT_done_right
// addCompilerPlugin("com.milessabin" % "si2712fix-plugin" % "1.2.0" cross CrossVersion.full)
import scalaz._, Scalaz._
implicit def f2k[F[_], A, B](f: A => F[B]) = Kleisli(f)
val a: Int => ListT[List, Int] = {
case 0 => ListT(List(List(0, 1)))
case 1 => ListT(List(List(0), List(1)))
}
println(((a >=> a) >=> a ).run(0))
println(( a >=> (a >=> a)).run(0))
// ListT(List(List(0, 1, 0, 0, 1), List(0, 1, 1, 0, 1), List(0, 1, 0, 0), List(0, 1, 0, 1), List(0, 1, 1, 0), List(0, 1, 1, 1)))
// ListT(List(List(0, 1, 0, 0, 1), List(0, 1, 0, 0), List(0, 1, 0, 1), List(0, 1, 1, 0, 1), List(0, 1, 1, 0), List(0, 1, 1, 1)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment