(** * Programming Pearls Column 2 Exercise B * * In-place routines to reverse and rotate arrays. * * Author: Michael DiBernardo *) (** Reverse an array in-place from start_index to end_index inclusive. *) let rec reverse_array arr start_index end_index = if start_index >= end_index then ignore () else let first_elem = arr.(start_index) and last_elem = arr.(end_index) in ( arr.(end_index) <- first_elem; arr.(start_index) <- last_elem; (reverse_array arr (start_index + 1) (end_index - 1)) ) (** Rotate an array in-place by 'amount' positions to the left. *) let rotate_left arr amount = let i = (amount mod (Array.length arr)) and last_index = ((Array.length arr) - 1) in ( reverse_array arr 0 (i - 1); reverse_array arr i last_index; reverse_array arr 0 last_index; )