question about sublist



Sheldon,

You ciould I suppose do this with sublist, but that gets messy and ugly,
since the predicate will have to have side-effects.

Why not use a simple loop?

By the way, your specification is ambiguous, since the last element of the
list has no "immediate successor"; also, how do you treat zero elements?
And do you intend that the last element of each run not be included (as
specified)?

signrun(l):=
  if l=[] then [] else
   block([acc:[],nextl:rest(l)],
              while nextl#[] do
                  ( if sign(first(l))=sign(first(nextl)) then
acc:cons(first(l),acc),
                    l: nextl, nextl:rest(l) ),
              reverse(acc) )$

signrun([2,2,2]) => [2,2]   (note that the last "2" doesn't have a
successor)
signrun([2,2,-1,-1]) => [2,-1]
signrun([2,2,-1,2,2,-1,-1]) => [2, 2, - 1]       (runs are not separated)

You could also define an analogue of sublist that worked a bit differently
(like Lisp maplist rather than mapcar), etc. , but I'm afraid I don't have
time to go into that now.

           -s