Implementation of REVERSE built-in function