title: Explanation for group by 3 elements from a list function using FOLD in OCAML
body: <p>I have a piece of code that does the following: group 3 elements of a list of n elements. The main function is called <code>group_by_3</code>. For example, executing group_by_3 <code>[1;2;3;4;5;6;7]</code> will give me <code>([1;2;3],[4;5;6],[7])</code>.</p>
  
  <pre><code>let group_by_3 lst =
    let accum = ( [], [], 0 )
    in
    let f (all_groups, current_group, size) x =
      if size = 3
      then ( (List.rev current_group) :: all_groups, 
             [x], 1 )
      else ( all_groups, x::current_group, size+1)
    in
    let (groups, last, _) = List.fold_left f accum lst
    in List.rev ( List.rev last :: groups)
  </code></pre>
  
  <p>I don't really understand why this works (it is provided in class).</p>
  
  <ul>
  <li>What are all_groups, current_group, size?</li>
  <li><p>What does this do?</p>
  
  <pre><code>if size = 3
  then ( (List.rev current_group) :: all_groups, 
         [x], 1 )
  else ( all_groups, x::current_group, size+1)
  </code></pre></li>
  </ul>
  
  <p>Thank you!</p>