refactored length
last and rest were actually an accumulator. Unboxing e in pattern match