solved P4
solved P5
solved P6
refactor: added tailrec where applicable. Unified usage of xs as variable name. Applied usage of last() where applicable to reduce code duplication