Re[2]: Return from nested subroutines (still goto discussion)

new topic     » topic index » view thread      » older message » newer message

D> for i = 1 to 5 do
D>     *code*
D> end for
D> puts(1, "success\n")
D> puts(1, "program end\n")

D> procedure r(integer x)
D>     if x = 5 then
D>         puts(1, "success")
D>     else
D>         r(x+1)
D>     end if
D> end procedure

D> procedure main()
D>     r(1)
D>     puts(1, "program end")
D> end procedure
 

Thank you, but...

Maybe my code was too much simplified. What I meant is algorithms such
as backtracking or permutation. Now modified example:

object d
d = {0,0,0,0,0}

procedure r(integer x, integer y)
    d[x] = y
    if x = 5 then
        if d[1]+d[2]+d[3]+d[4]+d[5] = 10 then
            puts(1, "success")
            -- (print d here)
            goto ok
        end if
    else
        for i = 0 to 3 do
            r(x+1, i)
        end for
    end if
end procedure

procedure main()
    for i = 0 to 3 do
        r(1, i)
    end for
ok:
    puts(1, "program end")
end procedure
 

Now the purpose of the program is to search d so that sum(d) = 10 and
every element of d is from 0 to 3. But when a solution is found, it
will just print the first solution found and continue to printing
"program end".

Now how can it modified to use no GOTO and no decrease on performance?
(using  flags  to  indicate  whether  a  solution has been found would
decrease performance, I think)

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu