Factorial using higher order functions