Skip to content

Instantly share code, notes, and snippets.

@tpolecat
Last active October 10, 2021 06:51
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tpolecat/1227e22e3161b5816e014c00650f3b57 to your computer and use it in GitHub Desktop.
Save tpolecat/1227e22e3161b5816e014c00650f3b57 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