implemented the length function with pattern matching