I try to go through a list xl by List.fold_left, I would like to quit the iteration when some condition is satisfied:
List.fold_left
(fun x acc ->
if x = 5 then STOP THE ITERATION
else x + acc)
xl
Could anyone tell me how to express STOP THE ITERATION here? Thank you
Edit1: By the code above, I would like to say we do not stop accumulation until we meet the first 5.
You cannot do this with the built in folds without an exception or some no-op being flagged in the accumulator for the remaining calls –as phimuemue mentions. Alternatively, you can just write a very simple tail-recursive function to take care of returning early,