diff --git a/mute-interpreter/src/env/core.rs b/mute-interpreter/src/env/core.rs index 6c75124..40333d8 100644 --- a/mute-interpreter/src/env/core.rs +++ b/mute-interpreter/src/env/core.rs @@ -334,7 +334,11 @@ pub(super) fn core() -> HashMap { } }; - list.get(nth).unwrap().clone() + if nth < 1 { + return Node::Error(format!("TypeError: expected positive int, got {}", nth)); + } + + list.get(nth - 1).unwrap().clone() }), ), ( diff --git a/mute-interpreter/src/evaluator.rs b/mute-interpreter/src/evaluator.rs index 5ebb6f5..3a79597 100644 --- a/mute-interpreter/src/evaluator.rs +++ b/mute-interpreter/src/evaluator.rs @@ -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)")]