revise the code to use Scala list