Look and say sequence (Eiffel)

From LiteratePrograms

Jump to: navigation, search
Other implementations: C++ | dc | Eiffel | Haskell | J | Java | Lua | OCaml | Perl | PHP | Python | Ruby | Scala | sed | sh

This program is a code dump.
Code dumps are articles with little or no documentation or rearrangement of code. Please help to turn it into a literate program. Also make sure that the source of this code does consent to release it under the MIT or public domain license.

This is a simple Eiffel program to generate look-and-say sequences such as the Conway sequence.

<<look_and_say_sequence.e>>=
class LOOK_AND_SAY_SEQUENCE
create
    make
feature {NONE} -- Initialization
    make (gen: INTEGER) is
        do
            initial_item := gen.out
            start
        end
feature -- Access
    item : STRING
feature -- Traversal
    start is
        do
            item := initial_item
        end
    next is
        local
            i, count: INTEGER
            test_char: CHARACTER
            next_item: STRING
        do
            from
                i := 1
                count := 0
                test_char := item.item (i)
                next_item := ""
            until
                i > item.count
            loop
                if test_char /= item.item (i) then
                    next_item := next_item + count.out + test_char.out
                    count := 0
                    test_char := item.item (i)
                end
                i := i + 1
                count := count + 1
            end
            next_item := next_item + count.out + test_char.out
            item := next_item
        end
feature {NONE} -- Implementation
    initial_item: STRING
end
<<look_and_say_test.e>>=
class LOOK_AND_SAY_TEST
create
    make
feature {NONE} -- Initialization
    make is
        local
            i: INTEGER
            conway_sequence: LOOK_AND_SAY_SEQUENCE
        do
            create conway_sequence.make (3)
            from
                i := 1
            until
                i >= 10
            loop
                io.put_string (conway_sequence.item)
                io.put_new_line
                conway_sequence.next
                i := i + 1
            end
        end
end
Download code
Views