#![allow(unused, warnings)]
use std::boxed::Box;
#[derive(Debug)]
enum List<i32> {
Nil,
Cons(i32, Box<List<i32>>),
}
impl List<i32> {
fn head(&self) -> &i32 {
match self {
List::Cons(x, _) => x,
List::Nil => &1,
}
}
fn tail(&self) -> &List<i32> {
match self {
List::Cons(_, x) => &*x,
List::Nil => &List::Nil,
}
}
}
fn into_vec(x: List<i32>, mut r: Vec<i32>) -> Vec<i32> {
return match x {
List::Nil => r,
List::Cons(x, y) => into_vec(*y, {
r.push(x);
r
}),
};
}
fn fib(a: List<i32>, n: i32) -> List<i32> {
if n == 0 {
return a;
}
return match a {
List::Nil => fib(
List::Cons(1, Box::new(List::Cons(1, Box::new(List::Nil)))),
n - 1,
),
List::Cons(_, _) => fib(List::Cons(*a.head() + *a.tail().head(), Box::new(a)), n - 1),
};
}
fn main() {
let v = List::Cons(1, Box::new(List::Cons(1, Box::new(List::Nil))));
println!("{:?}", into_vec(fib(v, 20), Vec::<i32>::new()));
}