eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'Cons':

Home

Documentation
www.exept.de
Everywhere
for:
[back]

Class: Cons


Inheritance:

   Object
   |
   +--Collection
      |
      +--SequenceableCollection
         |
         +--Cons
            |
            +--LazyCons

Package:
stx:libbasic2
Category:
Collections-Linked
Version:
rev: 1.31 date: 2018/05/15 16:15:43
user: cg
file: Cons.st directory: libbasic2
module: stx stc-classLibrary: libbasic2
Author:
Claus Gittinger (Jun 2002)

Description:


A pair as in lisp.
    Cons car:a cdr:b

Conses are not heavily used by Smalltalk (actually: not at all).
Consider this a demo class.


Related information:



Class protocol:

instance creation
o  car: carArg cdr: cdrArg

o  fromArray: anArray
Cons fromArray:#(1 2 3 4)
Cons fromArray:#()
Cons fromArray:#(1)
Cons fromArray:(1 to:10000)

o  makeList: size
(self makeList:0) size
(self makeList:1) size
(self makeList:100) size
(self makeList:1000) size
(self makeList:10000) size

sExpressions
o  readLispAtomFrom: aStream
smalltalk allows +n; scheme does not

o  readLispFrom: aStream
EOL comment
usage example(s):
     self readLispFrom:('(cons 1 2)' readStream).

o  readLispListFrom: aStream
the leading '('


Instance protocol:

accessing
o  at: n
for collection compatibility:
a slow indexed accessor
usage example(s):
     (Cons fromArray:#(1))       at:1     
     (Cons fromArray:#(1 2 3 4)) at:1 
     (Cons fromArray:#(1 2 3 4)) at:3  
     (Cons fromArray:#(1 2 3 4)) at:4  
     (Cons fromArray:#(1 2 3 4)) at:5  

o  at: n put: newValue
destructive:
for collection compatibility: a slow indexed accessor

o  first
return the head, first or car - whatever you wonna call it

o  head
return the head, first or car - whatever you wonna call it

o  last
for lispers:
return the last element of a list
usage example(s):
     (Cons fromArray:#(1))       last     
     (Cons fromArray:#(1 2 3 4)) last    

o  nth: n
for lispers:
return the nth element of a list
usage example(s):
     (Cons fromArray:#(1))       nth:1     
     (Cons fromArray:#(1 2 3 4)) nth:1 
     (Cons fromArray:#(1 2 3 4)) nth:3  
     (Cons fromArray:#(1 2 3 4)) nth:4  
     (Cons fromArray:#(1 2 3 4)) nth:5  
     (Cons fromArray:#( ))       nth:1  -> error    

o  rest
return the tail, rest or cdr - whatever you wonna call it

o  reversed
for lispers:
return a new list with the cars in reverse order
usage example(s):
     (Cons fromArray:#(1))       reversed     
     (Cons fromArray:#(1 2))     reversed     
     (Cons fromArray:#(1 2 3 4)) reversed    
     (Cons fromArray:(1 to:10000)) reversed    

o  tail
return the tail, rest or cdr - whatever you wonna call it

accessing-basic
o  cadddr
return the fourth element

o  caddr
return the third element

o  cadr
return the second element

o  car
return the head, first or car - whatever you wonna call it

o  car: something
set the head, first or car - whatever you wonna call it

o  car: carArg cdr: cdrArg
set both car and cdr

o  cddr
return the rest after the second element

o  cdr
return the tail, second or cdr - whatever you wonna call it

o  cdr: something
set the tail, second or cdr - whatever you wonna call it

o  first: carArg rest: cdrArg
set both car and cdr

o  head: carArg tail: cdrArg
set both car and cdr

o  nthPair: n
a helper:
return the nth pair of a list

comparing
o  = aCons

o  hash

enumerating
o  do: aBlock
evaluate aBlock for each car

list processing
o  append: aCons
for lispers:
append the arg. Return a new list, where the 2nd part is shared.
Destructive: the receiver's last cdr is modified.
usage example(s):
     (Cons fromArray:#(1 2 3 4)) 
        append:(Cons fromArray:#(5 6 7 8)) 

o  take: nTaken
for lispers:
take n elements from the list; return a new list
usage example(s):
^ Cons car:(self car) cdr:(self cdr take:nTaken-1)
usage example(s):
     (Cons fromArray:#(1 2 3 4)) take:3  
     (Cons fromArray:#(1)) take:0  
     (Cons fromArray:#()) take:3  
     (Cons fromArray:(1 to: 1000)) take:999  

     |gen allNumbers|
     gen := [:n | LazyCons car:n cdr:[ gen value:n+1 ]].
     allNumbers := gen value:1. 
     allNumbers take:10

printing
o  displayString

o  printOn: aStream
(comment from inherited method)
append a user readable representation of the receiver to aStream.
The text appended is not meant to be read back for reconstruction of
the receiver. Also, this method limits the size of generated string.

queries
o  beginAndSizeOfCycle
return the begin and size of a cycle, if the list contains one.
Nil otherwise.
Floyd's cycle-finding algorithm

o  isCyclic
true if the list contains a cycle

o  size
the list's length
usage example(s):
     (Cons fromArray:#(1)) size       
     (Cons fromArray:#(1 2 3 4)) size    
     (Cons car:1 cdr:2) size          --> error (degenerated list)

streaming
o  readStream

testing
o  isCons

o  isLazy


Examples:


    |p1 p2 p3|

    p3 := Cons car:3 cdr:nil.
    p2 := Cons car:2 cdr:p3.
    p1 := Cons car:1 cdr:p2.
    p1 head.
    p1 tail.
    p1 size.
    p1 do:[:each | Transcript showCR:each].
    p1 at:2


ST/X 7.1.0.0; WebServer 1.663 at exept.de:8081; Wed, 19 Sep 2018 10:47:58 GMT