Object subclass: #ConsCell instanceVariableNames: 'car cdr' classVariableNames: '' poolDictionaries: '' category: 'MessageReceivableCons-Lisp'! !ConsCell methodsFor: 'enumerating' stamp: 'sumim 4/25/2015 17:12'! collect: aBlock ^ self class car: (aBlock value: car) cdr: (cdr isConsCell ifTrue: [ cdr collect: aBlock ] ifFalse: [ cdr ])! ! !ConsCell methodsFor: 'accessing' stamp: 'sumim 4/25/2015 16:57'! car ^ car! ! !ConsCell methodsFor: 'accessing' stamp: 'sumim 4/25/2015 16:57'! car: aCellOrObject car := aCellOrObject! ! !ConsCell methodsFor: 'accessing' stamp: 'sumim 4/25/2015 16:57'! cdr ^ cdr! ! !ConsCell methodsFor: 'accessing' stamp: 'sumim 4/25/2015 16:57'! cdr: aCellOrObject cdr := aCellOrObject! ! !ConsCell methodsFor: 'LISP-FUNCTIONS' stamp: 'sumim 4/25/2015 17:17'! CONS ^ self class car: car cdr: cdr car! ! !ConsCell methodsFor: 'LISP-FUNCTIONS' stamp: 'sumim 4/25/2015 17:17'! CDR ^ car cdr! ! !ConsCell methodsFor: 'LISP-FUNCTIONS' stamp: 'sumim 4/25/2015 17:18'! MAP ^ cdr car collect: car! ! !ConsCell methodsFor: 'LISP-FUNCTIONS' stamp: 'sumim 4/25/2015 17:16'! EVAL | funSymbol args | funSymbol := car EVAL. args := cdr collect: #EVAL. ^ (self class car: funSymbol cdr: args) APPLY! ! !ConsCell methodsFor: 'LISP-FUNCTIONS' stamp: 'sumim 4/25/2015 17:18'! LIST ^ self! ! !ConsCell methodsFor: 'LISP-FUNCTIONS' stamp: 'sumim 4/25/2015 17:21'! APPLY cdr isNil ifTrue: [ ^ self class perform: car ]. car isBinarySelector ifTrue: [ ^ cdr asArray reduce: car ]. car numArgs = 0 ifTrue: [ ^ car value: cdr ]. ^ car valueWithArguments: cdr asArray! ! !ConsCell methodsFor: 'LISP-FUNCTIONS' stamp: 'sumim 4/25/2015 17:17'! CAR ^ car car! ! !ConsCell methodsFor: 'converting' stamp: 'sumim 4/25/2015 17:11'! asArray ^ (Array with: car), (cdr ifNil: [ #() ]) asArray! ! !ConsCell methodsFor: 'printing' stamp: 'sumim 4/25/2015 16:59'! printOn: aStream | nextCellOrObject | aStream nextPut: $(. aStream nextPutAll: car asString. nextCellOrObject := cdr. [ nextCellOrObject isConsCell ] whileTrue: [ aStream space; nextPutAll: nextCellOrObject car asString. nextCellOrObject := nextCellOrObject cdr ]. nextCellOrObject notNil ifTrue: [ aStream nextPutAll: ' . ' , nextCellOrObject asString ]. aStream nextPut: $)! ! !ConsCell methodsFor: 'testing' stamp: 'sumim 4/25/2015 16:59'! isConsCell ^ true! ! "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "! ConsCell class instanceVariableNames: ''! !ConsCell class methodsFor: 'instance creation' stamp: 'sumim 4/25/2015 17:22'! newFrom: aCollection | instance elem next | aCollection isEmpty ifTrue: [ ^ nil ]. elem := aCollection first. (elem isCollection and: [ elem isString not ]) ifTrue: [ elem := elem as: self ]. instance := next := self new car: elem; yourself. aCollection allButFirstDo: [ :each | elem := each. (elem isCollection and: [ elem isString not ]) ifTrue: [ elem := each as: self ]. next cdr: (next := self new car: elem; yourself) ]. ^ instance "#(a (1 2 3) b c) as: ConsCell"! ! !ConsCell class methodsFor: 'instance creation' stamp: 'sumim 4/25/2015 17:22'! car: carCellOrObject cdr: cdrCellOrObject ^ self new car: carCellOrObject; cdr: cdrCellOrObject; yourself "ConsCell car: 1 cdr: 2"! ! !ConsCell class methodsFor: 'examples' stamp: 'sumim 4/25/2015 17:24'! example4 | ast | ast := #(CONS (+ (CAR (CDR (CONS 3 (CONS 4 5)))) (CDR (CAR (CONS (CONS 5 6) 7)))) (CONS 11 12)) as: ConsCell. ^ ast EVAL "=> (10 11 . 12) "! ! !ConsCell class methodsFor: 'examples' stamp: 'sumim 4/25/2015 17:23'! example1 ^ #(a (1 2 3) b c) as: ConsCell "=> (a (1 2 3) b c) "! ! !ConsCell class methodsFor: 'examples' stamp: 'sumim 4/25/2015 17:23'! example2 | ast | ast := #(CDR (CONS (CONS 3 4) (CONS 1 2))) as: ConsCell. "=> (CDR (CONS (CONS 3 4) (CONS 1 2))) " ^ ast EVAL "=> (1 . 2) "! ! !ConsCell class methodsFor: 'examples' stamp: 'sumim 4/25/2015 17:25'! example5 ^ (#(MAP factorial (LIST 1 2 3)) as: ConsCell) EVAL "=> (1 2 6) "! ! !ConsCell class methodsFor: 'examples' stamp: 'sumim 4/25/2015 17:23'! example3 | ast | ast := #(+ 1 2 3 (+ 4 5)) as: ConsCell. "=> (+ 1 2 3 (+ 4 5)) " ^ ast EVAL "=> 15 "! ! !ConsCell class methodsFor: 'examples' stamp: 'sumim 4/25/2015 17:25'! example6 ^ (#(reduce: (asArray 1 2 3) +) as: ConsCell) EVAL "=> 6 "! ! 'From Pharo3.0 of 18 March 2013 [Latest update: #30864] on 25 April 2015 at 5:25:47.423754 pm'! !Symbol methodsFor: '*MessageReceivableCons-Lisp' stamp: 'sumim 4/25/2015 17:08'! valueWithArguments: args ^ args first perform: self withArguments: args allButFirst! ! 'From Pharo3.0 of 18 March 2013 [Latest update: #30864] on 25 April 2015 at 5:25:47.424754 pm'! !Symbol methodsFor: '*MessageReceivableCons-Lisp' stamp: 'sumim 4/25/2015 17:14'! isBinarySelector ^ self allSatisfy: #isSpecial! ! 'From Pharo3.0 of 18 March 2013 [Latest update: #30864] on 25 April 2015 at 5:25:47.424754 pm'! !Symbol methodsFor: '*MessageReceivableCons-Lisp' stamp: 'sumim 4/25/2015 17:10'! argumentCount ^ self numArgs + 1! ! 'From Pharo3.0 of 18 March 2013 [Latest update: #30864] on 25 April 2015 at 5:25:47.424754 pm'! !Object methodsFor: '*MessageReceivableCons-Lisp' stamp: 'sumim 4/25/2015 17:15'! EVAL ^ self! ! 'From Pharo3.0 of 18 March 2013 [Latest update: #30864] on 25 April 2015 at 5:25:47.425754 pm'! !Object methodsFor: '*MessageReceivableCons-Lisp' stamp: 'sumim 4/25/2015 17:01'! isConsCell ^ false! !