1. Quiz! -Reply

After running the code, I think that the following recursive reverse is
more intuitively correct than the version you posted.  Plus you needed an
empty case to bottom out the recursion blink

function r_reverse(sequence in)
    sequence out

-- added this line to bottom the recursion
    out = {}
--

    for i = 1 to length(in) do
        if atom(in[i]) then
            out = in[i] & out
        else

           -- added braces to this line to 'sequencize' the recursed
           -- sequences
           -- out = r_reverse(in[i]) & out
           out = {r_reverse(in[i])} & out

        end if
    end for
    return out
end function

now r_reverse({"hello","world"}) will return
{"dlrow","olleh"}
rather than
{d,l,r,o,w,o,l,l,e,h}

-Jay Turley
 "bored at work"

new topic     » topic index » view message » categorize

2. Re: Quiz! -Reply

On Tue, 20 Oct 1998, Jay Turley wrote:
>            -- added braces to this line to 'sequencize' the recursed
>            -- sequences
>            -- out = r_reverse(in[i]) & out
>            out = {r_reverse(in[i])} & out
>
>         end if
>     end for
>     return out
> end function
>
> now r_reverse({"hello","world"}) will return
> {"dlrow","olleh"}
> rather than
> {d,l,r,o,w,o,l,l,e,h}

Oops! Caught coding on the fly again! Jay's code is how I intended mine to
be...

Looks like I didn't even pass my own test! :-S :)

Incidentally, it's probably better to use:

    out = prepend(r_reverse(in[i]), out)

rather than

    out = {r_reverse(in[i])} & out

. Semantically equivalent (i.e. they do the same thing), but syntactically
tidier. IMHO.

Feeling very silly,
Carl

/me hits self over head with fish. *Splat*.

--
Carl R White -- Final Year Computer Science at the University of Bradford
E-mail...: cyrek- at -bigfoot.com -- Remove the hyphens before mailing. Ta :)
URL......: http://www.bigfoot.com/~cyrek/
Ykk rnyllaqur rgiokc cea nyemdok ymc giququezka caysgr -- B.Q.Vgesa

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu