I’m trying to achieve this:
7 2 3 5
10 12 20
res = 10 + max(7,2) ; 12 + max(2,3); 20 + max(3,5)
this is my code so far:
//prevline.count is always currLine.count+1
let getResLine currLine prevLine =
let rec loop resLine prevLine' = function
|[] -> resLine
|hd::tl -> loop (hd + (max (List.nth prevLine' 0) (List.nth prevLine' 1)))::resLine (List.tail prevLine') tl
loop [] prevLine currLine
but it doesn’t compile, it tells me some type mismatch errors, probably somebody more experienced could see some obvious errors that I made, please help
Edit: updated my code accoding to the suggestions, but it’s still not working
First, if you want to split a list into a head and a tail, use just
hd::tl(without brackets).Second, you don’t seem to be actually creating the result list. And I think you don’t need an accumulator here.
Third, I think it’s confusing to use the same variable names in both functions (because you can use the variables from the outer function in the inner function).
Because of all that, I would rewrite your function like this:
Although I think it would be more readable using higher-order functions, not explicit recursion:
I used
Seq.map2, insted ofList.map2, because the latter doesn’t handle lists of unequal length.