Implement scanl() and scanr()