use unfoldr instead of explicit recursion