Cons.st
changeset 1243 15058ebc321b
parent 1237 3814a1f983a5
child 1248 2d0493cd8d3c
--- a/Cons.st	Tue Jun 10 13:06:51 2003 +0200
+++ b/Cons.st	Tue Jun 10 15:23:44 2003 +0200
@@ -167,6 +167,48 @@
     "
 !
 
+reversed
+    "for lispers:
+     return a new list with the cars in reverse order"
+
+"/ for now, tail recursion is not yet optimized by the st/x jitter...
+"/
+"/    |rev|
+"/
+"/    rev := [:lst :acc |
+"/                lst isNil ifTrue:[
+"/                    acc
+"/                ] ifFalse:[
+"/                    rev value:(lst tail)
+"/                        value:(Cons car:(lst head) cdr:acc)
+"/                ]
+"/           ].
+"/    ^ rev value:self value:nil
+
+    | lst acc|
+
+    lst := self.
+    acc := nil.
+
+    [
+        |nLst nAcc|
+
+        lst isNil ifTrue:[ ^ acc].
+
+        nLst := lst tail.
+        nAcc := Cons car:(lst head) cdr:acc.
+        lst := nLst.
+        acc := nAcc.
+   ] loop
+
+    "
+     (Cons fromArray:#(1))       reversed     
+     (Cons fromArray:#(1 2))     reversed     
+     (Cons fromArray:#(1 2 3 4)) reversed    
+     (Cons fromArray:(1 to:10000)) reversed    
+    "
+!
+
 tail
     "return the tail, second or cdr - whatever you wonna call it"
 
@@ -309,5 +351,5 @@
 !Cons class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/Cons.st,v 1.4 2003-06-05 18:39:20 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/Cons.st,v 1.5 2003-06-10 13:23:44 cg Exp $'
 ! !