#Code Review
1 messages · Page 1 of 1 (latest)
I'd also look into list.fold, since it looks like it should be a good fit what you're doing
here's a very slight readability rewrite:
import gleam/int
pub fn find_length_of_lcis(nums: List(Int)) -> Int {
case nums {
[] -> 0
[_] -> 1
_ -> find_length_helper(nums, 1, 1)
}
}
fn find_length_helper(
nums: List(Int),
max_length: Int,
current_length: Int,
) -> Int {
case nums {
[] | [_] -> max_length
[first, second, ..rest] if second > first -> {
let new_current = current_length + 1
let new_max = int.max(max_length, new_current)
find_length_helper([second, ..rest], new_max, new_current)
}
[_, second, ..rest] -> find_length_helper([second, ..rest], max_length, 1)
}
}
untested, of course
but lgtm
i flattened the case expression and took out a few {} where they weren't necessary
i recommend looking at actual changes like using list.fold too - i just was curious what i could do in terms of how pretty it was
As far as I see list.fold operates on each element of a list
But we need to loop through elements pairwise. Aka sliding window of 2
You can either keep the previous element in the accumulator of the fold, or stick a list.window_by_2 call before the fold
i think the recursive solution is much clearer than attempting to craft one from the list.* functions
Recursion nice