ngthelistsofvaluesinbrackets.Anexampleis
car1:=[\"buick\",\"skylark\",1978,2450]
inwhichthelistcar1hasfourvalues,twoofwhicharestringsandtwoofwhichareintegers.Notethatthevaluesinalistneednotallbeofthesametype.Infact,anykindofvaluecanoccurinalist--evenanotherlist,asin
inventory:=[car1,car2,car3,car4]
Listsalsocanbecreatedby
L:=list(i,x)
whichcreatesalistofivalues,eachofwhichhasthevaluex.
Thevaluesinalistcanbereferencedbyposition.Thus,
car1:=2400
changesthelastvalueincar1to2400.Areferencethatisoutoftherangeofthelistfails.Forexample,
write(car1[5])
fails.
ThevaluesinalistLaregeneratedby!L.Thus
everywrite(!L)
writesallthevaluesinL.
Listscanbemanipulatedlikestacksandqueues.Thefunctionpush(L,x)addsthevalueofxtotheleftendofthelistL,automaticallyincreasingthesizeofLbyone.Similarly,pop(L)removestheleftmostvaluefromL,automaticallydecreasingthesizeofLbyone,andproducestheremovedvalue.
4.2Sets
Asetisacollectionofvalues.Anemptysetiscreatedbyset().Alternatively,set(L)producesasetwiththevaluesinthelistL.Forexample,
S:=set([1,\"abc\",[]])
assignstoSasetthatcontainstheinteger1,thestring\"abc\",andanemptylist.
Thesetoperationsofunion,intersection,anddifferenceareprovided.Thefunctionmember(S,x)succeedsifxisamemberofthesetSbutfailsotherwise.Thefunctioninsert(S,x)addsxtothesetS,whiledelete(S,x)removesxfromS.Avalueonlycanoccuronceinaset,soinsert(S,x)hasnoeffectifxisalreadyinS.!SgeneratesthemembersofS.
Asimpleexampleoftheuseofsetsisgivenbythefollowingsegmentof,whichlistsallthedifferentwordsthatappearintheinputfile:
words:=set()
whileline:=read()do
od))srettel&(otpu(batelihw?enil
w t . p o o問訪請容內說小多更
)))srettel&(ynam(bat,sdrow(tresni
)sdrow!(etirwyreve
selbaT3.4
.stsilniseulavotsseccalanoitisopehthtiwtsartnocnisseccaevitaicossafomrofaedivorpselbat,suhT.yllacitamotuapudekoolebnacyekynarofeulavehtdna,epytynafoebyameulavgnidnopserrocstidnayekehT.eulavgnidnopserrocadnayekafostsisnochcihwfohcaesriapfosteseraselbaT
sahcusnoisserpxenaybdetaercsielbatA
)0(elbat=:slobmys
sahcus,yekynaybdecnereferebnacslobmys,yltneuqesbuS.eulavrehtonadengissatoneratahtsyekrofdesusieulavtluafedehT.0eulavtluafedehthtiwelbataslobmysotsngissahcihw
1=:]"\ereht"\[slobmys
.slobmysni"\ereht"\yekehtot1eulavehtsngissahcihw
udorptnemgesmargorpgniwollofeht,elpmaxeroF.deddaerasyekwensayllacitamotuaworgselbaT