I am working on a OCaml motion detection program. It analyzes two images and detects if there was motion. One part requires me summing a row of values and then also summing an entire image. This is what I have currenly:
let rec sumImRow(maskedrow) =
match maskedrow with
|  -> 0
| mskRhd::mskRtl -> mskRhd + (sumImRow mskRtl)
let rec sumImage(maskedimage) =
match maskedimage with
| mskRhd::mskRtl -> (sumImRow mskRhd)::(sumImage mskRtl)
| _ -> 
and the given value is
int list list -> int list = <fun>. I don't quite understand why this is giving me
Best How To :
tl;dr: You construct a new list instead of summing the integers.
Well, we can agree that
sumImRow has type
int list -> int as it takes elements from the list and then return their sum.
sumImage will have a
list argument as it deconstructs it in the pattern matching. It then returns the
list of result of
sumImage gets as argument a list of what
sumImRow takes and returns a list of results. So we indeed have
int list list -> int list.
You can avoid that by replacing
sumImage's matching result.
You can also make a more improved code by using
let sumImRow l = List.fold_left (+) 0 l;;
let sumImage l = List.fold_left (List.fold_left (+)) 0 l;;