fix: 1-based indexing, babey

This commit is contained in:
Roman Godmaire 2024-05-14 14:48:52 -04:00
parent e25c9baa66
commit a2210f2db3
2 changed files with 6 additions and 2 deletions

View file

@ -334,7 +334,11 @@ pub(super) fn core() -> HashMap<String, Value> {
}
};
list.get(nth).unwrap().clone()
if nth < 1 {
return Node::Error(format!("TypeError: expected positive int, got {}", nth));
}
list.get(nth - 1).unwrap().clone()
}),
),
(

View file

@ -474,7 +474,7 @@ mod test {
// List Manipulation
#[case("(first (list 1 2 3))", "1")]
#[case("(last (list 1 2 3))", "3")]
#[case("(nth (list 1 2 3) 1)", "2")]
#[case("(nth (list 1 2 3) 2)", "2")]
#[case("(push-front (list 1 2 3) 0)", "(0 1 2 3)")]
#[case("(push-back (list 1 2 3) 4)", "(1 2 3 4)")]
#[case("(pop-front (list 1 2 3))", "(2 3)")]