Solved problem 8 in SML