From 17463c839f7e5dc34fdce79181bcb8ceb73e15aa Mon Sep 17 00:00:00 2001 From: mehbark Date: Thu, 15 Jun 2023 00:07:27 -0400 Subject: [PATCH] fix up matches there was a bug i ran into when matching on the empty list --- src/main.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index f7b896c..e467b2f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -502,6 +502,7 @@ type Matches<'src> = HashMap; // DONE?: there. can. be. at most. one. match. // i'm happy that this is faster, but it might be worth going back to the per-variable thing +#[must_use] pub fn matches<'src>(vars: &[Symbol], lhs: &Sexp, expr: &'src Sexp) -> Option> { match (lhs, expr) { (Sexp::Atom(a), Sexp::Atom(b)) => { @@ -526,13 +527,14 @@ pub fn matches<'src>(vars: &[Symbol], lhs: &Sexp, expr: &'src Sexp) -> Option None, (Sexp::List(xs), Sexp::List(ys)) => { - if xs.len() == ys.len() { + if xs == ys { + Some(HashMap::with_capacity(0)) + } else if xs.len() == ys.len() { xs.iter() .zip(ys) .map(|(lhs, expr)| matches(vars, lhs, expr)) .reduce(|a, b| match (a, b) { - (None, _) => None, - (_, None) => None, + (None, _) | (_, None) => None, (Some(a), Some(b)) => merge_matches(vars, a, b), }) .unwrap_or(None)