Replace algorithm implemented in Scheme.