Created
April 14, 2016 17:04
-
-
Save anonymous/d2591457abad090900214f2566f3bc7f to your computer and use it in GitHub Desktop.
Shared via Rust Playground
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
#![feature(trace_macros)] | |
trace_macros!(true); | |
/// Piano numbers | |
#[derive(Debug)] | |
struct S<T>(T); | |
trait ToInt {fn to_int(self) -> i32;} | |
impl ToInt for i32 {fn to_int(self) -> i32 {self}} | |
macro_rules! to_peano { | |
(0) => (0); | |
(1) => (S(0)); | |
(2) => (S(S(0))); | |
(3) => (S(((0)))); | |
(4) => (S(to_peano!(3))); | |
(5) => (S(to_peano!(4))); | |
(6) => (S(to_peano!(5))); | |
(7) => (S(to_peano!(6))); | |
(8) => (S(to_peano!(7))); | |
} | |
macro_rules! plus { | |
($n:expr, 0) => ($n); | |
($n:expr, S($($m:tt)*)) => (plus!(S($n),$($m)*)); | |
//($n:expr, $($m:tt)*) => (plus!($n,to_peano!($($m)*))) | |
} | |
fn main() { | |
print!("{:?}\n", plus!(to_peano!(3), to_peano!(2))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment