Created
July 24, 2015 22:31
-
-
Save jnordwick/a01ce25acc8c13b4da34 to your computer and use it in GitHub Desktop.
stupid enum tricks
This file contains hidden or 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
#[derive(Debug)] | |
enum List<T,L> { | |
Null(T), | |
Cell(T,L), | |
} | |
use List::*; | |
type Nil<T> = List<T,()>; | |
type Single<T> = List<T,Nil<T>>; | |
type Pair<T> = List<T,Single<T>>; | |
type Triple<T> = List<T,Pair<T>>; | |
type Quad<T> = List<T,Triple<T>>; | |
fn car<T,L>(x: &List<T,L>) -> &T { | |
match *x { | |
Cell(ref v,_) => v, | |
Null(ref v) => v, | |
} | |
} | |
fn cdr<T,L>(x: &List<T,L>) -> &L { | |
match *x { | |
Cell(_,ref v) => v, | |
Null(_) => unreachable!(), | |
} | |
} | |
fn main() { | |
let f : Nil<u8> = List::Null(0u8); | |
let g : Quad<u8> = Cell(1u8,Cell(2u8,Cell(3u8,Cell(4u8,f)))); | |
println!("{:?}", car(cdr(cdr(&g)))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment