To begin with, given a string representing a number we need to be able to generate the next number. generateNext does that by consuming the string one character at a time: if the last character seen is the same as the previous one, then the last seen character remains unchanged, but its count goes up by one. Every time the current character no longer matches the previous one, the length of the current sequence curlen is written out together with the character that contains the digit we've seen that many times, and the character to match is now the new character with a count of 1.

Now we can write the lookAndSay function. It just takes the seed to start the sequence, and the requested length of the sequence. lookAndSay would call generateNext and use the result to seed the recursive call to itself, with length decremented by one.

Note that Scala allows us to nest function definitions; generateNext is local to the scope of lookAndSay since we're not going to use it anywhere else.