-
Notifications
You must be signed in to change notification settings - Fork 0
Changed List to Pair, and also re-implemented type_of #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
547fac6
8da73c2
5e3393f
657374b
0d4e29b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,37 +1,40 @@ | ||
| #[derive(Clone, PartialEq, Debug)] | ||
| pub enum Node { | ||
| Atom(Atom), | ||
| List(List), | ||
| Type(i64) | ||
| Atom(&'static str), | ||
| Pair(Box<Node>, Box<Node>), | ||
| Type(Type), | ||
| } | ||
|
|
||
| pub type Atom = &'static str; | ||
|
|
||
| // ===================================== | ||
| // List | ||
| // ===================================== | ||
| pub type List = Vec<Node>; | ||
| #[derive(Clone, PartialEq, Debug)] | ||
| pub enum Type { | ||
| Universe(i64), | ||
| Atom, | ||
| Pair(Box<Type>, Box<Type>) | ||
| } | ||
|
|
||
| pub fn cons(list: List, atom: Atom) -> Node { | ||
| let mut cloned_list = list.clone(); | ||
| cloned_list.push(Node::Atom(atom)); | ||
| Node::List(cloned_list) | ||
| pub fn cons(node: Node, atom: Node) -> Node { | ||
| Node::Pair(Box::new(node), Box::new(atom)) | ||
| } | ||
|
|
||
| pub fn car(list: List) -> Node { | ||
| list.first().unwrap().clone() | ||
| pub fn car(node: Node) -> Result<Node, &'static str> { | ||
| match node { | ||
| Node::Pair(a, _) => Result::Ok(*a), | ||
| _ => Result::Err("Can only apply car to Pair") | ||
| } | ||
| } | ||
|
|
||
| pub fn cdr(list: List) -> Node { | ||
| let mut cloned_list = list.clone(); | ||
| cloned_list.remove(0); | ||
| Node::List(cloned_list) | ||
| pub fn cdr(node: Node) -> Result<Node, &'static str> { | ||
| match node { | ||
| Node::Pair(_, b) => Result::Ok(*b), | ||
| _ => Result::Err("Cannot apply cdr to Pair") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's better to use a dedicated There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. However, for this case |
||
| } | ||
| } | ||
|
|
||
| pub fn type_of(node: Node) -> Node { | ||
| pub fn type_of(node: Node) -> Type { | ||
| match node { | ||
| Node::Atom(_) => Node::Type(0), | ||
| Node::List(_) => Node::Type(0), | ||
| Node::Type(n) => Node::Type(n + 1) | ||
| Node::Atom(_) => Type::Atom, | ||
| Node::Pair(a, b) => Type::Pair(Box::new(type_of(*a)), Box::new(type_of(*b))), | ||
| Node::Type(Type::Universe(n)) => Type::Universe(n + 1), | ||
| Node::Type(_) => Type::Universe(0) | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.