reimplemented takeWhile using foldRight