Last active
December 13, 2015 17:18
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> 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