[Date Prev] [Date Next] [Thread Prev] [Thread Next] Date Index Thread Index Search archive:
Date:Mon Aug 20 11:50:58 1993 
Subject:delete_nth - the saga continues 
From:jonr (Jonathan Rowe) 
Volume-ID:930820.01 


Here's a functional approach. First we define a function ZIP which applies a
binary procedure over the elements of two lists (until one runs out), putting
the results in a list:

    define zip( L1, L2, p );
        lvars L1 L2 p i j;

        [%
        for i, j in L1, L2 do
            p( i, j )
        endfor
        %]

    enddefine;

Now we have a functional "if":

    define do_if( test, p );
        lvars test p;
        if test then
            p()
        endif
    enddefine;


Now we create a list containing all the positive numbers:

    vars numbers =
        pdtolist(
            procedure( x ) -> y;
                lvars x y;
                x.cont -> y;
                y + 1 -> x.cont;
            endprocedure(% consref( 1 ) %)
        );

And now all we have to do is zip up the numbers simultaneously with our list,
erasing when we get to n:

    define delete_nth( n, L );
        lvars n L;

        zip(L, numbers, (nonop =(% n %)) <> do_if(% erase %) );

    enddefine;


Jon Rowe