2

I'm trying to loop through a mapping list (type_a) and update a different mapping list (type_b). Both lists are different. For List.fold, does the accumulator have to be the same type as list? ie, if we have

let fn = fun (acc, a: type_b * type_a list) : type_b = ...

let my_list : type_a list = Map.literal[...]
let acc : type_b = ...
let sum_of_elements : int = List.fold_left fn acc my_list

Does acc have to be of type_a? I'm getting a few errors about the fields not matching, but I'm not sure if that is because this is not allowed, or something else..

1 Answer 1

4

The type signature for List.fold_left is

val fold_left : ('acc * 'item -> 'acc) -> 'acc -> 'item list -> 'acc

That means 'acc can be of any type, e.g.

let fn = fun (acc, a: (int, int) map * int) : (int, int) map -> acc
let my_map : (int,int) map = Map.empty
let folded_map : (int,int) map = List.fold_left fn my_map ([] : int list)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.