SML code for homework