This patch move the implementation of "first(x, n)" from
STAGG to URAGG; also add a specialized version for List.

-- 
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 fricas-devel+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/fricas-devel/CAGBJN91HYc0MXxQofoOx9xyqMx-hNTC4b-n2G0hySzRZr1e1qA%40mail.gmail.com.
diff --git a/src/algebra/aggcat.spad b/src/algebra/aggcat.spad
index 2a057cb8..01ffaaba 100644
--- a/src/algebra/aggcat.spad
+++ b/src/algebra/aggcat.spad
@@ -1420,6 +1420,18 @@ UnaryRecursiveAggregate(S : Type) : Category == RecursiveAggregate S with
   cyclic? x       == not empty? x and not empty? findCycle x
   last x          == first tail x
 
+  first(x, n) ==
+      l : List S := []
+      for i in 1..n repeat
+          empty? x => error "Index out of range"
+          l := concat(first x, l)
+          x := rest x
+      res : % := empty()
+      while not empty? l repeat
+          res := concat(first l, res)
+          l := rest l
+      res
+
   nodes x ==
       l := empty()$List(%)
       while not empty? x repeat
@@ -1616,15 +1628,8 @@ StreamAggregate(S : Type) : Category ==
 
    import from Integer
 
-   c2 : (%, %) -> S
-
    explicitlyFinite? x == not cyclic? x
    possiblyInfinite? x == cyclic? x
-   first(x, n)         == construct [c2(x, x := rest x) for i in 1..n]
-
-   c2(x, r) ==
-       empty? x => error "Index out of range"
-       first x
 
    elt(x : %, i : Integer) ==
        i := i - minIndex x
diff --git a/src/algebra/list.spad b/src/algebra/list.spad
index 7f382a03..e3d19e51 100644
--- a/src/algebra/list.spad
+++ b/src/algebra/list.spad
@@ -100,6 +100,14 @@ List(S : Type) : Exports == Implementation where
       minIndex x          == LISTMININDEX
       maxIndex x          == # x
 
+      first(x, n) ==
+          l : % := empty()
+          for i in 1..n repeat
+              empty? x => error "Index out of range"
+              l := concat(first x, l)
+              x := rest x
+          reverse! l
+
       rest(x, n) ==
          for i in 1..n repeat
             if empty? x then error "index out of range"

Reply via email to