Cons.st
changeset 1237 3814a1f983a5
parent 1074 cb147ae2e03c
child 1243 15058ebc321b
--- a/Cons.st	Thu Jun 05 20:38:07 2003 +0200
+++ b/Cons.st	Thu Jun 05 20:39:20 2003 +0200
@@ -116,6 +116,12 @@
     "
 !
 
+head
+    "return the head, first or car - whatever you wonna call it"
+
+    ^ self car
+!
+
 last
     "for lispers:
      return the last element of a list"
@@ -126,7 +132,7 @@
     [(rest := p cdr) notNil] whileTrue:[
         p := rest
     ].
-    ^ p
+    ^ p car
 
     "
      (Cons fromArray:#(1))       last     
@@ -144,7 +150,7 @@
     p := self.
     [true] whileTrue:[
         cnt := cnt - 1.
-        cnt == 0 ifTrue:[^ p].
+        cnt == 0 ifTrue:[^ p car].
         p := p cdr.
         p isNil ifTrue:[
             self error:'no such element' mayProceed:true.
@@ -159,6 +165,12 @@
      (Cons fromArray:#(1 2 3 4)) nth:4  
      (Cons fromArray:#(1 2 3 4)) nth:5  
     "
+!
+
+tail
+    "return the tail, second or cdr - whatever you wonna call it"
+
+    ^ self cdr
 ! !
 
 !Cons methodsFor:'accessing - basic'!
@@ -224,6 +236,22 @@
      b car:'five'.
      ab    
     "
+!
+
+take:n
+    "for lispers:
+     take n elements from the list; return a new list"
+
+    n > 0 ifTrue:[
+        ^ Cons car:(self car) cdr:(self cdr take:n-1)
+    ].
+    ^ nil
+
+    "
+     (Cons fromArray:#(1 2 3 4)) take:3  
+     (Cons fromArray:#(1)) take:0  
+     (Cons fromArray:#()) take:3  
+    "
 ! !
 
 !Cons methodsFor:'printing'!
@@ -281,5 +309,5 @@
 !Cons class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/Cons.st,v 1.3 2002-07-31 19:34:07 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/Cons.st,v 1.4 2003-06-05 18:39:20 cg Exp $'
 ! !