Skip to content

Instantly share code, notes, and snippets.

@folone
Last active December 13, 2015 17:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save folone/4946543 to your computer and use it in GitHub Desktop.
Save folone/4946543 to your computer and use it in GitHub Desktop.
Hanoi towers at compile time become very easy with dependent types. https://gist.github.com/travisbrown/3772462
> shapeless-core/console
[warn] Credentials file /home/folone/.ivy2/.credentials does not exist
[info] Compiling 24 Scala sources to /home/folone/workspace/shapeless/core/target/scala-2.11/classes...
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.11.0-20130205-141957-132e09fc2e (OpenJDK 64-Bit Server VM, Java 1.7.0_09).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import shapeless.SingletonTypes._
import shapeless.SingletonTypes._
scala> :paste
// Entering paste mode (ctrl-D to finish)
def hanoi(n: Int, a: String = "A", b: String = "B", c: String = "C"): String = {
if (n == 0) ""
else {
hanoi(n-1, a, c, b) + "from " + a + " to " + b + "; " + hanoi(n-1, c, b, a)
}
}
// Exiting paste mode, now interpreting.
hanoi: (n: Int, a: String, b: String, c: String)String
scala> implicitly[^(hanoi(3))]
<console>:12: error: could not find implicit value for parameter e: String("from A to B; from A to C; from B to C; from A to B; from C to A; from C to B; from A to B; ")
implicitly[^(hanoi(3))]
^
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment