Skip to content

Instantly share code, notes, and snippets.

@anka-213
Forked from anonymous/playground.rs
Created April 14, 2016 17:04
Show Gist options
  • Save anka-213/32f760685409758fd31d87aa909a17c3 to your computer and use it in GitHub Desktop.
Save anka-213/32f760685409758fd31d87aa909a17c3 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
#![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