use foldl for cumulative search