Ergonomic pattern matching in recursive solutions
I've stumbled upon 99OCamlProblems which is a very good collection of problems with varying difficulty and someone thankfully ported the problems with tests in a repo you can check out here any way since this collection of problems started with lisp and then OCaml so to keep the spirit of functional languages I thought it'd be fun to try to them in rust fully functional style with no for/while loops unless absolutely necessary. I am saying all of this just to say
TIL this is valid rust code:
fn reverse<T: Clone + Copy>(list: &[T]) -> Vec<T> {
match list {
[] => vec![],
[first, last @ ..] => vec![reverse(last), vec![*first]].concat(),
}
}
fn is_palindrome<T: PartialEq>(list: &[T]) -> bool {
match list {
[] | [_] => true,
[first, mid @ .., last] => is_palindrome(mid) && first == last,
}
}
I do realize maybe these maybe aren't the most optimal solutions for the problem, but I like the fact that a low level systems programming language can be expressive with pattern matching like this! absolutely love it!