Following a suggestion from RJF to use the builtin function 'map', I
rewrote the program 'sublist.mac' which is attached. It has both
Jaime's routine, called 'j_sublist' and two other routines, called 'opp_1' and 'same_1'.
The routines opp_1, and same_1 work very fast.
But, they won't compile. I tried putting the function names 'fex' and
'proj_1' into the local variables of the function 'opp_1'
The warning disappeared, but it still would not compile.
(%i1) load("sublist");
Evaluation took 0.00 seconds (00.10 elapsed)
(%o1) ./sublist.mac
(%i2) xx: xrl(10000)$
Evaluation took 0.60 seconds (0.60 elapsed)
(%i3) same_1(xx)$
Evaluation took 0.52 seconds (0.53 elapsed)
(%i4) compile(same_1);
Warning-> fex is an undefined global variable.
Warning-> proj_1 is an undefined global variable.
Compiling gazonk0.lsp.
End of Pass 1.
End of Pass 2.
OPTIMIZE levels: Safety=2, Space=3, Speed=2
Finished compiling gazonk0.lsp.
Evaluation took 0.00 seconds (0.20 elapsed)
(%o4) [same_1]
(%i5) same_1(xx)$
Maxima encountered a Lisp error:
symbol
Automatically continuing.
To reenable the Lisp debugger set *debugger-hook* to nil.
Any ideas?
TIA,
-sen
---------------------------------------------------------------------------
| Sheldon E. Newhouse | e-mail: sen1 at math.msu.edu |
| Mathematics Department | |
| Michigan State University | telephone: 517-355-9684 |
| E. Lansing, MI 48824-1027 USA | FAX: 517-432-1562 |
---------------------------------------------------------------------------
On Tue, 17 Apr 2007, Stavros Macrakis wrote:
> On 4/17/07, Richard Fateman <fateman at cs.berkeley.edu> wrote:
>>
>> You can do some extra checking, but I think something like this will work:
>>
>> Extract(input):=block([result:[]],
>> map(lambda([x,y], if x*y>0 then result:cons(x,result)), input,
>> rest(input)),
>> reverse(result));
>>
>
> This will always given an error, since "map" requires all its list arguments
> to be the same length.
> Perhaps we should have a Lisp-like version of map which stops when the first
> list runs out.
>
> Also, map will construct an unnecessary list (which is then thrown away),
> but I suppose that is a reasonable trade-off for easier programming.
>
> -s
>
-------------- next part --------------
showtime: all;
xrl(i):= makelist(random(10.0)-5,j,1,i);
j_sublist(x):= block([l: [], prev: x[1]], for i:2 thru length(x) do
(if prev*x[i] > 0 then l: cons(prev,l), prev:x[i]), reverse(l));
/********************** old routines
li_2(list):= makelist([list[i],list[i+1]],i,1, length(list) -1)$
pos_2(list):=
sublist(list, lambda([x],x[1]*x[2] > 0))$
*/
/* project the list to its first coordinate */
proj_1(list):= list[1]$
fex(x,y):=[x,y]$
/* to get the sublist of points whose immediate successor has the
opposite sign, do opp_1(list) */
opp_1(list):= block([zz,yy,uu,vv],
zz: rest(list),
yy: reverse(rest(reverse(list))),
uu: map(fex,yy,zz),
vv: sublist(uu, lambda([x], x[1]*x[2] < 0)),
map(proj_1,vv)
)$
/* to get the sublist of points whose immediate successor has the
same sign, do same_1(list) */
same_1(list):= block([zz,yy,uu,vv],
zz: rest(list),
yy: reverse(rest(reverse(list))),
uu: map(fex,yy,zz),
vv: sublist(uu, lambda([x], x[1]*x[2] > 0)),
map(proj_1,vv)
)$