optimize 'last' in URAGG

diff --git a/src/algebra/aggcat.spad b/src/algebra/aggcat.spad
index 822a0178..de4666ff 100644
--- a/src/algebra/aggcat.spad
+++ b/src/algebra/aggcat.spad
@@ -1508,8 +1508,15 @@
           k

       last(x, n) ==
-          n > (m := #x) => error "index out of range"
-          copy rest(x, (m - n)::NonNegativeInteger)
+          xn := x
+          for i in 1..n repeat
+              empty? xn => error "index out of range"
+              xn := rest xn
+          -- xn is n nodes ahead of x
+          while not empty? xn repeat
+              xn := rest xn
+              x := rest x
+          copy x

       find(f, x) ==
           while not empty? x and not f first x repeat x := rest x

-- 
You received this message because you are subscribed to the Google Groups 
"FriCAS - computer algebra system" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/fricas-devel.
For more options, visit https://groups.google.com/d/optout.

Reply via email to