Cons.st
author Claus Gittinger <cg@exept.de>
Wed, 30 Apr 2014 13:03:07 +0200
changeset 3269 3f964738277d
parent 2753 aaebdee8dba9
child 3307 4298986dd5de
permissions -rw-r--r--
class: Cons added: #readLispAtomFrom: #readLispListFrom: removed: #realLispAtomFrom: #realLispListFrom: changed: #readLispFrom: (send #readLispListFrom: instead of #realLispListFrom:, send #readLispAtomFrom: instead of #realLispAtomFrom:)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     1
"
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     2
 COPYRIGHT (c) 2002 by eXept Software AG
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     3
              All Rights Reserved
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     4
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     5
 This software is furnished under a license and may be used
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    10
 hereby transferred.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    11
"
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    12
"{ Package: 'stx:libbasic2' }"
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    14
SequenceableCollection subclass:#Cons
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    15
	instanceVariableNames:'car cdr'
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    16
	classVariableNames:''
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    17
	poolDictionaries:''
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    18
	category:'Collections-Linked'
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
!Cons class methodsFor:'documentation'!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    23
copyright
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
"
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    25
 COPYRIGHT (c) 2002 by eXept Software AG
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    26
              All Rights Reserved
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    27
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    28
 This software is furnished under a license and may be used
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    29
 only in accordance with the terms of that license and with the
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    30
 inclusion of the above copyright notice.   This software may not
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    31
 be provided or otherwise made available to, or used by, any
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    32
 other person.  No title to or ownership of the software is
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    33
 hereby transferred.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    34
"
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    35
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    36
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    37
documentation
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    38
"
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    39
    A pair as in lisp.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    40
    Create with:
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
    41
        a !! b
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
    42
    or:
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
    43
        Cons car:a cdr:b
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
    44
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
    45
    Conses are not heavily used by Smalltalk (actually: not at all).
1377
150000bb9d20 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1248
diff changeset
    46
    Consider this a demo class.
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    47
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    48
    [author:]
2662
b3249d9d9134 changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 2660
diff changeset
    49
        Claus Gittinger (Jun 2002)
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    50
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    51
    [see also:]
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    52
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
"
1627
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    54
!
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    55
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    56
examples
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    57
"
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    58
                                                                        [exBegin]
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    59
    |p1 p2 p3|
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    60
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    61
    p3 := Cons car:3 cdr:nil.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    62
    p2 := Cons car:2 cdr:p3.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    63
    p1 := Cons car:1 cdr:p2.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    64
    p1 head.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    65
    p1 tail.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    66
    p1 size.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    67
    p1 do:[:each | Transcript showCR:each].
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    68
    p1 at:2
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    69
                                                                        [exEnd]
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    70
"
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    71
! !
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    72
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    73
!Cons class methodsFor:'instance creation'!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    74
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    75
car:carArg cdr:cdrArg
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    76
    ^ self basicNew car:carArg cdr:cdrArg
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    77
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    78
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    79
fromArray:anArray
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    80
    |p last first|
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    81
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    82
    anArray do:[:el |
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    83
        p := self car:el cdr:nil.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    84
        first isNil ifTrue:[
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    85
            first := p.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    86
        ] ifFalse:[
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    87
            last cdr:p.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    88
        ].
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
        last := p.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    90
    ].
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    91
    ^ first.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    93
    "
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
    94
     Cons fromArray:#(1 2 3 4)   
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
    95
     Cons fromArray:#()    
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
    96
     Cons fromArray:#(1)    
1248
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
    97
     Cons fromArray:(1 to:10000)    
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    98
    "
2556
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
    99
!
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   100
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   101
makeList:size
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   102
    |first prev this|
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   103
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   104
    size == 0 ifTrue:[^ nil].
2557
35bc2d58019d comment/format in: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2556
diff changeset
   105
2556
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   106
    first := prev := self car:nil cdr:nil.
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   107
    2 to:size do:[:n |
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   108
        this := self car:nil cdr:nil.
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   109
        prev cdr:this.
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   110
        prev := this
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   111
    ].
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   112
    ^ first
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   113
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   114
    "
2557
35bc2d58019d comment/format in: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2556
diff changeset
   115
     (self makeList:0) size    
35bc2d58019d comment/format in: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2556
diff changeset
   116
     (self makeList:1) size    
2556
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   117
     (self makeList:100) size
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   118
     (self makeList:1000) size 
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   119
     (self makeList:10000) size 
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   120
    "
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   121
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   122
    "Created: / 28-04-2011 / 00:37:11 / cg"
2557
35bc2d58019d comment/format in: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2556
diff changeset
   123
    "Modified: / 29-04-2011 / 10:38:11 / cg"
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   124
! !
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   125
2469
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   126
!Cons class methodsFor:'sExpressions'!
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   127
3269
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   128
readLispAtomFrom:aStream
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   129
    |chars n|
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   130
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   131
    aStream skipSeparators.
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   132
    aStream atEnd ifTrue:[^ nil].
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   133
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   134
    chars := aStream upToMatching:[:ch | ch isSeparator or:[ch = $( or:[ch = $)]]].
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   135
    (n := Number readFromString:chars onError:nil) notNil ifTrue:[
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   136
        "/ smalltalk allows +n; scheme does not
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   137
        (chars startsWith:'+') ifFalse:[
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   138
            ^ n
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   139
        ].
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   140
    ].
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   141
    ^ chars asSymbol
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   142
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   143
    "
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   144
     self readLispFrom:('(cons 1 2)' readStream).
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   145
     self readLispFrom:('(cons 1+ 2)' readStream).  
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   146
     self readLispFrom:('(cons +1 2)' readStream).
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   147
    "
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   148
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   149
    "Created: / 08-08-2010 / 17:15:18 / cg"
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   150
!
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   151
2469
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   152
readLispFrom:aStream
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   153
    aStream skipSeparators.
2470
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   154
    aStream atEnd ifTrue:[^ nil].
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   155
2469
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   156
    aStream peek ==$; ifTrue:[
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   157
        "/ comment
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   158
        aStream skipLine.
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   159
        thisContext restart
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   160
    ].
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   161
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   162
    aStream peek ==$( ifTrue:[
3269
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   163
        ^ self readLispListFrom:aStream
2469
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   164
    ].
3269
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   165
    ^ self readLispAtomFrom:aStream
2469
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   166
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   167
    "
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   168
     self readLispFrom:('(cons 1 2)' readStream).
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   169
    "
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   170
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   171
    "Created: / 08-08-2010 / 17:07:49 / cg"
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   172
!
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   173
3269
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   174
readLispListFrom:aStream
2469
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   175
    |first this prev element|
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   176
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   177
    aStream next.   "/ the leading '('
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   178
    [
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   179
        aStream skipSeparators.
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   180
        aStream peek ~= $)
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   181
    ] whileTrue:[
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   182
        element := self readLispFrom:aStream.
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   183
        this := Cons car:element cdr:nil.
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   184
        prev isNil ifTrue:[
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   185
            first := this
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   186
        ] ifFalse:[
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   187
            prev cdr:this.
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   188
        ].
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   189
        prev := this.
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   190
    ].
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   191
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   192
    aStream next.   "/ the trailing ')'
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   193
    ^ first.
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   194
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   195
    "
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   196
     self readLispFrom:('(cons 1 2)' readStream).
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   197
    "
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   198
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   199
    "Modified: / 08-08-2010 / 17:15:54 / cg"
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   200
! !
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   201
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   202
!Cons methodsFor:'accessing'!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   203
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   204
at:n
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   205
    "for collection compatibility:
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   206
     a slow indexed accessor"
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   207
1627
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   208
    ^ (self nth:n)
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   209
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   210
    "
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   211
     (Cons fromArray:#(1))       at:1     
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   212
     (Cons fromArray:#(1 2 3 4)) at:1 
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   213
     (Cons fromArray:#(1 2 3 4)) at:3  
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   214
     (Cons fromArray:#(1 2 3 4)) at:4  
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   215
     (Cons fromArray:#(1 2 3 4)) at:5  
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   216
    "
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   217
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   218
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   219
at:n put:newValue
1627
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   220
    "destructive: 
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   221
     for collection compatibility: a slow indexed accessor"
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   222
1627
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   223
    (self nthPair:n) car:newValue.
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   224
    ^ newValue.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   225
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   226
    "
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   227
     |l|
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   228
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   229
     l := Cons fromArray:#(1 2 3 4).
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   230
     l at:1 put:'one'.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   231
     l at:3 put:'three'.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   232
     l       
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   233
    "
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   234
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   235
1623
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   236
first
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   237
    "return the head, first or car - whatever you wonna call it"
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   238
2469
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   239
    ^ car
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   240
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   241
    "Modified: / 08-08-2010 / 17:04:23 / cg"
1623
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   242
!
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   243
1237
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   244
head
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   245
    "return the head, first or car - whatever you wonna call it"
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   246
2469
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   247
    ^ car
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   248
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   249
    "Modified: / 08-08-2010 / 17:04:20 / cg"
1237
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   250
!
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   251
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   252
last
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   253
    "for lispers:
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   254
     return the last element of a list"
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   255
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   256
    |p rest|
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   257
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   258
    p := self.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   259
    [(rest := p cdr) notNil] whileTrue:[
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   260
        p := rest
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   261
    ].
1237
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   262
    ^ p car
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   263
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   264
    "
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   265
     (Cons fromArray:#(1))       last     
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   266
     (Cons fromArray:#(1 2 3 4)) last    
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   267
    "
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   268
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   269
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   270
nth:n
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   271
    "for lispers:
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   272
     return the nth element of a list"
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   273
1627
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   274
    ^ (self nthPair:n) car
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   275
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   276
    "
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   277
     (Cons fromArray:#(1))       nth:1     
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   278
     (Cons fromArray:#(1 2 3 4)) nth:1 
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   279
     (Cons fromArray:#(1 2 3 4)) nth:3  
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   280
     (Cons fromArray:#(1 2 3 4)) nth:4  
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   281
     (Cons fromArray:#(1 2 3 4)) nth:5  
1248
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   282
     (Cons fromArray:#( ))       nth:1  -> error    
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   283
    "
1237
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   284
!
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   285
1623
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   286
rest
2469
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   287
    "return the tail, rest or cdr - whatever you wonna call it"
1623
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   288
2469
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   289
    ^ cdr
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   290
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   291
    "Modified: / 08-08-2010 / 17:04:48 / cg"
1623
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   292
!
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   293
1243
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   294
reversed
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   295
    "for lispers:
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   296
     return a new list with the cars in reverse order"
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   297
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   298
"/ for now, tail recursion is not yet optimized by the st/x jitter...
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   299
"/
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   300
"/    |rev|
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   301
"/
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   302
"/    rev := [:lst :acc |
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   303
"/                lst isNil ifTrue:[
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   304
"/                    acc
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   305
"/                ] ifFalse:[
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   306
"/                    rev value:(lst tail)
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   307
"/                        value:(Cons car:(lst head) cdr:acc)
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   308
"/                ]
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   309
"/           ].
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   310
"/    ^ rev value:self value:nil
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   311
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   312
    | lst acc|
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   313
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   314
    lst := self.
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   315
    acc := nil.
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   316
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   317
    [
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   318
        |nLst nAcc|
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   319
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   320
        lst isNil ifTrue:[ ^ acc].
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   321
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   322
        nLst := lst tail.
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   323
        nAcc := Cons car:(lst head) cdr:acc.
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   324
        lst := nLst.
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   325
        acc := nAcc.
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   326
   ] loop
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   327
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   328
    "
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   329
     (Cons fromArray:#(1))       reversed     
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   330
     (Cons fromArray:#(1 2))     reversed     
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   331
     (Cons fromArray:#(1 2 3 4)) reversed    
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   332
     (Cons fromArray:(1 to:10000)) reversed    
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   333
    "
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   334
!
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   335
1237
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   336
tail
2469
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   337
    "return the tail, rest or cdr - whatever you wonna call it"
1237
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   338
2469
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   339
    ^ cdr
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   340
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   341
    "Modified: / 08-08-2010 / 17:04:59 / cg"
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   342
! !
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   343
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   344
!Cons methodsFor:'accessing - basic'!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   345
2470
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   346
cadddr
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   347
    "return the fourth element"
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   348
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   349
    ^ cdr cdr cdr car
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   350
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   351
    "Created: / 08-08-2010 / 17:29:40 / cg"
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   352
!
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   353
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   354
caddr
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   355
    "return the third element"
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   356
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   357
    ^ cdr cdr car
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   358
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   359
    "Created: / 08-08-2010 / 17:29:32 / cg"
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   360
!
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   361
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   362
cadr
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   363
    "return the second element"
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   364
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   365
    ^ cdr car
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   366
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   367
    "Created: / 08-08-2010 / 17:29:25 / cg"
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   368
!
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   369
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   370
car
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   371
    "return the head, first or car - whatever you wonna call it"
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   372
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   373
    ^ car
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   374
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   375
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   376
car:something
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   377
    "set the head, first or car - whatever you wonna call it"
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   378
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   379
    car := something.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   380
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   381
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   382
car:carArg cdr:cdrArg 
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   383
    "set both car and cdr"
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   384
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   385
    car := carArg.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   386
    cdr := cdrArg.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   387
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   388
2472
bdfac3129628 added: #cddr
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
   389
cddr
bdfac3129628 added: #cddr
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
   390
    "return the rest after the second element"
bdfac3129628 added: #cddr
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
   391
bdfac3129628 added: #cddr
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
   392
    ^ cdr cdr
bdfac3129628 added: #cddr
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
   393
bdfac3129628 added: #cddr
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
   394
    "Created: / 08-08-2010 / 17:47:11 / cg"
bdfac3129628 added: #cddr
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
   395
!
bdfac3129628 added: #cddr
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
   396
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   397
cdr
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   398
    "return the tail, second or cdr - whatever you wonna call it"
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   399
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   400
    ^ cdr
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   401
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   402
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   403
cdr:something
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   404
    "set the tail, second or cdr - whatever you wonna call it"
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   405
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   406
    cdr := something.
1623
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   407
!
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   408
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   409
first:carArg rest:cdrArg 
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   410
    "set both car and cdr"
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   411
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   412
    car := carArg.
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   413
    cdr := cdrArg.
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   414
!
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   415
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   416
head:carArg tail:cdrArg 
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   417
    "set both car and cdr"
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   418
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   419
    car := carArg.
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   420
    cdr := cdrArg.
1627
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   421
!
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   422
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   423
nthPair:n
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   424
    "a helper:
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   425
     return the nth pair of a list"
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   426
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   427
    |cnt p|
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   428
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   429
    cnt := n.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   430
    p := self.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   431
    [
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   432
        cnt := cnt - 1.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   433
        cnt == 0 ifTrue:[^ p].
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   434
        p := p cdr.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   435
        p isNil ifTrue:[
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   436
            self error:'no such element' mayProceed:true.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   437
            ^ nil
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   438
        ].
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   439
    ] loop.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   440
! !
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   441
2437
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   442
!Cons methodsFor:'comparing'!
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   443
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   444
= aCons
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   445
    ^ aCons class == self class
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   446
    and:[ car = aCons car
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   447
    and:[ cdr = aCons cdr ]]
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   448
!
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   449
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   450
hash
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   451
    ^ car hash bitXor: cdr hash
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   452
! !
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   453
1627
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   454
!Cons methodsFor:'enumerating'!
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   455
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   456
do:aBlock
2471
d3561a593169 changed: #do:
Claus Gittinger <cg@exept.de>
parents: 2470
diff changeset
   457
    "evaluate aBlock for each car"
d3561a593169 changed: #do:
Claus Gittinger <cg@exept.de>
parents: 2470
diff changeset
   458
1627
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   459
    |ptr|
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   460
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   461
    aBlock value:car.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   462
    ptr := cdr.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   463
    [ ptr notNil ] whileTrue:[
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   464
        aBlock value:ptr car.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   465
        ptr := ptr cdr.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   466
    ].
2471
d3561a593169 changed: #do:
Claus Gittinger <cg@exept.de>
parents: 2470
diff changeset
   467
d3561a593169 changed: #do:
Claus Gittinger <cg@exept.de>
parents: 2470
diff changeset
   468
    "Modified: / 08-08-2010 / 17:33:44 / cg"
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   469
! !
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   470
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   471
!Cons methodsFor:'list processing'!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   472
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   473
append:aCons
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   474
    "for lispers:
1248
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   475
     append the arg. Return a new list, where the 2nd part is shared.
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   476
     Destructive: the receivers last cdr is modified."
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   477
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   478
    |p rest|
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   479
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   480
    p := self.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   481
    [(rest := p cdr) notNil] whileTrue:[
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   482
        p := rest
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   483
    ].
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   484
    p cdr:aCons.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   485
    ^ self
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   486
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   487
    "
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   488
     (Cons fromArray:#(1 2 3 4)) 
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   489
        append:(Cons fromArray:#(5 6 7 8)) 
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   490
    "
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   491
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   492
    "sharing demonstrated:
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   493
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   494
     |a b ab|
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   495
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   496
     a := Cons fromArray:#(1 2 3 4).
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   497
     b := Cons fromArray:#(5 6 7 8).
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   498
     ab := a append:b.
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   499
     b car:'five'.
1248
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   500
     ab      
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   501
    "
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   502
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   503
    "destruction demonstrated:
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   504
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   505
     |a b ab|
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   506
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   507
     a := Cons fromArray:#(1 2 3 4).
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   508
     b := Cons fromArray:#(5 6 7 8).
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   509
     ab := a append:b.
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   510
     a  
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   511
    "
1237
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   512
!
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   513
1248
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   514
take:nTaken
1237
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   515
    "for lispers:
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   516
     take n elements from the list; return a new list"
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   517
1248
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   518
    |nRemain l rslt lastCons cons|
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   519
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   520
    nTaken > 0 ifTrue:[
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   521
        "/ avoiding recursion here...
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   522
        "/ instead of:
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   523
        "/        ^ Cons car:(self car) cdr:(self cdr take:nTaken-1)
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   524
        "/ we do:
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   525
        nRemain := nTaken.
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   526
        l := self.
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   527
        rslt := lastCons := Cons car:(l car) cdr:nil.
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   528
        [nRemain > 1] whileTrue:[
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   529
            l := l cdr.
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   530
            cons := Cons car:(l car) cdr:nil.    
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   531
            lastCons cdr:cons.
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   532
            lastCons := cons.
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   533
            nRemain := nRemain - 1.
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   534
        ].
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   535
        ^ rslt.
1237
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   536
    ].
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   537
    ^ nil
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   538
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   539
    "
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   540
     (Cons fromArray:#(1 2 3 4)) take:3  
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   541
     (Cons fromArray:#(1)) take:0  
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   542
     (Cons fromArray:#()) take:3  
1248
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   543
     (Cons fromArray:(1 to: 1000)) take:999  
2656
0d2ca6713ca7 comment/format in: #take:
Claus Gittinger <cg@exept.de>
parents: 2557
diff changeset
   544
0d2ca6713ca7 comment/format in: #take:
Claus Gittinger <cg@exept.de>
parents: 2557
diff changeset
   545
     |gen allNumbers|
0d2ca6713ca7 comment/format in: #take:
Claus Gittinger <cg@exept.de>
parents: 2557
diff changeset
   546
     gen := [:n | LazyCons car:n cdr:[ gen value:n+1 ]].
0d2ca6713ca7 comment/format in: #take:
Claus Gittinger <cg@exept.de>
parents: 2557
diff changeset
   547
     allNumbers := gen value:1. 
0d2ca6713ca7 comment/format in: #take:
Claus Gittinger <cg@exept.de>
parents: 2557
diff changeset
   548
     allNumbers take:10
1237
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   549
    "
2656
0d2ca6713ca7 comment/format in: #take:
Claus Gittinger <cg@exept.de>
parents: 2557
diff changeset
   550
0d2ca6713ca7 comment/format in: #take:
Claus Gittinger <cg@exept.de>
parents: 2557
diff changeset
   551
    "Modified (comment): / 27-09-2011 / 11:31:38 / cg"
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   552
! !
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   553
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   554
!Cons methodsFor:'printing'!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   555
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   556
displayString
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   557
    ^ self printString
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   558
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   559
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   560
printOn:aStream
2448
f21170d2e545 changed: #printOn:
Claus Gittinger <cg@exept.de>
parents: 2437
diff changeset
   561
    thisContext isRecursive ifTrue:[ 
f21170d2e545 changed: #printOn:
Claus Gittinger <cg@exept.de>
parents: 2437
diff changeset
   562
        'Cons [error]: printOn: of self referencing collection.' errorPrintCR.
f21170d2e545 changed: #printOn:
Claus Gittinger <cg@exept.de>
parents: 2437
diff changeset
   563
        aStream nextPutAll:'#("recursive")'.
f21170d2e545 changed: #printOn:
Claus Gittinger <cg@exept.de>
parents: 2437
diff changeset
   564
        ^ self.
f21170d2e545 changed: #printOn:
Claus Gittinger <cg@exept.de>
parents: 2437
diff changeset
   565
    ].
f21170d2e545 changed: #printOn:
Claus Gittinger <cg@exept.de>
parents: 2437
diff changeset
   566
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   567
    (car isLazyValue not and:[ car isCons ]) ifTrue:[
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   568
        aStream nextPutAll:'('.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   569
        car printOn:aStream.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   570
        aStream nextPutAll:')'.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   571
    ] ifFalse:[
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   572
        car printOn:aStream.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   573
    ].
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   574
1073
1b0886e40a5a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1043
diff changeset
   575
    aStream nextPutAll:'!!'.
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   576
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   577
    (cdr isLazyValue not and:[ cdr isCons ]) ifTrue:[
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   578
        aStream nextPutAll:'('.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   579
        cdr printOn:aStream.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   580
        aStream nextPutAll:')'.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   581
    ] ifFalse:[
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   582
        cdr printOn:aStream.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   583
    ].
2448
f21170d2e545 changed: #printOn:
Claus Gittinger <cg@exept.de>
parents: 2437
diff changeset
   584
f21170d2e545 changed: #printOn:
Claus Gittinger <cg@exept.de>
parents: 2437
diff changeset
   585
    "Modified: / 18-05-2010 / 10:25:49 / cg"
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   586
! !
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   587
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   588
!Cons methodsFor:'queries'!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   589
2753
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   590
beginAndSizeOfCycle
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   591
    "true if the list contains a cycle.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   592
     Floyd's cycle-finding algorithm"
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   593
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   594
    |t h i loopStartIndex loopSize|
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   595
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   596
    t := self cdr. t isNil ifTrue:[^ nil].
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   597
    h := t cdr. h isNil ifTrue:[^ nil].
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   598
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   599
    [t ~~ h] whileTrue:[
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   600
        t := t cdr.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   601
        h := h cdr. h isNil ifTrue:[^ nil].
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   602
        h := h cdr. h isNil ifTrue:[^ nil].
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   603
    ].
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   604
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   605
    "/ both h and t are now inside the cycle,
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   606
    "/ equidistant from the start of the cycle
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   607
    t := self.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   608
    i := 1.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   609
    [t ~~ h] whileTrue:[
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   610
        t := t cdr.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   611
        h := h cdr.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   612
        i := i + 1.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   613
    ].
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   614
    loopStartIndex := i.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   615
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   616
    loopSize := 1.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   617
    h := t cdr.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   618
    [t ~~ h] whileTrue:[
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   619
        h := h cdr.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   620
        i := i + 1.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   621
        loopSize := loopSize + 1.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   622
    ].
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   623
    
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   624
    ^ { loopStartIndex. loopSize }
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   625
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   626
    "
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   627
     |n1 n2 n3 n4 n5|
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   628
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   629
     n1 := Cons new car:1.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   630
     n2 := Cons new car:2.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   631
     n3 := Cons new car:3.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   632
     n4 := Cons new car:4.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   633
     n5 := Cons new car:5.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   634
     n1 cdr: n2.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   635
     n2 cdr: n3.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   636
     n3 cdr: n4.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   637
     n4 cdr: n5.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   638
     n1 beginAndSizeOfCycle.      
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   639
     n5 cdr: n3.    
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   640
     n1 beginAndSizeOfCycle.             
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   641
    "
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   642
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   643
    "Created: / 27-07-2012 / 00:00:36 / cg"
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   644
!
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   645
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   646
isCons
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   647
    ^ true
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   648
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   649
2753
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   650
isCyclic
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   651
    "true if the list contains a cycle"
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   652
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   653
    ^ self beginAndSizeOfCycle notNil
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   654
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   655
    "
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   656
     |n1 n2 n3 n4 n5|
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   657
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   658
     n1 := Cons new car:1.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   659
     n2 := Cons new car:2.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   660
     n3 := Cons new car:3.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   661
     n4 := Cons new car:4.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   662
     n5 := Cons new car:5.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   663
     n1 cdr: n2.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   664
     n2 cdr: n3.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   665
     n3 cdr: n4.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   666
     n4 cdr: n5.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   667
     n1 isCyclic.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   668
     n5 cdr: n3.    
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   669
     n1 isCyclic.   
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   670
    "
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   671
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   672
    "Created: / 26-07-2012 / 23:32:52 / cg"
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   673
!
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   674
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   675
size
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   676
    "for smalltalkers: the lists length"
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   677
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   678
    |l p rest|
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   679
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   680
    l := 1.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   681
    p := self.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   682
    [(rest := p cdr) notNil] whileTrue:[
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   683
        l := l + 1.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   684
        p := rest
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   685
    ].
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   686
    ^ l
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   687
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   688
    "
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   689
     (Cons fromArray:#( )) size    
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   690
     (Cons fromArray:#(1)) size     
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   691
     (Cons fromArray:#(1 2 3 4)) size    
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   692
    "
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   693
! !
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   694
1377
150000bb9d20 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1248
diff changeset
   695
!Cons methodsFor:'streaming'!
150000bb9d20 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1248
diff changeset
   696
150000bb9d20 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1248
diff changeset
   697
readStream
150000bb9d20 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1248
diff changeset
   698
    ^ ConsStream on:self.
150000bb9d20 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1248
diff changeset
   699
! !
150000bb9d20 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1248
diff changeset
   700
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   701
!Cons class methodsFor:'documentation'!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   702
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   703
version
3269
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   704
    ^ '$Header: /cvs/stx/stx/libbasic2/Cons.st,v 1.22 2014-04-30 11:03:07 cg Exp $'
2437
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   705
!
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   706
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   707
version_CVS
3269
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   708
    ^ '$Header: /cvs/stx/stx/libbasic2/Cons.st,v 1.22 2014-04-30 11:03:07 cg Exp $'
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   709
! !
3269
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   710