An optimal, purely functional implementation of the Garsia–Wachs algorithm — scholarly article