[Date Prev] [Date Next] [Thread Prev] [Thread Next] Date Index Thread Index Search archive:
Date:Sun, 29 Feb 2004 16:04:45 +0000 (UTC) 
Subject:Re: sort() 
From:A . Sloman 
Volume-ID: 

Ian,

> Hopefully a slightly simpler problem this time. I have decided to add cost
> to my parse, and so now i have a list of lists
>
> [[DET N V PREP 25] [DET V N PREP 72] [DET N N PREP 35]] .
>
> I would like to use sort() if it was possible, but obviously using it on
> this list i get an 'alphabefore' error. Is there a way of sorting the
> elements in the list in relation to the number at the end of each of the
> elements (which represents the total cost of the particular parse).

The autoloadable procedure 'sort' (ENTER SHOWLIB sort) is defined in
terms of the more general procedure syssort, thus:

define global sort(list);
    lvars list;
    if null(list) then
        []
    elseif isnumber(fast_front(list)) then
        syssort(list, nonop <=)
    else
        syssort(list, alphabefore)
    endif
enddefine;


It checks if the first item is a number and if so assumes that the list
contains numbers and does

        syssort( list, nonop <= )

otherwise it assumes the list contains words, and does

        syssort( list, alphabefore )

which produces an error if the list contains lists.

Notice that
        syssort( list, nonop >= )

would sort a list of numbers in decreasing order.

And:
        syssort( list, alphabefore <> not )

would sort a list of words in reverse alphabetic order

        syssort( [a z b w e q r m], alphabefore <> not ) =>
        ** [z w r q m e b a]

So you need to define a procedure, perhaps called 'ordercost' that can
go as the second argument of syssort, and then you just do

    syssort( list, ordercost )

to produce the list ordered according to cost.

Define ordercost any way you like, except that it must take two
items of the kinds that can be found in list, and return true or false
for any two of them. In your case you could use one of the number
comparison operators on the 5th element of each list. You can choose
whether to produce the list in increasing or decreasing order.

It's a good idea to make your ordercost procedure check that it has the
right kinds of inputs, as alphabefore does, even though that will slow
it down a bit. It could save a lot of hassle debugging. See HELP MISHAP

Aaron
==
http://www.cs.bham.ac.uk/~axs/