From 4b11d9fbd2b4ef0bc4fd3bfba18c3bd2ce10ea73 Mon Sep 17 00:00:00 2001 From: bnewbold Date: Mon, 25 Apr 2016 23:29:08 -0400 Subject: rust: more car/cdr/cons tweaks (still needs review) --- rust/spectrum.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/rust/spectrum.rs b/rust/spectrum.rs index b61e3ad..3a0362f 100644 --- a/rust/spectrum.rs +++ b/rust/spectrum.rs @@ -520,7 +520,7 @@ fn apply_action(list: &Vec, &SchemeExpr::SchemeList(ref list) => { Ok(list[0].clone()) }, - _ => Err(format!("cdr only takes lists")) + _ => Err(format!("car only takes lists")) } }, "cdr" => { @@ -529,9 +529,13 @@ fn apply_action(list: &Vec, } match &args[0] { &SchemeExpr::SchemeList(ref list) => { - Ok(SchemeExpr::SchemeList(list[1..].to_vec())) + if list.len() > 1 { + Ok(SchemeExpr::SchemeList(list[1..].to_vec())) + } else { + Ok(SchemeExpr::SchemeNull) + } }, - _ => Err(format!("car only takes lists")) + _ => Err(format!("cdr only takes lists")) } }, "cons" => { @@ -539,12 +543,15 @@ fn apply_action(list: &Vec, return Err(format!("cons takes two arguments")); } match &args[1] { + &SchemeExpr::SchemeNull => { + Ok(SchemeExpr::SchemeList(vec![args[0].clone()])) + }, &SchemeExpr::SchemeList(ref list) => { let mut ret = vec![args[0].clone()]; ret.extend_from_slice(list); Ok(SchemeExpr::SchemeList(ret)) }, - _ => Err(format!("cdr takes only lists")) + _ => Err(format!("cons second arg must be list or null")) } }, "display" => { -- cgit v1.2.3