The   Look and say sequence   is a recursively defined sequence of numbers studied most notably by   John Conway.



The   look-and-say sequence   is also known as the   Morris Number Sequence,   after cryptographer Robert Morris,   and the puzzle   What is the next number in the sequence 1,   11,   21,   1211,   111221?   is sometimes referred to as the Cuckoo's Egg,   from a description of Morris in Clifford Stoll's book   The Cuckoo's Egg.


Sequence Definition

 Take a decimal number
 Look at the number, visually grouping consecutive runs of the same digit.
 Say the number, from left to right, group by group; as how many of that digit there are - followed by the digit grouped.
 This becomes the next number of the sequence.

An example:

 Starting with the number 1,   you have one 1 which produces 11
 Starting with 11,   you have two 1's.   I.E.:   21
 Starting with 21,   you have one 2, then one 1.   I.E.:   (12)(11) which becomes 1211
 Starting with 1211,   you have one 1, one 2, then two 1's.   I.E.:   (11)(12)(21) which becomes 111221


Task

Write a program to generate successive members of the look-and-say sequence.



Related tasks

   Fours is the number of letters in the ...
   Number names
   Self-describing numbers
   Self-referential sequence
   Spelling of ordinal numbers


See also

   Look-and-Say Numbers (feat John Conway), A Numberphile Video.
   This task is related to, and an application of, the Run-length encoding task.
   Sequence A005150 on The On-Line Encyclopedia of Integer Sequences.


on lookAndSay(startNumber, howMany)
    if (howMany < 1) then return {}
 
    -- The numbers are handled as lists of digit-value integers for efficiency and output as a list of strings.
    script o
        property previousNumber : {}
        property newNumber : {}
        property output : {}
    end script
 
    -- "Digitise" the start number.
    repeat
        set beginning of o's newNumber to startNumber mod 10 as integer
        set startNumber to startNumber div 10
        if (startNumber is 0) then exit repeat
    end repeat
    -- Add it to the output as text and successively derive the remaining numbers.
    set astid to AppleScript's text item delimiters
    set AppleScript's text item delimiters to ""
    set end of o's output to o's newNumber as text
    repeat (howMany - 1) times
        set o's previousNumber to o's newNumber
        set o's newNumber to {}
        set i to 1
        set previousLength to (o's previousNumber's length)
        set currentDigit to beginning of o's previousNumber
        repeat with j from 2 to previousLength
            set thisDigit to item j of o's previousNumber
            if (thisDigit is not currentDigit) then
                set end of o's newNumber to j - i
                set end of o's newNumber to currentDigit
                set i to j
                set currentDigit to thisDigit
            end if
        end repeat
        set end of o's newNumber to previousLength - i + 1
        set end of o's newNumber to currentDigit
 
        set end of o's output to o's newNumber as text
    end repeat
    set AppleScript's text item delimiters to astid
 
    return o's output
end lookAndSay
 
-- Test code:
return lookAndSay(1, 10)