Memoizing head to avoid re-calculation

here `case Cons(h,t) if f(h()) => Some((h(), t()))` `h()` is called twice re-calculating it.
This PR fixes it via memoization