My various dotfiles

chap-14.texi 113KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848
  1. @node Conses, Arrays, Characters, Top
  2. @chapter Conses
  3. @menu
  4. * Cons Concepts::
  5. * Conses Dictionary::
  6. @end menu
  7. @node Cons Concepts, Conses Dictionary, Conses, Conses
  8. @section Cons Concepts
  9. @c including concept-conses
  10. A @i{cons}
  11. @IGindex{cons}
  12. is a compound data @i{object}
  13. having two components called the @i{car} and the @i{cdr}.
  14. @group
  15. @noindent
  16. @w{ car cons rplacd }
  17. @w{ cdr rplaca }
  18. @noindent
  19. @w{ Figure 14--1: Some defined names relating to conses.}
  20. @end group
  21. Depending on context, a group of connected @i{conses} can be viewed
  22. in a variety of different ways. A variety of operations is provided to
  23. support each of these various views.
  24. @menu
  25. * Conses as Trees::
  26. * Conses as Lists::
  27. @end menu
  28. @node Conses as Trees, Conses as Lists, Cons Concepts, Cons Concepts
  29. @subsection Conses as Trees
  30. A @i{tree}
  31. @IGindex{tree}
  32. is a binary recursive data structure made up of
  33. @i{conses} and @i{atoms}:
  34. the @i{conses} are themselves also @i{trees}
  35. (sometimes called ``subtrees'' or ``branches''), and the @i{atoms}
  36. are terminal nodes (sometimes called @i{leaves}
  37. @IGindex{leaves}
  38. ).
  39. Typically, the @i{leaves} represent data while the branches
  40. establish some relationship among that data.
  41. @group
  42. @noindent
  43. @w{ caaaar caddar cdar nsubst }
  44. @w{ caaadr cadddr cddaar nsubst-if }
  45. @w{ caaar caddr cddadr nsubst-if-not }
  46. @w{ caadar cadr cddar nthcdr }
  47. @w{ caaddr cdaaar cdddar sublis }
  48. @w{ caadr cdaadr cddddr subst }
  49. @w{ caar cdaar cdddr subst-if }
  50. @w{ cadaar cdadar cddr subst-if-not }
  51. @w{ cadadr cdaddr copy-tree tree-equal }
  52. @w{ cadar cdadr nsublis }
  53. @noindent
  54. @w{ Figure 14--2: Some defined names relating to trees.}
  55. @end group
  56. @menu
  57. * General Restrictions on Parameters that must be Trees::
  58. @end menu
  59. @node General Restrictions on Parameters that must be Trees, , Conses as Trees, Conses as Trees
  60. @subsubsection General Restrictions on Parameters that must be Trees
  61. Except as explicitly stated otherwise,
  62. for any @i{standardized} @i{function} that takes a @i{parameter}
  63. that is required to be a @i{tree},
  64. the consequences are undefined
  65. if that @i{tree} is circular.
  66. @node Conses as Lists, , Conses as Trees, Cons Concepts
  67. @subsection Conses as Lists
  68. A @i{list}
  69. @IGindex{list}
  70. is a chain of @i{conses} in which the @i{car} of each
  71. @i{cons} is an @i{element} of the @i{list},
  72. and the @i{cdr} of each @i{cons} is either the next
  73. link in the chain or a terminating @i{atom}.
  74. A @i{proper list}
  75. @IGindex{proper list}
  76. is a @i{list} terminated by the @i{empty list}.
  77. The @i{empty list} is a @i{proper list}, but is not a @i{cons}.
  78. An @i{improper list}
  79. @IGindex{improper list}
  80. is a @i{list} that is not a @i{proper list};
  81. that is, it is a @i{circular list} or a @i{dotted list}.
  82. A @i{dotted list}
  83. @IGindex{dotted list}
  84. is a @i{list} that has a terminating @i{atom}
  85. that is not the @i{empty list}. A @i{non-nil} @i{atom} by itself
  86. is not considered to be a @i{list} of any kind---not even a @i{dotted list}.
  87. A @i{circular list}
  88. @IGindex{circular list}
  89. is a chain of @i{conses} that has no termination
  90. because some @i{cons} in the chain is the @i{cdr} of a later @i{cons}.
  91. @group
  92. @noindent
  93. @w{ append last nbutlast rest }
  94. @w{ butlast ldiff nconc revappend }
  95. @w{ copy-alist list ninth second }
  96. @w{ copy-list list* nreconc seventh }
  97. @w{ eighth list-length nth sixth }
  98. @w{ endp make-list nthcdr tailp }
  99. @w{ fifth member pop tenth }
  100. @w{ first member-if push third }
  101. @w{ fourth member-if-not pushnew }
  102. @noindent
  103. @w{ Figure 14--3: Some defined names relating to lists.}
  104. @end group
  105. @menu
  106. * Lists as Association Lists::
  107. * Lists as Sets::
  108. * General Restrictions on Parameters that must be Lists::
  109. @end menu
  110. @node Lists as Association Lists, Lists as Sets, Conses as Lists, Conses as Lists
  111. @subsubsection Lists as Association Lists
  112. An @i{association list}
  113. @IGindex{association list}
  114. is a @i{list} of @i{conses}
  115. representing an association of @i{keys} with @i{values},
  116. where the @i{car} of each @i{cons} is the @i{key}
  117. and the @i{cdr} is the @i{value} associated with that @i{key}.
  118. @group
  119. @noindent
  120. @w{ acons assoc-if pairlis rassoc-if }
  121. @w{ assoc assoc-if-not rassoc rassoc-if-not }
  122. @noindent
  123. @w{ Figure 14--4: Some defined names related to assocation lists.}
  124. @end group
  125. @node Lists as Sets, General Restrictions on Parameters that must be Lists, Lists as Association Lists, Conses as Lists
  126. @subsubsection Lists as Sets
  127. @i{Lists} are sometimes viewed as sets by considering their elements
  128. unordered and by assuming there is no duplication of elements.
  129. @group
  130. @noindent
  131. @w{ adjoin nset-difference set-difference union }
  132. @w{ intersection nset-exclusive-or set-exclusive-or }
  133. @w{ nintersection nunion subsetp }
  134. @noindent
  135. @w{ Figure 14--5: Some defined names related to sets. }
  136. @end group
  137. @node General Restrictions on Parameters that must be Lists, , Lists as Sets, Conses as Lists
  138. @subsubsection General Restrictions on Parameters that must be Lists
  139. Except as explicitly specified otherwise,
  140. any @i{standardized} @i{function} that takes a @i{parameter}
  141. that is required to be a @i{list} should be prepared to signal
  142. an error of @i{type} @b{type-error} if the @i{value} received is a @i{dotted list}.
  143. Except as explicitly specified otherwise,
  144. for any @i{standardized} @i{function} that takes a @i{parameter}
  145. that is required to be a @i{list},
  146. the consequences are undefined
  147. if that @i{list} is @i{circular}.
  148. @c end of including concept-conses
  149. @node Conses Dictionary, , Cons Concepts, Conses
  150. @section Conses Dictionary
  151. @c including dict-conses
  152. @menu
  153. * list::
  154. * null (System Class)::
  155. * cons (System Class)::
  156. * atom (Type)::
  157. * cons::
  158. * consp::
  159. * atom::
  160. * rplaca::
  161. * car::
  162. * copy-tree::
  163. * sublis::
  164. * subst::
  165. * tree-equal::
  166. * copy-list::
  167. * list::
  168. * list-length::
  169. * listp::
  170. * make-list::
  171. * push::
  172. * pop::
  173. * first::
  174. * nth::
  175. * endp::
  176. * null::
  177. * nconc::
  178. * append::
  179. * revappend::
  180. * butlast::
  181. * last::
  182. * ldiff::
  183. * nthcdr::
  184. * rest::
  185. * member::
  186. * mapc::
  187. * acons::
  188. * assoc::
  189. * copy-alist::
  190. * pairlis::
  191. * rassoc::
  192. * get-properties::
  193. * getf::
  194. * remf::
  195. * intersection::
  196. * adjoin::
  197. * pushnew::
  198. * set-difference::
  199. * set-exclusive-or::
  200. * subsetp::
  201. * union::
  202. @end menu
  203. @node list, null (System Class), Conses Dictionary, Conses Dictionary
  204. @subsection list [System Class]
  205. @subsubheading Class Precedence List::
  206. @b{list},
  207. @b{sequence},
  208. @b{t}
  209. @subsubheading Description::
  210. A @i{list}
  211. @IGindex{list}
  212. is a chain of @i{conses} in which the @i{car} of each
  213. @i{cons} is an @i{element} of the @i{list}, and the @i{cdr} of
  214. each @i{cons} is either the next link in the chain or a terminating
  215. @i{atom}.
  216. A @i{proper list}
  217. @IGindex{proper list}
  218. is a chain of @i{conses} terminated by
  219. the @i{empty list}
  220. @IGindex{empty list}
  221. , @t{()}, which is itself a @i{proper list}.
  222. A @i{dotted list}
  223. @IGindex{dotted list}
  224. is a @i{list} which has a terminating @i{atom}
  225. that is not the @i{empty list}.
  226. A @i{circular list}
  227. @IGindex{circular list}
  228. is a chain of @i{conses} that has no termination
  229. because some @i{cons} in the chain is the @i{cdr} of a later @i{cons}.
  230. @i{Dotted lists} and @i{circular lists} are also @i{lists}, but usually
  231. the unqualified term ``list'' within this specification means @i{proper list}.
  232. Nevertheless, the @i{type} @b{list} unambiguously includes @i{dotted lists}
  233. and @i{circular lists}.
  234. For each @i{element} of a @i{list} there is a @i{cons}.
  235. The @i{empty list} has no @i{elements} and is not a @i{cons}.
  236. The @i{types} @b{cons} and @b{null} form an @i{exhaustive partition}
  237. of the @i{type} @b{list}.
  238. @subsubheading See Also::
  239. @ref{Left-Parenthesis},
  240. @ref{Printing Lists and Conses}
  241. @node null (System Class), cons (System Class), list, Conses Dictionary
  242. @subsection null [System Class]
  243. @subsubheading Class Precedence List::
  244. @b{null},
  245. @b{symbol},
  246. @b{list},
  247. @b{sequence},
  248. @b{t}
  249. @subsubheading Description::
  250. The only @i{object} of @i{type} @b{null} is @b{nil},
  251. which represents the @i{empty list} and can also be notated @t{()}.
  252. @subsubheading See Also::
  253. @ref{Symbols as Tokens},
  254. @ref{Left-Parenthesis},
  255. @ref{Printing Symbols}
  256. @node cons (System Class), atom (Type), null (System Class), Conses Dictionary
  257. @subsection cons [System Class]
  258. @subsubheading Class Precedence List::
  259. @b{cons},
  260. @b{list},
  261. @b{sequence},
  262. @b{t}
  263. @subsubheading Description::
  264. A @i{cons} is a compound @i{object} having two components,
  265. called the @i{car} and @i{cdr}. These form a @i{dotted pair}.
  266. Each component can be any @i{object}.
  267. @subsubheading Compound Type Specifier Kind::
  268. Specializing.
  269. @subsubheading Compound Type Specifier Syntax::
  270. (@code{cons}@{@i{@t{[}car-typespec @r{[}cdr-typespec@r{]}@t{]}}@})
  271. @subsubheading Compound Type Specifier Arguments::
  272. @i{car-typespec}---a @i{type specifier},
  273. or the @i{symbol} @b{*}.
  274. The default is the @i{symbol} @b{*}.
  275. @i{cdr-typespec}---a @i{type specifier},
  276. or the @i{symbol} @b{*}.
  277. The default is the @i{symbol} @b{*}.
  278. @subsubheading Compound Type Specifier Description::
  279. This denotes the set of @i{conses}
  280. whose @i{car} is constrained to be of @i{type} @i{car-typespec} and
  281. whose @i{cdr} is constrained to be of @i{type} @i{cdr-typespec}.
  282. (If either @i{car-typespec} or @i{cdr-typespec} is @b{*},
  283. it is as if the @i{type} @b{t} had been denoted.)
  284. @subsubheading See Also::
  285. @ref{Left-Parenthesis},
  286. @ref{Printing Lists and Conses}
  287. @node atom (Type), cons, cons (System Class), Conses Dictionary
  288. @subsection atom [Type]
  289. @subsubheading Supertypes::
  290. @b{atom},
  291. @b{t}
  292. @subsubheading Description::
  293. It is equivalent to @t{(not cons)}.
  294. @node cons, consp, atom (Type), Conses Dictionary
  295. @subsection cons [Function]
  296. @code{cons} @i{object-1 object-2} @result{} @i{cons}
  297. @subsubheading Arguments and Values::
  298. @i{object-1}---an @i{object}.
  299. @i{object-2}---an @i{object}.
  300. @i{cons}---a @i{cons}.
  301. @subsubheading Description::
  302. Creates a @i{fresh} @i{cons}, the @i{car} of which is @i{object-1}
  303. and the @i{cdr} of which is @i{object-2}.
  304. @subsubheading Examples::
  305. @example
  306. (cons 1 2) @result{} (1 . 2)
  307. (cons 1 nil) @result{} (1)
  308. (cons nil 2) @result{} (NIL . 2)
  309. (cons nil nil) @result{} (NIL)
  310. (cons 1 (cons 2 (cons 3 (cons 4 nil)))) @result{} (1 2 3 4)
  311. (cons 'a 'b) @result{} (A . B)
  312. (cons 'a (cons 'b (cons 'c '@t{()}))) @result{} (A B C)
  313. (cons 'a '(b c d)) @result{} (A B C D)
  314. @end example
  315. @subsubheading See Also::
  316. @ref{list}
  317. @subsubheading Notes::
  318. If @i{object-2} is a @i{list}, @b{cons} can be thought of as
  319. producing a new @i{list} which is like it but has @i{object-1} prepended.
  320. @node consp, atom, cons, Conses Dictionary
  321. @subsection consp [Function]
  322. @code{consp} @i{object} @result{} @i{generalized-boolean}
  323. @subsubheading Arguments and Values::
  324. @i{object}---an @i{object}.
  325. @i{generalized-boolean}---a @i{generalized boolean}.
  326. @subsubheading Description::
  327. Returns @i{true} if @i{object} is of @i{type} @b{cons};
  328. otherwise, returns @i{false}.
  329. @subsubheading Examples::
  330. @example
  331. (consp nil) @result{} @i{false}
  332. (consp (cons 1 2)) @result{} @i{true}
  333. @end example
  334. The @i{empty list} is not a @i{cons}, so
  335. @example
  336. (consp '()) @equiv{} (consp 'nil) @result{} @i{false}
  337. @end example
  338. @subsubheading See Also::
  339. @ref{listp}
  340. @subsubheading Notes::
  341. @example
  342. (consp @i{object}) @equiv{} (typep @i{object} 'cons) @equiv{} (not (typep @i{object} 'atom)) @equiv{} (typep @i{object} '(not atom))
  343. @end example
  344. @node atom, rplaca, consp, Conses Dictionary
  345. @subsection atom [Function]
  346. @code{atom} @i{object} @result{} @i{generalized-boolean}
  347. @subsubheading Arguments and Values::
  348. @i{object}---an @i{object}.
  349. @i{generalized-boolean}---a @i{generalized boolean}.
  350. @subsubheading Description::
  351. Returns @i{true} if @i{object} is of @i{type} @b{atom};
  352. otherwise, returns @i{false}.
  353. @subsubheading Examples::
  354. @example
  355. (atom 'sss) @result{} @i{true}
  356. (atom (cons 1 2)) @result{} @i{false}
  357. (atom nil) @result{} @i{true}
  358. (atom '()) @result{} @i{true}
  359. (atom 3) @result{} @i{true}
  360. @end example
  361. @subsubheading Notes::
  362. @example
  363. (atom @i{object}) @equiv{} (typep @i{object} 'atom) @equiv{} (not (consp @i{object}))
  364. @equiv{} (not (typep @i{object} 'cons)) @equiv{} (typep @i{object} '(not cons))
  365. @end example
  366. @node rplaca, car, atom, Conses Dictionary
  367. @subsection rplaca, rplacd [Function]
  368. @code{rplaca} @i{cons object} @result{} @i{cons}
  369. @code{rplacd} @i{cons object} @result{} @i{cons}
  370. @subsubheading Pronunciation::
  371. @b{rplaca}: pronounced ,r\=e 'plak e
  372. or pronounced ,re 'plak e
  373. @b{rplacd}: pronounced ,r\=e 'plak de
  374. or pronounced ,re 'plak de
  375. or pronounced ,r\=e 'plak d\=e
  376. or pronounced ,re 'plak d\=e
  377. @subsubheading Arguments and Values::
  378. @i{cons}---a @i{cons}.
  379. @i{object}---an @i{object}.
  380. @subsubheading Description::
  381. @b{rplaca} replaces the @i{car} of the @i{cons} with @i{object}.
  382. @b{rplacd} replaces the @i{cdr} of the @i{cons} with @i{object}.
  383. @subsubheading Examples::
  384. @example
  385. (defparameter *some-list* (list* 'one 'two 'three 'four)) @result{} *some-list*
  386. *some-list* @result{} (ONE TWO THREE . FOUR)
  387. (rplaca *some-list* 'uno) @result{} (UNO TWO THREE . FOUR)
  388. *some-list* @result{} (UNO TWO THREE . FOUR)
  389. (rplacd (last *some-list*) (list 'IV)) @result{} (THREE IV)
  390. *some-list* @result{} (UNO TWO THREE IV)
  391. @end example
  392. @subsubheading Side Effects::
  393. The @i{cons} is modified.
  394. Should signal an error of @i{type} @b{type-error}
  395. if @i{cons} is not a @i{cons}.
  396. @node car, copy-tree, rplaca, Conses Dictionary
  397. @subsection car, cdr,
  398. @subheading caar, cadr, cdar, cddr,
  399. @subheading caaar, caadr, cadar, caddr, cdaar, cdadr, cddar, cdddr,
  400. @subheading caaaar, caaadr, caadar, caaddr, cadaar, cadadr, caddar, cadddr,
  401. @subheading cdaaar, cdaadr, cdadar, cdaddr, cddaar, cddadr, cdddar, cddddr
  402. @flushright
  403. @i{[Accessor]}
  404. @end flushright
  405. @code{car} @i{x} @result{} @i{object}
  406. (setf (@code{car} @i{x}) new-object)@*
  407. @code{cdr} @i{x} @result{} @i{object}
  408. (setf (@code{cdr} @i{x}) new-object)@*
  409. @code{\vksip 5pt} @i{x} @result{} @i{object}
  410. (setf (@code{\vksip 5pt} @i{x}) new-object)@*
  411. @code{caar} @i{x} @result{} @i{object}
  412. (setf (@code{caar} @i{x}) new-object)@*
  413. @code{cadr} @i{x} @result{} @i{object}
  414. (setf (@code{cadr} @i{x}) new-object)@*
  415. @code{cdar} @i{x} @result{} @i{object}
  416. (setf (@code{cdar} @i{x}) new-object)@*
  417. @code{cddr} @i{x} @result{} @i{object}
  418. (setf (@code{cddr} @i{x}) new-object)@*
  419. @code{\vksip 5pt} @i{x} @result{} @i{object}
  420. (setf (@code{\vksip 5pt} @i{x}) new-object)@*
  421. @code{caaar} @i{x} @result{} @i{object}
  422. (setf (@code{caaar} @i{x}) new-object)@*
  423. @code{caadr} @i{x} @result{} @i{object}
  424. (setf (@code{caadr} @i{x}) new-object)@*
  425. @code{cadar} @i{x} @result{} @i{object}
  426. (setf (@code{cadar} @i{x}) new-object)@*
  427. @code{caddr} @i{x} @result{} @i{object}
  428. (setf (@code{caddr} @i{x}) new-object)@*
  429. @code{cdaar} @i{x} @result{} @i{object}
  430. (setf (@code{cdaar} @i{x}) new-object)@*
  431. @code{cdadr} @i{x} @result{} @i{object}
  432. (setf (@code{cdadr} @i{x}) new-object)@*
  433. @code{cddar} @i{x} @result{} @i{object}
  434. (setf (@code{cddar} @i{x}) new-object)@*
  435. @code{cdddr} @i{x} @result{} @i{object}
  436. (setf (@code{cdddr} @i{x}) new-object)@*
  437. @code{\vksip 5pt} @i{x} @result{} @i{object}
  438. (setf (@code{\vksip 5pt} @i{x}) new-object)@*
  439. @code{caaaar} @i{x} @result{} @i{object}
  440. (setf (@code{caaaar} @i{x}) new-object)@*
  441. @code{caaadr} @i{x} @result{} @i{object}
  442. (setf (@code{caaadr} @i{x}) new-object)@*
  443. @code{caadar} @i{x} @result{} @i{object}
  444. (setf (@code{caadar} @i{x}) new-object)@*
  445. @code{caaddr} @i{x} @result{} @i{object}
  446. (setf (@code{caaddr} @i{x}) new-object)@*
  447. @code{cadaar} @i{x} @result{} @i{object}
  448. (setf (@code{cadaar} @i{x}) new-object)@*
  449. @code{cadadr} @i{x} @result{} @i{object}
  450. (setf (@code{cadadr} @i{x}) new-object)@*
  451. @code{caddar} @i{x} @result{} @i{object}
  452. (setf (@code{caddar} @i{x}) new-object)@*
  453. @code{cadddr} @i{x} @result{} @i{object}
  454. (setf (@code{cadddr} @i{x}) new-object)@*
  455. @code{cdaaar} @i{x} @result{} @i{object}
  456. (setf (@code{cdaaar} @i{x}) new-object)@*
  457. @code{cdaadr} @i{x} @result{} @i{object}
  458. (setf (@code{cdaadr} @i{x}) new-object)@*
  459. @code{cdadar} @i{x} @result{} @i{object}
  460. (setf (@code{cdadar} @i{x}) new-object)@*
  461. @code{cdaddr} @i{x} @result{} @i{object}
  462. (setf (@code{cdaddr} @i{x}) new-object)@*
  463. @code{cddaar} @i{x} @result{} @i{object}
  464. (setf (@code{cddaar} @i{x}) new-object)@*
  465. @code{cddadr} @i{x} @result{} @i{object}
  466. (setf (@code{cddadr} @i{x}) new-object)@*
  467. @code{cdddar} @i{x} @result{} @i{object}
  468. (setf (@code{cdddar} @i{x}) new-object)@*
  469. @code{cddddr} @i{x} @result{} @i{object}
  470. (setf (@code{cddddr} @i{x}) new-object)@*
  471. @subsubheading Pronunciation::
  472. @b{cadr}: pronounced 'ka ,de r
  473. @b{caddr}: pronounced 'kad e ,de r
  474. or pronounced 'ka ,dude r
  475. @b{cdr}: pronounced 'ku ,de r
  476. @b{cddr}: pronounced 'kud e ,de r
  477. or pronounced 'ke ,dude r
  478. @subsubheading Arguments and Values::
  479. @i{x}---a @i{list}.
  480. @i{object}---an @i{object}.
  481. @i{new-object}---an @i{object}.
  482. @subsubheading Description::
  483. If @i{x} is a @i{cons}, @b{car} returns the @i{car}
  484. of that @i{cons}. If @i{x} is @b{nil}, @b{car} returns @b{nil}.
  485. If @i{x} is a @i{cons}, @b{cdr} returns the @i{cdr}
  486. of that @i{cons}. If @i{x} is @b{nil}, @b{cdr} returns @b{nil}.
  487. @i{Functions} are provided which perform compositions of up to four
  488. @b{car} and @b{cdr} operations. Their @i{names} consist of
  489. a @t{C}, followed by two, three, or four occurrences of @t{A} or @t{D},
  490. and finally an @t{R}. The series of @t{A}'s and @t{D}'s in each
  491. @i{function}'s @i{name} is chosen to identify the series of
  492. @b{car} and @b{cdr} operations that is performed by the function.
  493. The order in which the @t{A}'s and @t{D}'s appear is the inverse of the
  494. order in which the corresponding operations are performed. Figure 14--6
  495. defines the relationships precisely.
  496. @group
  497. @noindent
  498. @w{ This @i{place} ... Is equivalent to this @i{place} ... }
  499. @w{ @t{(caar @i{x})} @t{(car (car @i{x}))} }
  500. @w{ @t{(cadr @i{x})} @t{(car (cdr @i{x}))} }
  501. @w{ @t{(cdar @i{x})} @t{(cdr (car @i{x}))} }
  502. @w{ @t{(cddr @i{x})} @t{(cdr (cdr @i{x}))} }
  503. @w{ @t{(caaar @i{x})} @t{(car (car (car @i{x})))} }
  504. @w{ @t{(caadr @i{x})} @t{(car (car (cdr @i{x})))} }
  505. @w{ @t{(cadar @i{x})} @t{(car (cdr (car @i{x})))} }
  506. @w{ @t{(caddr @i{x})} @t{(car (cdr (cdr @i{x})))} }
  507. @w{ @t{(cdaar @i{x})} @t{(cdr (car (car @i{x})))} }
  508. @w{ @t{(cdadr @i{x})} @t{(cdr (car (cdr @i{x})))} }
  509. @w{ @t{(cddar @i{x})} @t{(cdr (cdr (car @i{x})))} }
  510. @w{ @t{(cdddr @i{x})} @t{(cdr (cdr (cdr @i{x})))} }
  511. @w{ @t{(caaaar @i{x})} @t{(car (car (car (car @i{x}))))} }
  512. @w{ @t{(caaadr @i{x})} @t{(car (car (car (cdr @i{x}))))} }
  513. @w{ @t{(caadar @i{x})} @t{(car (car (cdr (car @i{x}))))} }
  514. @w{ @t{(caaddr @i{x})} @t{(car (car (cdr (cdr @i{x}))))} }
  515. @w{ @t{(cadaar @i{x})} @t{(car (cdr (car (car @i{x}))))} }
  516. @w{ @t{(cadadr @i{x})} @t{(car (cdr (car (cdr @i{x}))))} }
  517. @w{ @t{(caddar @i{x})} @t{(car (cdr (cdr (car @i{x}))))} }
  518. @w{ @t{(cadddr @i{x})} @t{(car (cdr (cdr (cdr @i{x}))))} }
  519. @w{ @t{(cdaaar @i{x})} @t{(cdr (car (car (car @i{x}))))} }
  520. @w{ @t{(cdaadr @i{x})} @t{(cdr (car (car (cdr @i{x}))))} }
  521. @w{ @t{(cdadar @i{x})} @t{(cdr (car (cdr (car @i{x}))))} }
  522. @w{ @t{(cdaddr @i{x})} @t{(cdr (car (cdr (cdr @i{x}))))} }
  523. @w{ @t{(cddaar @i{x})} @t{(cdr (cdr (car (car @i{x}))))} }
  524. @w{ @t{(cddadr @i{x})} @t{(cdr (cdr (car (cdr @i{x}))))} }
  525. @w{ @t{(cdddar @i{x})} @t{(cdr (cdr (cdr (car @i{x}))))} }
  526. @w{ @t{(cddddr @i{x})} @t{(cdr (cdr (cdr (cdr @i{x}))))} }
  527. @noindent
  528. @w{ Figure 14--6: CAR and CDR variants }
  529. @end group
  530. @b{setf} can also be used with any of these functions to change an
  531. existing component of @i{x}, but @b{setf} will not make new
  532. components. So, for example, the @i{car} of a @i{cons}
  533. can be assigned with @b{setf} of @b{car},
  534. but the @i{car} of @b{nil} cannot be assigned with @b{setf} of @b{car}.
  535. Similarly, the @i{car} of the @i{car} of a @i{cons} whose @i{car}
  536. is a @i{cons} can be assigned with @b{setf} of @b{caar},
  537. but neither @b{nil} nor a @i{cons} whose car is @b{nil} can be assigned
  538. with @b{setf} of @b{caar}.
  539. The argument @i{x} is permitted to be a @i{dotted list}
  540. or a @i{circular list}.
  541. @subsubheading Examples::
  542. @example
  543. (car nil) @result{} NIL
  544. (cdr '(1 . 2)) @result{} 2
  545. (cdr '(1 2)) @result{} (2)
  546. (cadr '(1 2)) @result{} 2
  547. (car '(a b c)) @result{} A
  548. (cdr '(a b c)) @result{} (B C)
  549. @end example
  550. @subsubheading Exceptional Situations::
  551. The functions @b{car} and @b{cdr}
  552. should signal @b{type-error} if they receive an argument which is not a
  553. @i{list}. The other functions (@b{caar}, @b{cadr},
  554. ... @b{cddddr}) should behave for the purpose of
  555. error checking as if defined by appropriate calls to @b{car} and
  556. @b{cdr}.
  557. @subsubheading See Also::
  558. @ref{rplaca; rplacd}
  559. ,
  560. @ref{first; second; third; fourth; fifth; sixth; seventh; eighth; ninth; tenth}
  561. ,
  562. @ref{rest}
  563. @subsubheading Notes::
  564. The @i{car} of a @i{cons} can also be altered by using @b{rplaca},
  565. and the @i{cdr} of a @i{cons} can be altered by using @b{rplacd}.
  566. @example
  567. (car @i{x}) @equiv{} (first @i{x})
  568. (cadr @i{x}) @equiv{} (second @i{x}) @equiv{} (car (cdr @i{x}))
  569. (caddr @i{x}) @equiv{} (third @i{x}) @equiv{} (car (cdr (cdr @i{x})))
  570. (cadddr @i{x}) @equiv{} (fourth @i{x}) @equiv{} (car (cdr (cdr (cdr @i{x}))))
  571. @end example
  572. @node copy-tree, sublis, car, Conses Dictionary
  573. @subsection copy-tree [Function]
  574. @code{copy-tree} @i{tree} @result{} @i{new-tree}
  575. @subsubheading Arguments and Values::
  576. @i{tree}---a @i{tree}.
  577. @i{new-tree}---a @i{tree}.
  578. @subsubheading Description::
  579. Creates a @i{copy} of a @i{tree} of @i{conses}.
  580. If @i{tree} is not a @i{cons}, it is returned;
  581. otherwise, the result is a new @i{cons} of the results of calling @b{copy-tree}
  582. on the @i{car} and @i{cdr} of @i{tree}.
  583. In other words, all @i{conses} in the @i{tree} represented by @i{tree}
  584. are copied recursively, stopping only when non-@i{conses} are encountered.
  585. @b{copy-tree} does not preserve circularities and the sharing of substructure.
  586. @subsubheading Examples::
  587. @example
  588. (setq object (list (cons 1 "one")
  589. (cons 2 (list 'a 'b 'c))))
  590. @result{} ((1 . "one") (2 A B C))
  591. (setq object-too object) @result{} ((1 . "one") (2 A B C))
  592. (setq copy-as-list (copy-list object))
  593. (setq copy-as-alist (copy-alist object))
  594. (setq copy-as-tree (copy-tree object))
  595. (eq object object-too) @result{} @i{true}
  596. (eq copy-as-tree object) @result{} @i{false}
  597. (eql copy-as-tree object) @result{} @i{false}
  598. (equal copy-as-tree object) @result{} @i{true}
  599. (setf (first (cdr (second object))) "a"
  600. (car (second object)) "two"
  601. (car object) '(one . 1)) @result{} (ONE . 1)
  602. object @result{} ((ONE . 1) ("two" "a" B C))
  603. object-too @result{} ((ONE . 1) ("two" "a" B C))
  604. copy-as-list @result{} ((1 . "one") ("two" "a" B C))
  605. copy-as-alist @result{} ((1 . "one") (2 "a" B C))
  606. copy-as-tree @result{} ((1 . "one") (2 A B C))
  607. @end example
  608. @subsubheading See Also::
  609. @ref{tree-equal}
  610. @node sublis, subst, copy-tree, Conses Dictionary
  611. @subsection sublis, nsublis [Function]
  612. @code{sublis} @i{alist tree {&key} key test test-not} @result{} @i{new-tree}
  613. @code{nsublis} @i{alist tree {&key} key test test-not} @result{} @i{new-tree}
  614. @subsubheading Arguments and Values::
  615. @i{alist}---an @i{association list}.
  616. @i{tree}---a @i{tree}.
  617. @i{test}---a @i{designator} for a @i{function} of two @i{arguments}
  618. that returns a @i{generalized boolean}.
  619. @i{test-not}---a @i{designator} for
  620. a @i{function} of two @i{arguments}
  621. that returns a @i{generalized boolean}.
  622. @i{key}---a @i{designator} for a @i{function} of one argument,
  623. or @b{nil}.
  624. @i{new-tree}---a @i{tree}.
  625. @subsubheading Description::
  626. @b{sublis} makes substitutions for @i{objects} in @i{tree}
  627. (a structure of @i{conses}).
  628. @b{nsublis} is like @b{sublis}
  629. but destructively modifies the relevant
  630. parts of the @i{tree}.
  631. @b{sublis} looks at all subtrees and leaves of @i{tree};
  632. if a subtree or leaf appears as a key in @i{alist}
  633. (that is, the key and the subtree or leaf @i{satisfy the test}),
  634. it is replaced by the @i{object} with which that key is associated.
  635. This operation is non-destructive. In effect, @b{sublis} can
  636. perform several @b{subst} operations simultaneously.
  637. If @b{sublis} succeeds, a new copy of @i{tree} is returned in
  638. which each occurrence of such a subtree or leaf is replaced by the
  639. @i{object} with which it is associated. If no changes are made, the
  640. original tree is returned. The original @i{tree} is left unchanged,
  641. but the result tree may share cells with it.
  642. @b{nsublis} is permitted to modify @i{tree}
  643. but otherwise returns the same values as @b{sublis}.
  644. @subsubheading Examples::
  645. @example
  646. (sublis '((x . 100) (z . zprime))
  647. '(plus x (minus g z x p) 4 . x))
  648. @result{} (PLUS 100 (MINUS G ZPRIME 100 P) 4 . 100)
  649. (sublis '(((+ x y) . (- x y)) ((- x y) . (+ x y)))
  650. '(* (/ (+ x y) (+ x p)) (- x y))
  651. :test #'equal)
  652. @result{} (* (/ (- X Y) (+ X P)) (+ X Y))
  653. (setq tree1 '(1 (1 2) ((1 2 3)) (((1 2 3 4)))))
  654. @result{} (1 (1 2) ((1 2 3)) (((1 2 3 4))))
  655. (sublis '((3 . "three")) tree1)
  656. @result{} (1 (1 2) ((1 2 "three")) (((1 2 "three" 4))))
  657. (sublis '((t . "string"))
  658. (sublis '((1 . "") (4 . 44)) tree1)
  659. :key #'stringp)
  660. @result{} ("string" ("string" 2) (("string" 2 3)) ((("string" 2 3 44))))
  661. tree1 @result{} (1 (1 2) ((1 2 3)) (((1 2 3 4))))
  662. (setq tree2 '("one" ("one" "two") (("one" "Two" "three"))))
  663. @result{} ("one" ("one" "two") (("one" "Two" "three")))
  664. (sublis '(("two" . 2)) tree2)
  665. @result{} ("one" ("one" "two") (("one" "Two" "three")))
  666. tree2 @result{} ("one" ("one" "two") (("one" "Two" "three")))
  667. (sublis '(("two" . 2)) tree2 :test 'equal)
  668. @result{} ("one" ("one" 2) (("one" "Two" "three")))
  669. (nsublis '((t . 'temp))
  670. tree1
  671. :key #'(lambda (x) (or (atom x) (< (list-length x) 3))))
  672. @result{} ((QUOTE TEMP) (QUOTE TEMP) QUOTE TEMP)
  673. @end example
  674. @subsubheading Side Effects::
  675. @b{nsublis} modifies @i{tree}.
  676. @subsubheading See Also::
  677. @ref{subst; subst-if; subst-if-not; nsubst; nsubst-if; nsubst-if-not}
  678. ,
  679. @ref{Compiler Terminology},
  680. @ref{Traversal Rules and Side Effects}
  681. @subsubheading Notes::
  682. The @t{:test-not} parameter is deprecated.
  683. Because the side-effecting variants (@i{e.g.}, @b{nsublis}) potentially
  684. change the path that is being traversed, their effects in the presence
  685. of shared or circular structure structure may vary in surprising ways
  686. when compared to their non-side-effecting alternatives. To see this,
  687. consider the following side-effect behavior, which might be exhibited by
  688. some implementations:
  689. @example
  690. (defun test-it (fn)
  691. (let* ((shared-piece (list 'a 'b))
  692. (data (list shared-piece shared-piece)))
  693. (funcall fn '((a . b) (b . a)) data)))
  694. (test-it #'sublis) @result{} ((B A) (B A))
  695. (test-it #'nsublis) @result{} ((A B) (A B))
  696. @end example
  697. @node subst, tree-equal, sublis, Conses Dictionary
  698. @subsection subst, subst-if, subst-if-not, nsubst, nsubst-if, nsubst-if-not
  699. @flushright
  700. @i{[Function]}
  701. @end flushright
  702. @code{subst} @i{new old tree {&key} key test test-not} @result{} @i{new-tree}
  703. @code{subst-if} @i{new predicate tree {&key} key} @result{} @i{new-tree}
  704. @code{subst-if-not} @i{new predicate tree {&key} key} @result{} @i{new-tree}
  705. @code{nsubst} @i{new old tree {&key} key test test-not} @result{} @i{new-tree}
  706. @code{nsubst-if} @i{new predicate tree {&key} key} @result{} @i{new-tree}
  707. @code{nsubst-if-not} @i{new predicate tree {&key} key} @result{} @i{new-tree}
  708. @subsubheading Arguments and Values::
  709. @i{new}---an @i{object}.
  710. @i{old}---an @i{object}.
  711. @i{predicate}---a @i{symbol} that names a @i{function},
  712. or a @i{function} of one argument
  713. that returns a @i{generalized boolean} value.
  714. @i{tree}---a @i{tree}.
  715. @i{test}---a @i{designator} for a @i{function} of two @i{arguments}
  716. that returns a @i{generalized boolean}.
  717. @i{test-not}---a @i{designator} for
  718. a @i{function} of two @i{arguments}
  719. that returns a @i{generalized boolean}.
  720. @i{key}---a @i{designator} for a @i{function} of one argument,
  721. or @b{nil}.
  722. @i{new-tree}---a @i{tree}.
  723. @subsubheading Description::
  724. @b{subst}, @b{subst-if}, and @b{subst-if-not} perform
  725. substitution operations on @i{tree}.
  726. Each function searches @i{tree} for occurrences of a
  727. particular @i{old} item of an element or subexpression that
  728. @i{satisfies the test}.
  729. @b{nsubst}, @b{nsubst-if}, and @b{nsubst-if-not} are
  730. like @b{subst},
  731. @b{subst-if}, and @b{subst-if-not} respectively, except that the
  732. original @i{tree} is modified.
  733. @b{subst} makes a copy of @i{tree},
  734. substituting @i{new} for every subtree or leaf of @i{tree}
  735. (whether the subtree or leaf is a @i{car} or a @i{cdr} of its parent)
  736. such that @i{old} and the subtree or leaf @i{satisfy the test}.
  737. @b{nsubst} is a destructive version of @b{subst}.
  738. The list structure of
  739. @i{tree} is altered by destructively replacing with @i{new}
  740. each leaf of the @i{tree} such that @i{old} and the leaf
  741. @i{satisfy the test}.
  742. For @b{subst}, @b{subst-if},
  743. and @b{subst-if-not},
  744. if the functions succeed, a new
  745. copy of the tree is returned in which each occurrence of such an
  746. element is replaced by the
  747. @i{new} element or subexpression. If no changes are made, the original
  748. @i{tree} may be returned.
  749. The original @i{tree} is left unchanged, but the result tree
  750. may share storage with it.
  751. For @b{nsubst}, @b{nsubst-if},
  752. and @b{nsubst-if-not}
  753. the original @i{tree} is modified and returned as the function result,
  754. but the result may not be @b{eq} to @i{tree}.
  755. @subsubheading Examples::
  756. @example
  757. (setq tree1 '(1 (1 2) (1 2 3) (1 2 3 4))) @result{} (1 (1 2) (1 2 3) (1 2 3 4))
  758. (subst "two" 2 tree1) @result{} (1 (1 "two") (1 "two" 3) (1 "two" 3 4))
  759. (subst "five" 5 tree1) @result{} (1 (1 2) (1 2 3) (1 2 3 4))
  760. (eq tree1 (subst "five" 5 tree1)) @result{} @i{implementation-dependent}
  761. (subst 'tempest 'hurricane
  762. '(shakespeare wrote (the hurricane)))
  763. @result{} (SHAKESPEARE WROTE (THE TEMPEST))
  764. (subst 'foo 'nil '(shakespeare wrote (twelfth night)))
  765. @result{} (SHAKESPEARE WROTE (TWELFTH NIGHT . FOO) . FOO)
  766. (subst '(a . cons) '(old . pair)
  767. '((old . spice) ((old . shoes) old . pair) (old . pair))
  768. :test #'equal)
  769. @result{} ((OLD . SPICE) ((OLD . SHOES) A . CONS) (A . CONS))
  770. (subst-if 5 #'listp tree1) @result{} 5
  771. (subst-if-not '(x) #'consp tree1)
  772. @result{} (1 X)
  773. tree1 @result{} (1 (1 2) (1 2 3) (1 2 3 4))
  774. (nsubst 'x 3 tree1 :key #'(lambda (y) (and (listp y) (third y))))
  775. @result{} (1 (1 2) X X)
  776. tree1 @result{} (1 (1 2) X X)
  777. @end example
  778. @subsubheading Side Effects::
  779. @b{nsubst}, @b{nsubst-if}, and @b{nsubst-if-not}
  780. might alter the @i{tree structure} of @i{tree}.
  781. @subsubheading See Also::
  782. @ref{substitute; substitute-if; substitute-if-not; nsubstitute; nsubstitute-if; nsubstitute-if-not}
  783. ,
  784. @b{nsubstitute},
  785. @ref{Compiler Terminology},
  786. @ref{Traversal Rules and Side Effects}
  787. @subsubheading Notes::
  788. The @t{:test-not} parameter is deprecated.
  789. The functions @b{subst-if-not} and @b{nsubst-if-not} are deprecated.
  790. One possible definition of @b{subst}:
  791. @example
  792. (defun subst (old new tree &rest x &key test test-not key)
  793. (cond ((satisfies-the-test old tree :test test
  794. :test-not test-not :key key)
  795. new)
  796. ((atom tree) tree)
  797. (t (let ((a (apply #'subst old new (car tree) x))
  798. (d (apply #'subst old new (cdr tree) x)))
  799. (if (and (eql a (car tree))
  800. (eql d (cdr tree)))
  801. tree
  802. (cons a d))))))
  803. @end example
  804. @node tree-equal, copy-list, subst, Conses Dictionary
  805. @subsection tree-equal [Function]
  806. @code{tree-equal} @i{tree-1 tree-2 {&key} test test-not} @result{} @i{generalized-boolean}
  807. @subsubheading Arguments and Values::
  808. @i{tree-1}---a @i{tree}.
  809. @i{tree-2}---a @i{tree}.
  810. @i{test}---a @i{designator} for a @i{function} of two @i{arguments}
  811. that returns a @i{generalized boolean}.
  812. @i{test-not}---a @i{designator} for
  813. a @i{function} of two @i{arguments}
  814. that returns a @i{generalized boolean}.
  815. @i{generalized-boolean}---a @i{generalized boolean}.
  816. @subsubheading Description::
  817. @b{tree-equal} tests whether two trees are of the same shape
  818. and have the same leaves.
  819. @b{tree-equal} returns @i{true} if @i{tree-1} and @i{tree-2} are
  820. both @i{atoms} and @i{satisfy the test},
  821. or if they are both @i{conses} and
  822. the @i{car} of @i{tree-1} is @b{tree-equal} to
  823. the @i{car} of @i{tree-2} and
  824. the @i{cdr} of @i{tree-1} is @b{tree-equal} to
  825. the @i{cdr} of @i{tree-2}.
  826. Otherwise, @b{tree-equal} returns @i{false}.
  827. @b{tree-equal} recursively compares @i{conses} but not any
  828. other @i{objects} that have components.
  829. The first argument to the @t{:test} or @t{:test-not}
  830. function is @i{tree-1} or a @i{car} or @i{cdr} of @i{tree-1};
  831. the second argument is @i{tree-2} or a @i{car}
  832. or @i{cdr} of @i{tree-2}.
  833. @subsubheading Examples::
  834. @example
  835. (setq tree1 '(1 (1 2))
  836. tree2 '(1 (1 2))) @result{} (1 (1 2))
  837. (tree-equal tree1 tree2) @result{} @i{true}
  838. (eql tree1 tree2) @result{} @i{false}
  839. (setq tree1 '('a ('b 'c))
  840. tree2 '('a ('b 'c))) @result{} ('a ('b 'c))
  841. @result{} ((QUOTE A) ((QUOTE B) (QUOTE C)))
  842. (tree-equal tree1 tree2 :test 'eq) @result{} @i{true}
  843. @end example
  844. @subsubheading Exceptional Situations::
  845. The consequences are undefined
  846. if both @i{tree-1} and @i{tree-2} are circular.
  847. @subsubheading See Also::
  848. @ref{equal}
  849. ,
  850. @ref{Traversal Rules and Side Effects}
  851. @subsubheading Notes::
  852. The @t{:test-not} parameter is deprecated.
  853. @node copy-list, list, tree-equal, Conses Dictionary
  854. @subsection copy-list [Function]
  855. @code{copy-list} @i{list} @result{} @i{copy}
  856. @subsubheading Arguments and Values::
  857. @i{list}---a @i{proper list} or a @i{dotted list}.
  858. @i{copy}---a @i{list}.
  859. @subsubheading Description::
  860. Returns a @i{copy} of @i{list}.
  861. If @i{list} is a @i{dotted list},
  862. the resulting @i{list} will also be a @i{dotted list}.
  863. Only the @i{list structure} of @i{list} is copied;
  864. the @i{elements} of the resulting list are
  865. the @i{same} as the corresponding @i{elements} of the given @i{list}.
  866. @subsubheading Examples::
  867. @example
  868. (setq lst (list 1 (list 2 3))) @result{} (1 (2 3))
  869. (setq slst lst) @result{} (1 (2 3))
  870. (setq clst (copy-list lst)) @result{} (1 (2 3))
  871. (eq slst lst) @result{} @i{true}
  872. (eq clst lst) @result{} @i{false}
  873. (equal clst lst) @result{} @i{true}
  874. (rplaca lst "one") @result{} ("one" (2 3))
  875. slst @result{} ("one" (2 3))
  876. clst @result{} (1 (2 3))
  877. (setf (caadr lst) "two") @result{} "two"
  878. lst @result{} ("one" ("two" 3))
  879. slst @result{} ("one" ("two" 3))
  880. clst @result{} (1 ("two" 3))
  881. @end example
  882. @subsubheading Exceptional Situations::
  883. The consequences are undefined if @i{list} is a @i{circular list}.
  884. @subsubheading See Also::
  885. @ref{copy-alist}
  886. ,
  887. @ref{copy-seq}
  888. ,
  889. @ref{copy-tree}
  890. @subsubheading Notes::
  891. The copy created is @b{equal} to @i{list}, but not @b{eq}.
  892. @node list, list-length, copy-list, Conses Dictionary
  893. @subsection list, list* [Function]
  894. @code{list} @i{{&rest} objects} @result{} @i{list}
  895. @code{list*} @i{{&rest} objects^+} @result{} @i{result}
  896. @subsubheading Arguments and Values::
  897. @i{object}---an @i{object}.
  898. @i{list}---a @i{list}.
  899. @i{result}---an @i{object}.
  900. @subsubheading Description::
  901. @b{list} returns a @i{list} containing the supplied @i{objects}.
  902. @b{list*} is like @b{list} except that
  903. the last @i{argument} to @b{list} becomes
  904. the @i{car} of the last @i{cons} constructed, while
  905. the last @i{argument} to @b{list*} becomes
  906. the @i{cdr} of the last @i{cons} constructed.
  907. Hence, any given call to @b{list*} always produces one fewer @i{conses}
  908. than a call to @b{list} with the same number of arguments.
  909. If the last @i{argument} to @b{list*} is a @i{list},
  910. the effect is to construct a new @i{list} which is similar, but
  911. which has additional elements added to the front corresponding to
  912. the preceding @i{arguments} of @b{list*}.
  913. If @b{list*} receives only one @i{object},
  914. that @i{object} is returned, regardless of whether or not it is a @i{list}.
  915. @subsubheading Examples::
  916. @example
  917. (list 1) @result{} (1)
  918. (list* 1) @result{} 1
  919. (setq a 1) @result{} 1
  920. (list a 2) @result{} (1 2)
  921. '(a 2) @result{} (A 2)
  922. (list 'a 2) @result{} (A 2)
  923. (list* a 2) @result{} (1 . 2)
  924. (list) @result{} NIL ;@i{i.e.}, ()
  925. (setq a '(1 2)) @result{} (1 2)
  926. (eq a (list* a)) @result{} @i{true}
  927. (list 3 4 'a (car '(b . c)) (+ 6 -2)) @result{} (3 4 A B 4)
  928. (list* 'a 'b 'c 'd) @equiv{} (cons 'a (cons 'b (cons 'c 'd))) @result{} (A B C . D)
  929. (list* 'a 'b 'c '(d e f)) @result{} (A B C D E F)
  930. @end example
  931. @subsubheading See Also::
  932. @ref{cons}
  933. @subsubheading Notes::
  934. @example
  935. (list* @i{x}) @equiv{} @i{x}
  936. @end example
  937. @node list-length, listp, list, Conses Dictionary
  938. @subsection list-length [Function]
  939. @code{list-length} @i{list} @result{} @i{length}
  940. @subsubheading Arguments and Values::
  941. @i{list}---a @i{proper list} or a @i{circular list}.
  942. @i{length}---a non-negative @i{integer}, or @b{nil}.
  943. @subsubheading Description::
  944. Returns the @i{length} of @i{list} if @i{list} is a @i{proper list}.
  945. Returns @b{nil} if @i{list} is a @i{circular list}.
  946. @subsubheading Examples::
  947. @example
  948. (list-length '(a b c d)) @result{} 4
  949. (list-length '(a (b c) d)) @result{} 3
  950. (list-length '()) @result{} 0
  951. (list-length nil) @result{} 0
  952. (defun circular-list (&rest elements)
  953. (let ((cycle (copy-list elements)))
  954. (nconc cycle cycle)))
  955. (list-length (circular-list 'a 'b)) @result{} NIL
  956. (list-length (circular-list 'a)) @result{} NIL
  957. (list-length (circular-list)) @result{} 0
  958. @end example
  959. @subsubheading Exceptional Situations::
  960. Should signal an error of @i{type} @b{type-error}
  961. if @i{list} is not a @i{proper list} or a @i{circular list}.
  962. @subsubheading See Also::
  963. @ref{length}
  964. @subsubheading Notes::
  965. @b{list-length} could be implemented as follows:
  966. @example
  967. (defun list-length (x)
  968. (do ((n 0 (+ n 2)) ;Counter.
  969. (fast x (cddr fast)) ;Fast pointer: leaps by 2.
  970. (slow x (cdr slow))) ;Slow pointer: leaps by 1.
  971. (nil)
  972. ;; If fast pointer hits the end, return the count.
  973. (when (endp fast) (return n))
  974. (when (endp (cdr fast)) (return (+ n 1)))
  975. ;; If fast pointer eventually equals slow pointer,
  976. ;; then we must be stuck in a circular list.
  977. ;; (A deeper property is the converse: if we are
  978. ;; stuck in a circular list, then eventually the
  979. ;; fast pointer will equal the slow pointer.
  980. ;; That fact justifies this implementation.)
  981. (when (and (eq fast slow) (> n 0)) (return nil))))
  982. @end example
  983. @node listp, make-list, list-length, Conses Dictionary
  984. @subsection listp [Function]
  985. @code{listp} @i{object} @result{} @i{generalized-boolean}
  986. @subsubheading Arguments and Values::
  987. @i{object}---an @i{object}.
  988. @i{generalized-boolean}---a @i{generalized boolean}.
  989. @subsubheading Description::
  990. Returns @i{true} if @i{object} is of @i{type} @b{list};
  991. otherwise, returns @i{false}.
  992. @subsubheading Examples::
  993. @example
  994. (listp nil) @result{} @i{true}
  995. (listp (cons 1 2)) @result{} @i{true}
  996. (listp (make-array 6)) @result{} @i{false}
  997. (listp t) @result{} @i{false}
  998. @end example
  999. @subsubheading See Also::
  1000. @ref{consp}
  1001. @subsubheading Notes::
  1002. If @i{object} is a @i{cons},
  1003. @b{listp} does not check whether @i{object} is a @i{proper list};
  1004. it returns @i{true} for any kind of @i{list}.
  1005. @example
  1006. (listp @i{object}) @equiv{} (typep @i{object} 'list) @equiv{} (typep @i{object} '(or cons null))
  1007. @end example
  1008. @node make-list, push, listp, Conses Dictionary
  1009. @subsection make-list [Function]
  1010. @code{make-list} @i{size {&key} initial-element} @result{} @i{list}
  1011. @subsubheading Arguments and Values::
  1012. @i{size}---a non-negative @i{integer}.
  1013. @i{initial-element}---an @i{object}.
  1014. The default is @b{nil}.
  1015. @i{list}---a @i{list}.
  1016. @subsubheading Description::
  1017. Returns a @i{list} of @i{length} given by @i{size},
  1018. each of the @i{elements} of which is @i{initial-element}.
  1019. @subsubheading Examples::
  1020. @example
  1021. (make-list 5) @result{} (NIL NIL NIL NIL NIL)
  1022. (make-list 3 :initial-element 'rah) @result{} (RAH RAH RAH)
  1023. (make-list 2 :initial-element '(1 2 3)) @result{} ((1 2 3) (1 2 3))
  1024. (make-list 0) @result{} NIL ;@i{i.e.}, ()
  1025. (make-list 0 :initial-element 'new-element) @result{} NIL
  1026. @end example
  1027. @subsubheading Exceptional Situations::
  1028. Should signal an error of @i{type} @b{type-error}
  1029. if @i{size} is not a non-negative @i{integer}.
  1030. @subsubheading See Also::
  1031. @ref{cons}
  1032. ,
  1033. @ref{list}
  1034. @node push, pop, make-list, Conses Dictionary
  1035. @subsection push [Macro]
  1036. @code{push} @i{item place} @result{} @i{new-place-value}
  1037. @subsubheading Arguments and Values::
  1038. @i{item}---an @i{object}.
  1039. @i{place}---a @i{place}, the @i{value} of which may be any @i{object}.
  1040. @i{new-place-value}---a @i{list} (the new @i{value} of @i{place}).
  1041. @subsubheading Description::
  1042. @b{push} prepends @i{item} to the @i{list} that is stored
  1043. in @i{place}, stores the resulting @i{list} in @i{place},
  1044. and returns the @i{list}.
  1045. For information about the @i{evaluation} of @i{subforms} of @i{place},
  1046. see @ref{Evaluation of Subforms to Places}.
  1047. @subsubheading Examples::
  1048. @example
  1049. (setq llst '(nil)) @result{} (NIL)
  1050. (push 1 (car llst)) @result{} (1)
  1051. llst @result{} ((1))
  1052. (push 1 (car llst)) @result{} (1 1)
  1053. llst @result{} ((1 1))
  1054. (setq x '(a (b c) d)) @result{} (A (B C) D)
  1055. (push 5 (cadr x)) @result{} (5 B C)
  1056. x @result{} (A (5 B C) D)
  1057. @end example
  1058. @subsubheading Side Effects::
  1059. The contents of @i{place} are modified.
  1060. @subsubheading See Also::
  1061. @ref{pop}
  1062. ,
  1063. @ref{pushnew}
  1064. ,
  1065. @ref{Generalized Reference}
  1066. @subsubheading Notes::
  1067. The effect of @t{(push @i{item} @i{place})}
  1068. is equivalent to
  1069. @example
  1070. (setf place (cons @i{item} @i{place}))
  1071. @end example
  1072. except that the @i{subforms} of @i{place}
  1073. are evaluated only once, and @i{item} is evaluated
  1074. before @i{place}.
  1075. @node pop, first, push, Conses Dictionary
  1076. @subsection pop [Macro]
  1077. @code{pop} @i{place} @result{} @i{element}
  1078. @subsubheading Arguments and Values::
  1079. @i{place}---a @i{place}, the @i{value} of which is a @i{list}
  1080. (possibly, but necessarily, a @i{dotted list} or @i{circular list}).
  1081. @i{element}---an @i{object} (the @i{car} of the contents of @i{place}).
  1082. @subsubheading Description::
  1083. @b{pop} @i{reads} the @i{value} of @i{place},
  1084. remembers the @i{car} of the @i{list} which was retrieved,
  1085. @i{writes} the @i{cdr} of the @i{list} back into the @i{place},
  1086. and finally @i{yields} the @i{car} of the originally retrieved @i{list}.
  1087. For information about the @i{evaluation} of @i{subforms} of @i{place},
  1088. see @ref{Evaluation of Subforms to Places}.
  1089. @subsubheading Examples::
  1090. @example
  1091. (setq stack '(a b c)) @result{} (A B C)
  1092. (pop stack) @result{} A
  1093. stack @result{} (B C)
  1094. (setq llst '((1 2 3 4))) @result{} ((1 2 3 4))
  1095. (pop (car llst)) @result{} 1
  1096. llst @result{} ((2 3 4))
  1097. @end example
  1098. @subsubheading Side Effects::
  1099. The contents of @i{place} are modified.
  1100. @subsubheading See Also::
  1101. @ref{push}
  1102. ,
  1103. @ref{pushnew}
  1104. ,
  1105. @ref{Generalized Reference}
  1106. @subsubheading Notes::
  1107. The effect of @t{(pop @i{place})} is roughly equivalent to
  1108. @example
  1109. (prog1 (car @i{place}) (setf @i{place} (cdr @i{place})))
  1110. @end example
  1111. except that the latter would evaluate any @i{subforms} of @i{place}
  1112. three times, while @b{pop} evaluates them only once.
  1113. @node first, nth, pop, Conses Dictionary
  1114. @subsection first, second, third, fourth, fifth,
  1115. @subheading sixth, seventh, eighth, ninth, tenth
  1116. @flushright
  1117. @i{[Accessor]}
  1118. @end flushright
  1119. @code{first} @i{list} @result{} @i{object}
  1120. (setf (@code{first} @i{list}) new-object)@*
  1121. @code{second} @i{list} @result{} @i{object}
  1122. (setf (@code{second} @i{list}) new-object)@*
  1123. @code{third} @i{list} @result{} @i{object}
  1124. (setf (@code{third} @i{list}) new-object)@*
  1125. @code{fourth} @i{list} @result{} @i{object}
  1126. (setf (@code{fourth} @i{list}) new-object)@*
  1127. @code{fifth} @i{list} @result{} @i{object}
  1128. (setf (@code{fifth} @i{list}) new-object)@*
  1129. @code{sixth} @i{list} @result{} @i{object}
  1130. (setf (@code{sixth} @i{list}) new-object)@*
  1131. @code{seventh} @i{list} @result{} @i{object}
  1132. (setf (@code{seventh} @i{list}) new-object)@*
  1133. @code{eighth} @i{list} @result{} @i{object}
  1134. (setf (@code{eighth} @i{list}) new-object)@*
  1135. @code{ninth} @i{list} @result{} @i{object}
  1136. (setf (@code{ninth} @i{list}) new-object)@*
  1137. @code{tenth} @i{list} @result{} @i{object}
  1138. (setf (@code{tenth} @i{list}) new-object)@*
  1139. @subsubheading Arguments and Values::
  1140. @i{list}---a @i{list},
  1141. which might be a @i{dotted list} or a @i{circular list}.
  1142. @i{object}, @i{new-object}---an @i{object}.
  1143. @subsubheading Description::
  1144. The functions
  1145. @b{first},
  1146. @b{second},
  1147. @b{third},
  1148. @b{fourth},
  1149. @b{fifth},
  1150. @b{sixth},
  1151. @b{seventh},
  1152. @b{eighth},
  1153. @b{ninth},
  1154. and
  1155. @b{tenth}
  1156. @i{access} the first, second, third, fourth, fifth, sixth, seventh, eighth,
  1157. ninth, and tenth @i{elements} of @i{list}, respectively.
  1158. Specifically,
  1159. @example
  1160. (first @i{list}) @equiv{} (car @i{list})
  1161. (second @i{list}) @equiv{} (car (cdr @i{list}))
  1162. (third @i{list}) @equiv{} (car (cddr @i{list}))
  1163. (fourth @i{list}) @equiv{} (car (cdddr @i{list}))
  1164. (fifth @i{list}) @equiv{} (car (cddddr @i{list}))
  1165. (sixth @i{list}) @equiv{} (car (cdr (cddddr @i{list})))
  1166. (seventh @i{list}) @equiv{} (car (cddr (cddddr @i{list})))
  1167. (eighth @i{list}) @equiv{} (car (cdddr (cddddr @i{list})))
  1168. (ninth @i{list}) @equiv{} (car (cddddr (cddddr @i{list})))
  1169. (tenth @i{list}) @equiv{} (car (cdr (cddddr (cddddr @i{list}))))
  1170. @end example
  1171. @b{setf} can also be used with any of these functions to change an
  1172. existing component. The same equivalences apply. For example:
  1173. @example
  1174. (setf (fifth @i{list}) @i{new-object}) @equiv{} (setf (car (cddddr @i{list})) @i{new-object})
  1175. @end example
  1176. @subsubheading Examples::
  1177. @example
  1178. (setq lst '(1 2 3 (4 5 6) ((V)) vi 7 8 9 10))
  1179. @result{} (1 2 3 (4 5 6) ((V)) VI 7 8 9 10)
  1180. (first lst) @result{} 1
  1181. (tenth lst) @result{} 10
  1182. (fifth lst) @result{} ((V))
  1183. (second (fourth lst)) @result{} 5
  1184. (sixth '(1 2 3)) @result{} NIL
  1185. (setf (fourth lst) "four") @result{} "four"
  1186. lst @result{} (1 2 3 "four" ((V)) VI 7 8 9 10)
  1187. @end example
  1188. @subsubheading See Also::
  1189. @ref{car; cdr; caar; cadr; cdar; cddr; caaar; caadr; cadar; caddr; cdaar; cdadr; cddar; cdddr; caaaar; caaadr; caadar; caaddr; cadaar; cadadr; caddar; cadddr; cdaaar; cdaadr; cdadar; cdaddr; cddaar; cddadr; cdddar; cddddr}
  1190. ,
  1191. @ref{nth}
  1192. @subsubheading Notes::
  1193. @b{first} is functionally equivalent to @b{car},
  1194. @b{second} is functionally equivalent to @b{cadr},
  1195. @b{third} is functionally equivalent to @b{caddr}, and
  1196. @b{fourth} is functionally equivalent to @b{cadddr}.
  1197. The ordinal numbering used here is one-origin,
  1198. as opposed to the zero-origin numbering used by @b{nth}:
  1199. @example
  1200. (fifth x) @equiv{} (nth 4 x)
  1201. @end example
  1202. @node nth, endp, first, Conses Dictionary
  1203. @subsection nth [Accessor]
  1204. @code{nth} @i{n list} @result{} @i{object}
  1205. (setf (@code{ nth} @i{n list}) new-object)@*
  1206. @subsubheading Arguments and Values::
  1207. @i{n}---a non-negative @i{integer}.
  1208. @i{list}---a @i{list},
  1209. which might be a @i{dotted list} or a @i{circular list}.
  1210. @i{object}---an @i{object}.
  1211. @i{new-object}---an @i{object}.
  1212. @subsubheading Description::
  1213. @b{nth} locates the @i{n}th element of @i{list},
  1214. where the @i{car} of the @i{list} is the ``zeroth'' element.
  1215. Specifically,
  1216. @example
  1217. (nth @i{n} @i{list}) @equiv{} (car (nthcdr @i{n} @i{list}))
  1218. @end example
  1219. @b{nth} may be used to specify a @i{place} to @b{setf}.
  1220. Specifically,
  1221. @example
  1222. (setf (nth @i{n} @i{list}) @i{new-object}) @equiv{} (setf (car (nthcdr @i{n} @i{list})) @i{new-object})
  1223. @end example
  1224. @subsubheading Examples::
  1225. @example
  1226. (nth 0 '(foo bar baz)) @result{} FOO
  1227. (nth 1 '(foo bar baz)) @result{} BAR
  1228. (nth 3 '(foo bar baz)) @result{} NIL
  1229. (setq 0-to-3 (list 0 1 2 3)) @result{} (0 1 2 3)
  1230. (setf (nth 2 0-to-3) "two") @result{} "two"
  1231. 0-to-3 @result{} (0 1 "two" 3)
  1232. @end example
  1233. @subsubheading See Also::
  1234. @ref{elt}
  1235. ,
  1236. @ref{first; second; third; fourth; fifth; sixth; seventh; eighth; ninth; tenth}
  1237. ,
  1238. @ref{nthcdr}
  1239. @node endp, null, nth, Conses Dictionary
  1240. @subsection endp [Function]
  1241. @code{endp} @i{list} @result{} @i{generalized-boolean}
  1242. @subsubheading Arguments and Values::
  1243. @i{list}---a @i{list},
  1244. which might be a @i{dotted list} or a @i{circular list}.
  1245. @i{generalized-boolean}---a @i{generalized boolean}.
  1246. @subsubheading Description::
  1247. Returns @i{true} if @i{list} is the @i{empty list}.
  1248. Returns @i{false} if @i{list} is a @i{cons}.
  1249. @subsubheading Examples::
  1250. @example
  1251. (endp nil) @result{} @i{true}
  1252. (endp '(1 2)) @result{} @i{false}
  1253. (endp (cddr '(1 2))) @result{} @i{true}
  1254. @end example
  1255. @subsubheading Exceptional Situations::
  1256. Should signal an error of @i{type} @b{type-error}
  1257. if @i{list} is not a @i{list}.
  1258. @subsubheading Notes::
  1259. The purpose of @b{endp} is to test for the end of @i{proper list}.
  1260. Since @b{endp} does not descend into a @i{cons},
  1261. it is well-defined to pass it a @i{dotted list}.
  1262. However, if shorter ``lists'' are iteratively produced
  1263. by calling @b{cdr} on such a @i{dotted list}
  1264. and those ``lists'' are tested with @b{endp},
  1265. a situation that has undefined consequences will eventually result
  1266. when the @i{non-nil} @i{atom} (which is not in fact a @i{list})
  1267. finally becomes the argument to @b{endp}.
  1268. Since this is the usual way in which @b{endp} is used,
  1269. it is conservative programming style
  1270. and consistent with the intent of @b{endp}
  1271. to treat @b{endp} as simply a function on @i{proper lists}
  1272. which happens not to enforce an argument type of @i{proper list} except
  1273. when the argument is @i{atomic}.
  1274. @node null, nconc, endp, Conses Dictionary
  1275. @subsection null [Function]
  1276. @code{null} @i{object} @result{} @i{boolean}
  1277. @subsubheading Arguments and Values::
  1278. @i{object}---an @i{object}.
  1279. @i{boolean}---a @i{boolean}.
  1280. @subsubheading Description::
  1281. Returns @b{t} if @i{object} is the @i{empty list};
  1282. otherwise, returns @b{nil}.
  1283. @subsubheading Examples::
  1284. @example
  1285. (null '()) @result{} T
  1286. (null nil) @result{} T
  1287. (null t) @result{} NIL
  1288. (null 1) @result{} NIL
  1289. @end example
  1290. @subsubheading See Also::
  1291. @ref{not}
  1292. @subsubheading Notes::
  1293. @b{null} is intended to be used to test for the @i{empty list}
  1294. whereas @b{not} is intended to be used to invert a @i{boolean}
  1295. (or @i{generalized boolean}).
  1296. Operationally, @b{null} and @b{not} compute the same result;
  1297. which to use is a matter of style.
  1298. @example
  1299. (null @i{object}) @equiv{} (typep @i{object} 'null) @equiv{} (eq @i{object} '@t{()})
  1300. @end example
  1301. @node nconc, append, null, Conses Dictionary
  1302. @subsection nconc [Function]
  1303. @code{nconc} @i{{&rest} lists} @result{} @i{concatenated-list}
  1304. @subsubheading Arguments and Values::
  1305. @i{list}---each but the last must be a @i{list}
  1306. (which might be a @i{dotted list} but must not be a @i{circular list});
  1307. the last @i{list} may be any @i{object}.
  1308. @i{concatenated-list}---a @i{list}.
  1309. @subsubheading Description::
  1310. Returns a @i{list} that is the concatenation of @i{lists}.
  1311. If no @i{lists} are supplied, @t{(nconc)} returns @b{nil}.
  1312. @b{nconc} is defined using the following recursive relationship:
  1313. @example
  1314. (nconc) @result{} ()
  1315. (nconc nil . @i{lists}) @equiv{} (nconc . @i{lists})
  1316. (nconc @i{list}) @result{} @i{list}
  1317. (nconc @i{list-1} @i{list-2}) @equiv{} (progn (rplacd (last @i{list-1}) @i{list-2}) @i{list-1})
  1318. (nconc @i{list-1} @i{list-2} . @i{lists}) @equiv{} (nconc (nconc @i{list-1} @i{list-2}) . @i{lists})
  1319. @end example
  1320. @subsubheading Examples::
  1321. @example
  1322. (nconc) @result{} NIL
  1323. (setq x '(a b c)) @result{} (A B C)
  1324. (setq y '(d e f)) @result{} (D E F)
  1325. (nconc x y) @result{} (A B C D E F)
  1326. x @result{} (A B C D E F)
  1327. @end example
  1328. Note, in the example, that the value of @t{x} is now different,
  1329. since its last @i{cons}
  1330. has been @b{rplacd}'d to the value of @t{y}.
  1331. If @t{(nconc x y)} were evaluated again,
  1332. it would yield a piece of a @i{circular list},
  1333. whose printed representation would be
  1334. @t{(A B C D E F D E F D E F ...)}, repeating forever;
  1335. if the @b{*print-circle*} switch were @i{non-nil},
  1336. it would be printed as @t{(A B C . #1=(D E F . #1#))}.
  1337. @example
  1338. (setq foo (list 'a 'b 'c 'd 'e)
  1339. bar (list 'f 'g 'h 'i 'j)
  1340. baz (list 'k 'l 'm)) @result{} (K L M)
  1341. (setq foo (nconc foo bar baz)) @result{} (A B C D E F G H I J K L M)
  1342. foo @result{} (A B C D E F G H I J K L M)
  1343. bar @result{} (F G H I J K L M)
  1344. baz @result{} (K L M)
  1345. (setq foo (list 'a 'b 'c 'd 'e)
  1346. bar (list 'f 'g 'h 'i 'j)
  1347. baz (list 'k 'l 'm)) @result{} (K L M)
  1348. (setq foo (nconc nil foo bar nil baz)) @result{} (A B C D E F G H I J K L M)
  1349. foo @result{} (A B C D E F G H I J K L M)
  1350. bar @result{} (F G H I J K L M)
  1351. baz @result{} (K L M)
  1352. @end example
  1353. @subsubheading Side Effects::
  1354. The @i{lists} are modified rather than copied.
  1355. @subsubheading See Also::
  1356. @ref{append}
  1357. ,
  1358. @ref{concatenate}
  1359. @node append, revappend, nconc, Conses Dictionary
  1360. @subsection append [Function]
  1361. @code{append} @i{{&rest} lists} @result{} @i{result}
  1362. @subsubheading Arguments and Values::
  1363. @i{list}---each must be a @i{proper list} except the last,
  1364. which may be any @i{object}.
  1365. @i{result}---an @i{object}. This will be a @i{list}
  1366. unless the last @i{list} was not a @i{list}
  1367. and all preceding @i{lists} were @i{null}.
  1368. @subsubheading Description::
  1369. @b{append} returns a new @i{list} that is the concatenation of
  1370. the copies. @i{lists} are left unchanged; the @i{list structure}
  1371. of each of @i{lists} except the last is copied.
  1372. The last argument is not copied; it becomes the @i{cdr} of the
  1373. final @i{dotted pair} of the concatenation of the preceding @i{lists},
  1374. or is returned directly if there are no preceding
  1375. @i{non-empty}
  1376. @i{lists}.
  1377. @subsubheading Examples::
  1378. @example
  1379. (append '(a b c) '(d e f) '() '(g)) @result{} (A B C D E F G)
  1380. (append '(a b c) 'd) @result{} (A B C . D)
  1381. (setq lst '(a b c)) @result{} (A B C)
  1382. (append lst '(d)) @result{} (A B C D)
  1383. lst @result{} (A B C)
  1384. (append) @result{} NIL
  1385. (append 'a) @result{} A
  1386. @end example
  1387. @subsubheading See Also::
  1388. @ref{nconc}
  1389. ,
  1390. @ref{concatenate}
  1391. @node revappend, butlast, append, Conses Dictionary
  1392. @subsection revappend, nreconc [Function]
  1393. @code{revappend} @i{list tail} @result{} @i{result-list}
  1394. @code{nreconc} @i{list tail} @result{} @i{result-list}
  1395. @subsubheading Arguments and Values::
  1396. @i{list}---a @i{proper list}.
  1397. @i{tail}---an @i{object}.
  1398. @i{result-list}---an @i{object}.
  1399. @subsubheading Description::
  1400. @b{revappend} constructs a @i{copy}_2 of @i{list},
  1401. but with the @i{elements} in reverse order. It then appends (as if
  1402. by @b{nconc}) the @i{tail} to that reversed list and returns the result.
  1403. @b{nreconc} reverses the order of @i{elements} in @i{list}
  1404. (as if by @b{nreverse}). It then appends (as if by @b{nconc})
  1405. the @i{tail} to that reversed list and returns the result.
  1406. The resulting @i{list} shares @i{list structure} with @i{tail}.
  1407. @subsubheading Examples::
  1408. @example
  1409. (let ((list-1 (list 1 2 3))
  1410. (list-2 (list 'a 'b 'c)))
  1411. (print (revappend list-1 list-2))
  1412. (print (equal list-1 '(1 2 3)))
  1413. (print (equal list-2 '(a b c))))
  1414. @t{ |> } (3 2 1 A B C)
  1415. @t{ |> } T
  1416. @t{ |> } T
  1417. @result{} T
  1418. (revappend '(1 2 3) '()) @result{} (3 2 1)
  1419. (revappend '(1 2 3) '(a . b)) @result{} (3 2 1 A . B)
  1420. (revappend '() '(a b c)) @result{} (A B C)
  1421. (revappend '(1 2 3) 'a) @result{} (3 2 1 . A)
  1422. (revappend '() 'a) @result{} A ;degenerate case
  1423. (let ((list-1 '(1 2 3))
  1424. (list-2 '(a b c)))
  1425. (print (nreconc list-1 list-2))
  1426. (print (equal list-1 '(1 2 3)))
  1427. (print (equal list-2 '(a b c))))
  1428. @t{ |> } (3 2 1 A B C)
  1429. @t{ |> } NIL
  1430. @t{ |> } T
  1431. @result{} T
  1432. @end example
  1433. @subsubheading Side Effects::
  1434. @b{revappend} does not modify either of its @i{arguments}.
  1435. @b{nreconc} is permitted to modify @i{list} but not @i{tail}.
  1436. Although it might be implemented differently,
  1437. @b{nreconc} is constrained to have side-effect behavior equivalent to:
  1438. @example
  1439. (nconc (nreverse @i{list}) @i{tail})
  1440. @end example
  1441. @subsubheading See Also::
  1442. @ref{reverse; nreverse}
  1443. ,
  1444. @b{nreverse},
  1445. @ref{nconc}
  1446. @subsubheading Notes::
  1447. The following functional equivalences are true,
  1448. although good @i{implementations} will typically use a faster algorithm for
  1449. achieving the same effect:
  1450. @example
  1451. (revappend @i{list} @i{tail}) @equiv{} (nconc (reverse @i{list}) @i{tail})
  1452. (nreconc @i{list} @i{tail}) @equiv{} (nconc (nreverse @i{list}) @i{tail})
  1453. @end example
  1454. @node butlast, last, revappend, Conses Dictionary
  1455. @subsection butlast, nbutlast [Function]
  1456. @code{butlast} @i{list {&optional} n} @result{} @i{result-list}
  1457. @code{nbutlast} @i{list {&optional} n} @result{} @i{result-list}
  1458. @subsubheading Arguments and Values::
  1459. @i{list}---a @i{list},
  1460. which might be a @i{dotted list} but must not be a @i{circular list}.
  1461. @i{n}---a non-negative @i{integer}.
  1462. @i{result-list}---a @i{list}.
  1463. @subsubheading Description::
  1464. @b{butlast} returns a copy of @i{list} from which the last
  1465. @i{n}
  1466. conses
  1467. have been omitted.
  1468. If @i{n} is not supplied, its value is 1.
  1469. If there are fewer than @i{n}
  1470. conses
  1471. in @i{list},
  1472. @b{nil} is returned and, in the case of @b{nbutlast},
  1473. @i{list} is not modified.
  1474. @b{nbutlast} is like @b{butlast}, but @b{nbutlast}
  1475. may modify @i{list}.
  1476. It changes the @i{cdr} of
  1477. the @i{cons} @i{n}+1 from the end of the @i{list} to @b{nil}.
  1478. @subsubheading Examples::
  1479. @example
  1480. (setq lst '(1 2 3 4 5 6 7 8 9)) @result{} (1 2 3 4 5 6 7 8 9)
  1481. (butlast lst) @result{} (1 2 3 4 5 6 7 8)
  1482. (butlast lst 5) @result{} (1 2 3 4)
  1483. (butlast lst (+ 5 5)) @result{} NIL
  1484. lst @result{} (1 2 3 4 5 6 7 8 9)
  1485. (nbutlast lst 3) @result{} (1 2 3 4 5 6)
  1486. lst @result{} (1 2 3 4 5 6)
  1487. (nbutlast lst 99) @result{} NIL
  1488. lst @result{} (1 2 3 4 5 6)
  1489. (butlast '(a b c d)) @result{} (A B C)
  1490. (butlast '((a b) (c d))) @result{} ((A B))
  1491. (butlast '(a)) @result{} NIL
  1492. (butlast nil) @result{} NIL
  1493. (setq foo (list 'a 'b 'c 'd)) @result{} (A B C D)
  1494. (nbutlast foo) @result{} (A B C)
  1495. foo @result{} (A B C)
  1496. (nbutlast (list 'a)) @result{} NIL
  1497. (nbutlast '()) @result{} NIL
  1498. @end example
  1499. @subsubheading Exceptional Situations::
  1500. Should signal an error of @i{type} @b{type-error}
  1501. if @i{list} is not a @i{proper list} or a @i{dotted list}.
  1502. Should signal an error of @i{type} @b{type-error}
  1503. if @i{n} is not a non-negative @i{integer}.
  1504. @subsubheading Notes::
  1505. @example
  1506. (butlast @i{list} @i{n}) @equiv{} (ldiff @i{list} (last @i{list} @i{n}))
  1507. @end example
  1508. @node last, ldiff, butlast, Conses Dictionary
  1509. @subsection last [Function]
  1510. @code{last} @i{list {&optional} n} @result{} @i{tail}
  1511. @subsubheading Arguments and Values::
  1512. @i{list}---a @i{list},
  1513. which might be a @i{dotted list} but must not be a @i{circular list}.
  1514. @i{n}---a non-negative @i{integer}.
  1515. The default is @t{1}.
  1516. @i{tail}---an @i{object}.
  1517. @subsubheading Description::
  1518. @b{last} returns the last @i{n} @i{conses}
  1519. (not the last @i{n} elements) of @i{list}).
  1520. If @i{list} is @t{()}, @b{last} returns @t{()}.
  1521. If @i{n} is zero,
  1522. the atom that terminates @i{list} is returned.
  1523. If @i{n} is greater than or equal to the number of @i{cons} cells in @i{list},
  1524. the result is @i{list}.
  1525. @subsubheading Examples::
  1526. @example
  1527. (last nil) @result{} NIL
  1528. (last '(1 2 3)) @result{} (3)
  1529. (last '(1 2 . 3)) @result{} (2 . 3)
  1530. (setq x (list 'a 'b 'c 'd)) @result{} (A B C D)
  1531. (last x) @result{} (D)
  1532. (rplacd (last x) (list 'e 'f)) x @result{} (A B C D E F)
  1533. (last x) @result{} (F)
  1534. (last '(a b c)) @result{} (C)
  1535. (last '(a b c) 0) @result{} ()
  1536. (last '(a b c) 1) @result{} (C)
  1537. (last '(a b c) 2) @result{} (B C)
  1538. (last '(a b c) 3) @result{} (A B C)
  1539. (last '(a b c) 4) @result{} (A B C)
  1540. (last '(a . b) 0) @result{} B
  1541. (last '(a . b) 1) @result{} (A . B)
  1542. (last '(a . b) 2) @result{} (A . B)
  1543. @end example
  1544. @subsubheading Exceptional Situations::
  1545. The consequences are undefined if @i{list} is a @i{circular list}.
  1546. Should signal an error of @i{type} @b{type-error}
  1547. if @i{n} is not a non-negative @i{integer}.
  1548. @subsubheading See Also::
  1549. @ref{butlast; nbutlast}
  1550. ,
  1551. @ref{nth}
  1552. @subsubheading Notes::
  1553. The following code could be used to define @b{last}.
  1554. @example
  1555. (defun last (list &optional (n 1))
  1556. (check-type n (integer 0))
  1557. (do ((l list (cdr l))
  1558. (r list)
  1559. (i 0 (+ i 1)))
  1560. ((atom l) r)
  1561. (if (>= i n) (pop r))))
  1562. @end example
  1563. @node ldiff, nthcdr, last, Conses Dictionary
  1564. @subsection ldiff, tailp [Function]
  1565. @code{ldiff} @i{list object} @result{} @i{result-list}
  1566. @code{tailp} @i{object list} @result{} @i{generalized-boolean}
  1567. @subsubheading Arguments and Values::
  1568. @i{list}---a @i{list},
  1569. which might be a @i{dotted list}.
  1570. @i{object}---an @i{object}.
  1571. @i{result-list}---a @i{list}.
  1572. @i{generalized-boolean}---a @i{generalized boolean}.
  1573. @subsubheading Description::
  1574. If @i{object} is the @i{same} as some @i{tail} of @i{list},
  1575. @b{tailp} returns @i{true};
  1576. otherwise, it returns @i{false}.
  1577. If @i{object} is the @i{same} as some @i{tail} of @i{list},
  1578. @b{ldiff} returns a @i{fresh} @i{list}
  1579. of the @i{elements} of @i{list}
  1580. that precede @b{object} in the @i{list structure} of @i{list};
  1581. otherwise, it returns a @i{copy}_2 of @i{list}.
  1582. @subsubheading Examples::
  1583. @example
  1584. (let ((lists '#((a b c) (a b c . d))))
  1585. (dotimes (i (length lists)) ()
  1586. (let ((list (aref lists i)))
  1587. (format t "~2&list=~S ~21T(tailp object list)~
  1588. ~44T(ldiff list object)~
  1589. (let ((objects (vector list (cddr list) (copy-list (cddr list))
  1590. '(f g h) '() 'd 'x)))
  1591. (dotimes (j (length objects)) ()
  1592. (let ((object (aref objects j)))
  1593. (format t "~& object=~S ~21T~S ~44T~S"
  1594. object (tailp object list) (ldiff list object))))))))
  1595. @t{ |> }
  1596. @t{ |> } list=(A B C) (tailp object list) (ldiff list object)
  1597. @t{ |> } object=(A B C) T NIL
  1598. @t{ |> } object=(C) T (A B)
  1599. @t{ |> } object=(C) NIL (A B C)
  1600. @t{ |> } object=(F G H) NIL (A B C)
  1601. @t{ |> } object=NIL T (A B C)
  1602. @t{ |> } object=D NIL (A B C)
  1603. @t{ |> } object=X NIL (A B C)
  1604. @t{ |> }
  1605. @t{ |> } list=(A B C . D) (tailp object list) (ldiff list object)
  1606. @t{ |> } object=(A B C . D) T NIL
  1607. @t{ |> } object=(C . D) T (A B)
  1608. @t{ |> } object=(C . D) NIL (A B C . D)
  1609. @t{ |> } object=(F G H) NIL (A B C . D)
  1610. @t{ |> } object=NIL NIL (A B C . D)
  1611. @t{ |> } object=D T (A B C)
  1612. @t{ |> } object=X NIL (A B C . D)
  1613. @result{} NIL
  1614. @end example
  1615. @subsubheading Side Effects::
  1616. Neither @b{ldiff} nor @b{tailp} modifies either of its @i{arguments}.
  1617. @subsubheading Exceptional Situations::
  1618. Should be prepared to signal an error of @i{type} @b{type-error}
  1619. if @i{list} is not a @i{proper list} or a @i{dotted list}.
  1620. @subsubheading See Also::
  1621. @ref{set-difference; nset-difference}
  1622. @subsubheading Notes::
  1623. If the @i{list} is a @i{circular list},
  1624. @b{tailp} will reliably @i{yield} a @i{value}
  1625. only if the given @i{object} is in fact a @i{tail} of @i{list}.
  1626. Otherwise, the consequences are unspecified:
  1627. a given @i{implementation} which detects the circularity must return @i{false},
  1628. but since an @i{implementation} is not obliged to detect such a @i{situation},
  1629. @b{tailp} might just loop indefinitely without returning in that case.
  1630. @b{tailp} could be defined as follows:
  1631. @example
  1632. (defun tailp (object list)
  1633. (do ((list list (cdr list)))
  1634. ((atom list) (eql list object))
  1635. (if (eql object list)
  1636. (return t))))
  1637. @end example
  1638. and @b{ldiff} could be defined by:
  1639. @example
  1640. (defun ldiff (list object)
  1641. (do ((list list (cdr list))
  1642. (r '() (cons (car list) r)))
  1643. ((atom list)
  1644. (if (eql list object) (nreverse r) (nreconc r list)))
  1645. (when (eql object list)
  1646. (return (nreverse r)))))
  1647. @end example
  1648. @node nthcdr, rest, ldiff, Conses Dictionary
  1649. @subsection nthcdr [Function]
  1650. @code{nthcdr} @i{n list} @result{} @i{tail}
  1651. @subsubheading Arguments and Values::
  1652. @i{n}---a non-negative @i{integer}.
  1653. @i{list}---a @i{list},
  1654. which might be a @i{dotted list} or a @i{circular list}.
  1655. @i{tail}---an @i{object}.
  1656. @subsubheading Description::
  1657. Returns the @i{tail} of @i{list} that would be obtained by calling @b{cdr}
  1658. @i{n} times in succession.
  1659. @subsubheading Examples::
  1660. @example
  1661. (nthcdr 0 '()) @result{} NIL
  1662. (nthcdr 3 '()) @result{} NIL
  1663. (nthcdr 0 '(a b c)) @result{} (A B C)
  1664. (nthcdr 2 '(a b c)) @result{} (C)
  1665. (nthcdr 4 '(a b c)) @result{} ()
  1666. (nthcdr 1 '(0 . 1)) @result{} 1
  1667. (locally (declare (optimize (safety 3)))
  1668. (nthcdr 3 '(0 . 1)))
  1669. Error: Attempted to take CDR of 1.
  1670. @end example
  1671. @subsubheading Exceptional Situations::
  1672. Should signal an error of @i{type} @b{type-error}
  1673. if @i{n} is not a non-negative @i{integer}.
  1674. For @i{n} being an integer greater than @t{1},
  1675. the error checking done by @t{(nthcdr @i{n} @i{list})}
  1676. is the same as for @t{(nthcdr (- @i{n} 1) (cdr @i{list}))};
  1677. see the @i{function} @b{cdr}.
  1678. @subsubheading See Also::
  1679. @b{cdr},
  1680. @ref{nth}
  1681. ,
  1682. @ref{rest}
  1683. @node rest, member, nthcdr, Conses Dictionary
  1684. @subsection rest [Accessor]
  1685. @code{rest} @i{list} @result{} @i{tail}
  1686. (setf (@code{ rest} @i{list}) new-tail)@*
  1687. @subsubheading Arguments and Values::
  1688. @i{list}---a @i{list},
  1689. which might be a @i{dotted list} or a @i{circular list}.
  1690. @i{tail}---an @i{object}.
  1691. @subsubheading Description::
  1692. @b{rest} performs the same operation as @b{cdr},
  1693. but mnemonically complements @b{first}.
  1694. Specifically,
  1695. @example
  1696. (rest @i{list}) @equiv{} (cdr @i{list})
  1697. (setf (rest @i{list}) @i{new-tail}) @equiv{} (setf (cdr @i{list}) @i{new-tail})
  1698. @end example
  1699. @subsubheading Examples::
  1700. @example
  1701. (rest '(1 2)) @result{} (2)
  1702. (rest '(1 . 2)) @result{} 2
  1703. (rest '(1)) @result{} NIL
  1704. (setq *cons* '(1 . 2)) @result{} (1 . 2)
  1705. (setf (rest *cons*) "two") @result{} "two"
  1706. *cons* @result{} (1 . "two")
  1707. @end example
  1708. @subsubheading See Also::
  1709. @b{cdr},
  1710. @ref{nthcdr}
  1711. @subsubheading Notes::
  1712. @b{rest} is often preferred stylistically over @b{cdr}
  1713. when the argument is to being subjectively viewed as a @i{list}
  1714. rather than as a @i{cons}.
  1715. @node member, mapc, rest, Conses Dictionary
  1716. @subsection member, member-if, member-if-not [Function]
  1717. @code{member} @i{item list {&key} key test test-not} @result{} @i{tail}
  1718. @code{member-if} @i{predicate list {&key} key} @result{} @i{tail}
  1719. @code{member-if-not} @i{predicate list {&key} key} @result{} @i{tail}
  1720. @subsubheading Arguments and Values::
  1721. @i{item}---an @i{object}.
  1722. @i{list}---a @i{proper list}.
  1723. @i{predicate}---a @i{designator} for
  1724. a @i{function} of one @i{argument}
  1725. that returns a @i{generalized boolean}.
  1726. @i{test}---a @i{designator} for a @i{function} of two @i{arguments}
  1727. that returns a @i{generalized boolean}.
  1728. @i{test-not}---a @i{designator} for
  1729. a @i{function} of two @i{arguments}
  1730. that returns a @i{generalized boolean}.
  1731. @i{key}---a @i{designator} for a @i{function} of one argument,
  1732. or @b{nil}.
  1733. @i{tail}---a @i{list}.
  1734. @subsubheading Description::
  1735. @b{member}, @b{member-if}, and @b{member-if-not} each
  1736. search @i{list} for @i{item} or for a top-level element that
  1737. @i{satisfies the test}. The argument to the @i{predicate} function
  1738. is an element of @i{list}.
  1739. If some element @i{satisfies the test},
  1740. the tail of @i{list} beginning
  1741. with this element is returned; otherwise @b{nil} is returned.
  1742. @i{list} is searched on the top level only.
  1743. @subsubheading Examples::
  1744. @example
  1745. (member 2 '(1 2 3)) @result{} (2 3)
  1746. (member 2 '((1 . 2) (3 . 4)) :test-not #'= :key #'cdr) @result{} ((3 . 4))
  1747. (member 'e '(a b c d)) @result{} NIL
  1748. @end example
  1749. @example
  1750. (member-if #'listp '(a b nil c d)) @result{} (NIL C D)
  1751. (member-if #'numberp '(a #\Space 5/3 foo)) @result{} (5/3 FOO)
  1752. (member-if-not #'zerop
  1753. '(3 6 9 11 . 12)
  1754. :key #'(lambda (x) (mod x 3))) @result{} (11 . 12)
  1755. @end example
  1756. @subsubheading Exceptional Situations::
  1757. Should be prepared to signal an error of @i{type} @b{type-error}
  1758. if @i{list} is not a @i{proper list}.
  1759. @subsubheading See Also::
  1760. @ref{find; find-if; find-if-not}
  1761. ,
  1762. @ref{position; position-if; position-if-not}
  1763. ,
  1764. @ref{Traversal Rules and Side Effects}
  1765. @subsubheading Notes::
  1766. The @t{:test-not} parameter is deprecated.
  1767. The @i{function} @b{member-if-not} is deprecated.
  1768. In the following
  1769. @example
  1770. (member 'a '(g (a y) c a d e a f)) @result{} (A D E A F)
  1771. @end example
  1772. the value returned by @b{member} is @i{identical} to the portion
  1773. of the @i{list} beginning with @t{a}. Thus @b{rplaca} on the
  1774. result of @b{member} can be used to alter the part of the @i{list}
  1775. where @t{a} was found (assuming a check has been made that @b{member}
  1776. did not return @b{nil}).
  1777. @node mapc, acons, member, Conses Dictionary
  1778. @subsection mapc, mapcar, mapcan, mapl, maplist, mapcon [Function]
  1779. @code{mapc} @i{function {&rest} lists^+} @result{} @i{list-1}
  1780. @code{mapcar} @i{function {&rest} lists^+} @result{} @i{result-list}
  1781. @code{mapcan} @i{function {&rest} lists^+} @result{} @i{concatenated-results}
  1782. @code{mapl} @i{function {&rest} lists^+} @result{} @i{list-1}
  1783. @code{maplist} @i{function {&rest} lists^+} @result{} @i{result-list}
  1784. @code{mapcon} @i{function {&rest} lists^+} @result{} @i{concatenated-results}
  1785. @subsubheading Arguments and Values::
  1786. @i{function}---a @i{designator} for a @i{function}
  1787. that must take as many @i{arguments} as there are @i{lists}.
  1788. @i{list}---a @i{proper list}.
  1789. @i{list-1}---the first @i{list} (which must be a @i{proper list}).
  1790. @i{result-list}---a @i{list}.
  1791. @i{concatenated-results}---a @i{list}.
  1792. @subsubheading Description::
  1793. The mapping operation involves applying @i{function} to
  1794. successive sets of arguments in which
  1795. one argument is obtained from each @i{sequence}.
  1796. Except for @b{mapc} and @b{mapl},
  1797. the result contains the results returned by @i{function}.
  1798. In the cases of @b{mapc} and @b{mapl},
  1799. the resulting @i{sequence} is @i{list}.
  1800. @i{function} is called
  1801. first on all the elements with index @t{0}, then on all those
  1802. with index @t{1}, and so on.
  1803. @i{result-type} specifies the @i{type} of
  1804. the
  1805. resulting @i{sequence}.
  1806. If @i{function} is a @i{symbol}, it is @b{coerce}d
  1807. to a @i{function} as if by @b{symbol-function}.
  1808. @b{mapcar} operates on successive @i{elements} of the @i{lists}.
  1809. @i{function} is applied to the first @i{element} of each @i{list},
  1810. then to the second @i{element} of each @i{list}, and so on.
  1811. The iteration terminates when the shortest @i{list} runs out,
  1812. and excess elements in other lists are ignored.
  1813. The value returned by @b{mapcar} is a @i{list}
  1814. of the results of successive calls to @i{function}.
  1815. @b{mapc} is like @b{mapcar} except that the results of
  1816. applying @i{function} are not accumulated.
  1817. The @i{list} argument is returned.
  1818. @b{maplist} is like @b{mapcar} except that
  1819. @i{function} is applied to successive sublists of the @i{lists}.
  1820. @i{function}
  1821. is first applied to the @i{lists} themselves,
  1822. and then to the @i{cdr} of each
  1823. @i{list}, and then to the @i{cdr} of the @i{cdr}
  1824. of each @i{list}, and so on.
  1825. @b{mapl} is like @b{maplist} except that the results of
  1826. applying @i{function} are not accumulated;
  1827. @i{list-1} is returned.
  1828. @b{mapcan} and @b{mapcon} are like @b{mapcar} and
  1829. @b{maplist} respectively, except that the results of
  1830. applying @i{function} are combined
  1831. into a @i{list} by the use of @b{nconc}
  1832. rather than @b{list}.
  1833. That is,
  1834. @example
  1835. (mapcon f x1 ... xn)
  1836. @equiv{} (apply #'nconc (maplist f x1 ... xn))
  1837. @end example
  1838. and similarly for the relationship between @b{mapcan}
  1839. and @b{mapcar}.
  1840. @subsubheading Examples::
  1841. @example
  1842. (mapcar #'car '((1 a) (2 b) (3 c))) @result{} (1 2 3)
  1843. (mapcar #'abs '(3 -4 2 -5 -6)) @result{} (3 4 2 5 6)
  1844. (mapcar #'cons '(a b c) '(1 2 3)) @result{} ((A . 1) (B . 2) (C . 3))
  1845. (maplist #'append '(1 2 3 4) '(1 2) '(1 2 3))
  1846. @result{} ((1 2 3 4 1 2 1 2 3) (2 3 4 2 2 3))
  1847. (maplist #'(lambda (x) (cons 'foo x)) '(a b c d))
  1848. @result{} ((FOO A B C D) (FOO B C D) (FOO C D) (FOO D))
  1849. (maplist #'(lambda (x) (if (member (car x) (cdr x)) 0 1)) '(a b a c d b c))
  1850. @result{} (0 0 1 0 1 1 1)
  1851. ;An entry is 1 if the corresponding element of the input
  1852. ; list was the last instance of that element in the input list.
  1853. (setq dummy nil) @result{} NIL
  1854. (mapc #'(lambda (&rest x) (setq dummy (append dummy x)))
  1855. '(1 2 3 4)
  1856. '(a b c d e)
  1857. '(x y z)) @result{} (1 2 3 4)
  1858. dummy @result{} (1 A X 2 B Y 3 C Z)
  1859. (setq dummy nil) @result{} NIL
  1860. (mapl #'(lambda (x) (push x dummy)) '(1 2 3 4)) @result{} (1 2 3 4)
  1861. dummy @result{} ((4) (3 4) (2 3 4) (1 2 3 4))
  1862. (mapcan #'(lambda (x y) (if (null x) nil (list x y)))
  1863. '(nil nil nil d e)
  1864. '(1 2 3 4 5 6)) @result{} (D 4 E 5)
  1865. (mapcan #'(lambda (x) (and (numberp x) (list x)))
  1866. '(a 1 b c 3 4 d 5))
  1867. @result{} (1 3 4 5)
  1868. @end example
  1869. In this case the function serves as a filter;
  1870. this is a standard @r{Lisp} idiom using @b{mapcan}.
  1871. @example
  1872. (mapcon #'list '(1 2 3 4)) @result{} ((1 2 3 4) (2 3 4) (3 4) (4))
  1873. @end example
  1874. @subsubheading Exceptional Situations::
  1875. Should be prepared to signal an error of @i{type} @b{type-error}
  1876. if any @i{list} is not a @i{proper list}.
  1877. @subsubheading See Also::
  1878. @ref{dolist}
  1879. ,
  1880. @ref{map}
  1881. ,
  1882. @ref{Traversal Rules and Side Effects}
  1883. @node acons, assoc, mapc, Conses Dictionary
  1884. @subsection acons [Function]
  1885. @code{acons} @i{key datum alist} @result{} @i{new-alist}
  1886. @subsubheading Arguments and Values::
  1887. @i{key}---an @i{object}.
  1888. @i{datum}---an @i{object}.
  1889. @i{alist}---an @i{association list}.
  1890. @i{new-alist}---an @i{association list}.
  1891. @subsubheading Description::
  1892. Creates a @i{fresh} @i{cons},
  1893. the @i{cdr} of which is @i{alist} and
  1894. the @i{car} of which is another @i{fresh} @i{cons},
  1895. the @i{car} of which is @i{key} and
  1896. the @i{cdr} of which is @i{datum}.
  1897. @subsubheading Examples::
  1898. @example
  1899. (setq alist '()) @result{} NIL
  1900. (acons 1 "one" alist) @result{} ((1 . "one"))
  1901. alist @result{} NIL
  1902. (setq alist (acons 1 "one" (acons 2 "two" alist))) @result{} ((1 . "one") (2 . "two"))
  1903. (assoc 1 alist) @result{} (1 . "one")
  1904. (setq alist (acons 1 "uno" alist)) @result{} ((1 . "uno") (1 . "one") (2 . "two"))
  1905. (assoc 1 alist) @result{} (1 . "uno")
  1906. @end example
  1907. @subsubheading See Also::
  1908. @ref{assoc; assoc-if; assoc-if-not}
  1909. ,
  1910. @ref{pairlis}
  1911. @subsubheading Notes::
  1912. @example
  1913. (acons @i{key} @i{datum} @i{alist}) @equiv{} (cons (cons @i{key} @i{datum}) @i{alist})
  1914. @end example
  1915. @node assoc, copy-alist, acons, Conses Dictionary
  1916. @subsection assoc, assoc-if, assoc-if-not [Function]
  1917. @code{assoc} @i{item alist {&key} key test test-not} @result{} @i{entry}
  1918. @code{assoc-if} @i{predicate alist {&key} key} @result{} @i{entry}
  1919. @code{assoc-if-not} @i{predicate alist {&key} key} @result{} @i{entry}
  1920. @subsubheading Arguments and Values::
  1921. @i{item}---an @i{object}.
  1922. @i{alist}---an @i{association list}.
  1923. @i{predicate}---a @i{designator} for
  1924. a @i{function} of one @i{argument}
  1925. that returns a @i{generalized boolean}.
  1926. @i{test}---a @i{designator} for a @i{function} of two @i{arguments}
  1927. that returns a @i{generalized boolean}.
  1928. @i{test-not}---a @i{designator} for
  1929. a @i{function} of two @i{arguments}
  1930. that returns a @i{generalized boolean}.
  1931. @i{key}---a @i{designator} for a @i{function} of one argument,
  1932. or @b{nil}.
  1933. @i{entry}---a @i{cons} that is an @i{element} of @i{alist},
  1934. or @b{nil}.
  1935. @subsubheading Description::
  1936. @b{assoc}, @b{assoc-if}, and @b{assoc-if-not}
  1937. return the first @i{cons} in @i{alist} whose @i{car} @i{satisfies the test},
  1938. or @b{nil} if no such @i{cons} is found.
  1939. For @b{assoc}, @b{assoc-if}, and @b{assoc-if-not}, if @b{nil} appears
  1940. in @i{alist} in place of a pair, it is ignored.
  1941. @subsubheading Examples::
  1942. @example
  1943. (setq values '((x . 100) (y . 200) (z . 50))) @result{} ((X . 100) (Y . 200) (Z . 50))
  1944. (assoc 'y values) @result{} (Y . 200)
  1945. (rplacd (assoc 'y values) 201) @result{} (Y . 201)
  1946. (assoc 'y values) @result{} (Y . 201)
  1947. (setq alist '((1 . "one")(2 . "two")(3 . "three")))
  1948. @result{} ((1 . "one") (2 . "two") (3 . "three"))
  1949. (assoc 2 alist) @result{} (2 . "two")
  1950. (assoc-if #'evenp alist) @result{} (2 . "two")
  1951. (assoc-if-not #'(lambda(x) (< x 3)) alist) @result{} (3 . "three")
  1952. (setq alist '(("one" . 1)("two" . 2))) @result{} (("one" . 1) ("two" . 2))
  1953. (assoc "one" alist) @result{} NIL
  1954. (assoc "one" alist :test #'equalp) @result{} ("one" . 1)
  1955. (assoc "two" alist :key #'(lambda(x) (char x 2))) @result{} NIL
  1956. (assoc #\o alist :key #'(lambda(x) (char x 2))) @result{} ("two" . 2)
  1957. (assoc 'r '((a . b) (c . d) (r . x) (s . y) (r . z))) @result{} (R . X)
  1958. (assoc 'goo '((foo . bar) (zoo . goo))) @result{} NIL
  1959. (assoc '2 '((1 a b c) (2 b c d) (-7 x y z))) @result{} (2 B C D)
  1960. (setq alist '(("one" . 1) ("2" . 2) ("three" . 3)))
  1961. @result{} (("one" . 1) ("2" . 2) ("three" . 3))
  1962. (assoc-if-not #'alpha-char-p alist
  1963. :key #'(lambda (x) (char x 0))) @result{} ("2" . 2)
  1964. @end example
  1965. @subsubheading Exceptional Situations::
  1966. Should be prepared to signal an error of @i{type} @b{type-error}
  1967. if @i{alist} is not an @i{association list}.
  1968. @subsubheading See Also::
  1969. @ref{rassoc; rassoc-if; rassoc-if-not}
  1970. ,
  1971. @ref{find; find-if; find-if-not}
  1972. ,
  1973. @ref{member; member-if; member-if-not}
  1974. ,
  1975. @ref{position; position-if; position-if-not}
  1976. ,
  1977. @ref{Traversal Rules and Side Effects}
  1978. @subsubheading Notes::
  1979. The @t{:test-not} parameter is deprecated.
  1980. The @i{function} @b{assoc-if-not} is deprecated.
  1981. It is possible to @b{rplacd} the result of @b{assoc}, provided
  1982. that it is not @b{nil},
  1983. in order to ``update'' @i{alist}.
  1984. The two expressions
  1985. @example
  1986. (assoc item list :test fn)
  1987. @end example
  1988. and
  1989. @example
  1990. (find item list :test fn :key #'car)
  1991. @end example
  1992. are equivalent in meaning with one exception:
  1993. if @b{nil} appears in @i{alist} in place of a pair,
  1994. and @i{item} is @b{nil},
  1995. @b{find} will compute the @i{car} of the @b{nil} in @i{alist},
  1996. find that it is equal to @i{item}, and return @b{nil},
  1997. whereas @b{assoc} will ignore the @b{nil} in @i{alist} and continue
  1998. to search for an actual @i{cons} whose @i{car} is @b{nil}.
  1999. @node copy-alist, pairlis, assoc, Conses Dictionary
  2000. @subsection copy-alist [Function]
  2001. @code{copy-alist} @i{alist} @result{} @i{new-alist}
  2002. @subsubheading Arguments and Values::
  2003. @i{alist}---an @i{association list}.
  2004. @i{new-alist}---an @i{association list}.
  2005. @subsubheading Description::
  2006. @b{copy-alist} returns a @i{copy} of @i{alist}.
  2007. The @i{list structure} of @i{alist} is copied,
  2008. and the @i{elements} of @i{alist} which are @i{conses} are
  2009. also copied (as @i{conses} only).
  2010. Any other @i{objects} which are referred to,
  2011. whether directly or indirectly,
  2012. by the @i{alist} continue to be shared.
  2013. @subsubheading Examples::
  2014. @example
  2015. (defparameter *alist* (acons 1 "one" (acons 2 "two" '())))
  2016. *alist* @result{} ((1 . "one") (2 . "two"))
  2017. (defparameter *list-copy* (copy-list *alist*))
  2018. *list-copy* @result{} ((1 . "one") (2 . "two"))
  2019. (defparameter *alist-copy* (copy-alist *alist*))
  2020. *alist-copy* @result{} ((1 . "one") (2 . "two"))
  2021. (setf (cdr (assoc 2 *alist-copy*)) "deux") @result{} "deux"
  2022. *alist-copy* @result{} ((1 . "one") (2 . "deux"))
  2023. *alist* @result{} ((1 . "one") (2 . "two"))
  2024. (setf (cdr (assoc 1 *list-copy*)) "uno") @result{} "uno"
  2025. *list-copy* @result{} ((1 . "uno") (2 . "two"))
  2026. *alist* @result{} ((1 . "uno") (2 . "two"))
  2027. @end example
  2028. @subsubheading See Also::
  2029. @ref{copy-list}
  2030. @node pairlis, rassoc, copy-alist, Conses Dictionary
  2031. @subsection pairlis [Function]
  2032. @code{pairlis} @i{keys data {&optional} alist} @result{} @i{new-alist}
  2033. @subsubheading Arguments and Values::
  2034. @i{keys}---a @i{proper list}.
  2035. @i{data}---a @i{proper list}.
  2036. @i{alist}---an @i{association list}.
  2037. The default is the @i{empty list}.
  2038. @i{new-alist}---an @i{association list}.
  2039. @subsubheading Description::
  2040. Returns an @i{association list} that associates
  2041. elements of @i{keys} to corresponding elements of @i{data}.
  2042. The consequences are undefined if @i{keys} and @i{data} are
  2043. not of the same @i{length}.
  2044. If @i{alist} is supplied, @b{pairlis} returns
  2045. a modified @i{alist} with the
  2046. new pairs prepended to it.
  2047. The new pairs may appear in the resulting @i{association list} in
  2048. either forward or backward order.
  2049. The result of
  2050. @example
  2051. (pairlis '(one two) '(1 2) '((three . 3) (four . 19)))
  2052. @end example
  2053. might be
  2054. @example
  2055. ((one . 1) (two . 2) (three . 3) (four . 19))
  2056. @end example
  2057. or
  2058. @example
  2059. ((two . 2) (one . 1) (three . 3) (four . 19))
  2060. @end example
  2061. @subsubheading Examples::
  2062. @example
  2063. (setq keys '(1 2 3)
  2064. data '("one" "two" "three")
  2065. alist '((4 . "four"))) @result{} ((4 . "four"))
  2066. (pairlis keys data) @result{} ((3 . "three") (2 . "two") (1 . "one"))
  2067. (pairlis keys data alist)
  2068. @result{} ((3 . "three") (2 . "two") (1 . "one") (4 . "four"))
  2069. alist @result{} ((4 . "four"))
  2070. @end example
  2071. @subsubheading Exceptional Situations::
  2072. Should be prepared to signal an error of @i{type} @b{type-error}
  2073. if @i{keys} and @i{data} are not @i{proper lists}.
  2074. @subsubheading See Also::
  2075. @ref{acons}
  2076. @node rassoc, get-properties, pairlis, Conses Dictionary
  2077. @subsection rassoc, rassoc-if, rassoc-if-not [Function]
  2078. @code{rassoc} @i{item alist {&key} key test test-not} @result{} @i{entry}
  2079. @code{rassoc-if} @i{predicate alist {&key} key} @result{} @i{entry}
  2080. @code{rassoc-if-not} @i{predicate alist {&key} key} @result{} @i{entry}
  2081. @subsubheading Arguments and Values::
  2082. @i{item}---an @i{object}.
  2083. @i{alist}---an @i{association list}.
  2084. @i{predicate}---a @i{designator} for
  2085. a @i{function} of one @i{argument}
  2086. that returns a @i{generalized boolean}.
  2087. @i{test}---a @i{designator} for a @i{function} of two @i{arguments}
  2088. that returns a @i{generalized boolean}.
  2089. @i{test-not}---a @i{designator} for
  2090. a @i{function} of two @i{arguments}
  2091. that returns a @i{generalized boolean}.
  2092. @i{key}---a @i{designator} for a @i{function} of one argument,
  2093. or @b{nil}.
  2094. @i{entry}---a @i{cons} that is an @i{element} of the @i{alist},
  2095. or @b{nil}.
  2096. @subsubheading Description::
  2097. @b{rassoc}, @b{rassoc-if}, and @b{rassoc-if-not}
  2098. return the first @i{cons} whose @i{cdr}
  2099. @i{satisfies the test}.
  2100. If no such @i{cons} is found, @b{nil}
  2101. is returned.
  2102. If @b{nil} appears in @i{alist} in place of a pair, it is ignored.
  2103. @subsubheading Examples::
  2104. @example
  2105. (setq alist '((1 . "one") (2 . "two") (3 . 3)))
  2106. @result{} ((1 . "one") (2 . "two") (3 . 3))
  2107. (rassoc 3 alist) @result{} (3 . 3)
  2108. (rassoc "two" alist) @result{} NIL
  2109. (rassoc "two" alist :test 'equal) @result{} (2 . "two")
  2110. (rassoc 1 alist :key #'(lambda (x) (if (numberp x) (/ x 3)))) @result{} (3 . 3)
  2111. (rassoc 'a '((a . b) (b . c) (c . a) (z . a))) @result{} (C . A)
  2112. (rassoc-if #'stringp alist) @result{} (1 . "one")
  2113. (rassoc-if-not #'vectorp alist) @result{} (3 . 3)
  2114. @end example
  2115. @subsubheading See Also::
  2116. @ref{assoc; assoc-if; assoc-if-not}
  2117. ,
  2118. @ref{Traversal Rules and Side Effects}
  2119. @subsubheading Notes::
  2120. The @t{:test-not} parameter is deprecated.
  2121. The @i{function} @b{rassoc-if-not} is deprecated.
  2122. It is possible to @b{rplaca} the result of @b{rassoc},
  2123. provided that it is not @b{nil}, in order to ``update'' @i{alist}.
  2124. The expressions
  2125. @example
  2126. (rassoc item list :test fn)
  2127. @end example
  2128. and
  2129. @example
  2130. (find item list :test fn :key #'cdr)
  2131. @end example
  2132. are equivalent in meaning, except when the @t{item} is @b{nil}
  2133. and @b{nil} appears in place of a pair in the @i{alist}.
  2134. See the @i{function} @b{assoc}.
  2135. @node get-properties, getf, rassoc, Conses Dictionary
  2136. @subsection get-properties [Function]
  2137. @code{get-properties} @i{plist indicator-list} @result{} @i{indicator, value, tail}
  2138. @subsubheading Arguments and Values::
  2139. @i{plist}---a @i{property list}.
  2140. @i{indicator-list}---a @i{proper list} (of @i{indicators}).
  2141. @i{indicator}---an @i{object} that is an @i{element} of @i{indicator-list}.
  2142. @i{value}---an @i{object}.
  2143. @i{tail}---a @i{list}.
  2144. @subsubheading Description::
  2145. @b{get-properties} is used to look up any of several
  2146. @i{property list} entries all at once.
  2147. It searches the @i{plist} for the first entry whose @i{indicator}
  2148. is @i{identical} to one of the @i{objects} in @i{indicator-list}.
  2149. If such an entry is found, the @i{indicator} and @i{value} returned
  2150. are the @i{property indicator} and its associated @i{property value},
  2151. and the @i{tail} returned is the @i{tail} of the @i{plist}
  2152. that begins with the found entry (@i{i.e.}, whose @i{car} is the @i{indicator}).
  2153. If no such entry is found, the @i{indicator}, @i{value}, and @i{tail}
  2154. are all @b{nil}.
  2155. @subsubheading Examples::
  2156. @example
  2157. (setq x '()) @result{} NIL
  2158. (setq *indicator-list* '(prop1 prop2)) @result{} (PROP1 PROP2)
  2159. (getf x 'prop1) @result{} NIL
  2160. (setf (getf x 'prop1) 'val1) @result{} VAL1
  2161. (eq (getf x 'prop1) 'val1) @result{} @i{true}
  2162. (get-properties x *indicator-list*) @result{} PROP1, VAL1, (PROP1 VAL1)
  2163. x @result{} (PROP1 VAL1)
  2164. @end example
  2165. @subsubheading See Also::
  2166. @ref{get}
  2167. ,
  2168. @ref{getf}
  2169. @node getf, remf, get-properties, Conses Dictionary
  2170. @subsection getf [Accessor]
  2171. @code{getf} @i{plist indicator {&optional} default} @result{} @i{value}
  2172. (setf (@code{ getf} @i{place indicator {&optional} default}) new-value)@*
  2173. @subsubheading Arguments and Values::
  2174. @i{plist}---a @i{property list}.
  2175. @i{place}---a @i{place}, the @i{value} of which is a @i{property list}.
  2176. @i{indicator}---an @i{object}.
  2177. @i{default}---an @i{object}.
  2178. The default is @b{nil}.
  2179. @i{value}---an @i{object}.
  2180. @i{new-value}---an @i{object}.
  2181. @subsubheading Description::
  2182. @b{getf} finds a @i{property} on the @i{plist}
  2183. whose @i{property indicator} is @i{identical} to @i{indicator},
  2184. and returns its corresponding @i{property value}.
  2185. If there are multiple @i{properties}_1 with that @i{property indicator},
  2186. @b{getf} uses the first such @i{property}.
  2187. If there is no @i{property} with that @i{property indicator},
  2188. @i{default} is returned.
  2189. @b{setf} of @b{getf} may be used to associate a new @i{object}
  2190. with an existing indicator in the @i{property list} held by @i{place},
  2191. or to create a new assocation if none exists.
  2192. If there are multiple @i{properties}_1 with that @i{property indicator},
  2193. @b{setf} of @b{getf} associates the @i{new-value}
  2194. with the first such @i{property}.
  2195. When a @b{getf} @i{form} is used as a @b{setf} @i{place},
  2196. any @i{default} which is supplied is evaluated according to normal
  2197. left-to-right evaluation rules, but its @i{value} is ignored.
  2198. @b{setf} of @b{getf} is permitted to either
  2199. @i{write} the @i{value} of @i{place} itself,
  2200. or modify of any part, @i{car} or @i{cdr},
  2201. of the @i{list structure} held by @i{place}.
  2202. @subsubheading Examples::
  2203. @example
  2204. (setq x '()) @result{} NIL
  2205. (getf x 'prop1) @result{} NIL
  2206. (getf x 'prop1 7) @result{} 7
  2207. (getf x 'prop1) @result{} NIL
  2208. (setf (getf x 'prop1) 'val1) @result{} VAL1
  2209. (eq (getf x 'prop1) 'val1) @result{} @i{true}
  2210. (getf x 'prop1) @result{} VAL1
  2211. (getf x 'prop1 7) @result{} VAL1
  2212. x @result{} (PROP1 VAL1)
  2213. ;; Examples of implementation variation permitted.
  2214. (setq foo (list 'a 'b 'c 'd 'e 'f)) @result{} (A B C D E F)
  2215. (setq bar (cddr foo)) @result{} (C D E F)
  2216. (remf foo 'c) @result{} @i{true}
  2217. foo @result{} (A B E F)
  2218. bar
  2219. @result{} (C D E F)
  2220. @i{OR}@result{} (C)
  2221. @i{OR}@result{} (NIL)
  2222. @i{OR}@result{} (C NIL)
  2223. @i{OR}@result{} (C D)
  2224. @end example
  2225. @subsubheading See Also::
  2226. @ref{get}
  2227. ,
  2228. @ref{get-properties}
  2229. ,
  2230. @ref{setf; psetf}
  2231. ,
  2232. @ref{Function Call Forms as Places}
  2233. @subsubheading Notes::
  2234. There is no way (using @b{getf}) to distinguish an absent property
  2235. from one whose value is @i{default}; but see @b{get-properties}.
  2236. Note that while supplying a @i{default} argument to @b{getf}
  2237. in a @b{setf} situation is sometimes not very interesting,
  2238. it is still important because some macros, such as @b{push} and
  2239. @b{incf}, require a @i{place} argument which data is both @i{read}
  2240. from and @i{written} to. In such a context, if a @i{default}
  2241. argument is to be supplied for the @i{read} situation, it must be
  2242. syntactically valid for the @i{write} situation as well. For example,
  2243. @example
  2244. (let ((plist '()))
  2245. (incf (getf plist 'count 0))
  2246. plist) @result{} (COUNT 1)
  2247. @end example
  2248. @node remf, intersection, getf, Conses Dictionary
  2249. @subsection remf [Macro]
  2250. @code{remf} @i{place indicator} @result{} @i{generalized-boolean}
  2251. @subsubheading Arguments and Values::
  2252. @i{place}---a @i{place}.
  2253. @i{indicator}---an @i{object}.
  2254. @i{generalized-boolean}---a @i{generalized boolean}.
  2255. @subsubheading Description::
  2256. @b{remf} removes from the @i{property list} stored in @i{place}
  2257. a @i{property}_1 with a @i{property indicator}
  2258. @i{identical} to @i{indicator}.
  2259. If there are multiple @i{properties}_1 with the @i{identical} key,
  2260. @b{remf} only removes the first such @i{property}.
  2261. @b{remf} returns @i{false} if no such @i{property} was found,
  2262. or @i{true} if a property was found.
  2263. The @i{property indicator}
  2264. and the corresponding @i{property value}
  2265. are removed in an undefined order
  2266. by destructively splicing the property list.
  2267. @b{remf} is permitted to either @b{setf} @i{place} or to
  2268. @b{setf} any part, @b{car} or @b{cdr},
  2269. of the @i{list structure} held by that @i{place}.
  2270. For information about the @i{evaluation} of @i{subforms} of @i{place},
  2271. see @ref{Evaluation of Subforms to Places}.
  2272. @subsubheading Examples::
  2273. @example
  2274. (setq x (cons () ())) @result{} (NIL)
  2275. (setf (getf (car x) 'prop1) 'val1) @result{} VAL1
  2276. (remf (car x) 'prop1) @result{} @i{true}
  2277. (remf (car x) 'prop1) @result{} @i{false}
  2278. @end example
  2279. @subsubheading Side Effects::
  2280. The property list stored in @i{place} is modified.
  2281. @subsubheading See Also::
  2282. @ref{remprop}
  2283. ,
  2284. @ref{getf}
  2285. @node intersection, adjoin, remf, Conses Dictionary
  2286. @subsection intersection, nintersection [Function]
  2287. @code{intersection} @i{list-1 list-2 {&key} key test test-not} @result{} @i{result-list}
  2288. @code{nintersection} @i{list-1 list-2 {&key} key test test-not} @result{} @i{result-list}
  2289. @subsubheading Arguments and Values::
  2290. @i{list-1}---a @i{proper list}.
  2291. @i{list-2}---a @i{proper list}.
  2292. @i{test}---a @i{designator} for a @i{function} of two @i{arguments}
  2293. that returns a @i{generalized boolean}.
  2294. @i{test-not}---a @i{designator} for
  2295. a @i{function} of two @i{arguments}
  2296. that returns a @i{generalized boolean}.
  2297. @i{key}---a @i{designator} for a @i{function} of one argument,
  2298. or @b{nil}.
  2299. @i{result-list}---a @i{list}.
  2300. @subsubheading Description::
  2301. @b{intersection} and @b{nintersection} return a @i{list}
  2302. that contains every element that occurs in both @i{list-1} and @i{list-2}.
  2303. @b{nintersection} is the destructive version of @b{intersection}.
  2304. It performs the same operation,
  2305. but may destroy @i{list-1} using its cells to construct the result.
  2306. @i{list-2} is not destroyed.
  2307. The intersection operation is described as follows.
  2308. For all possible ordered pairs consisting of
  2309. one @i{element} from @i{list-1}
  2310. and one @i{element} from @i{list-2},
  2311. @t{:test} or @t{:test-not} are used
  2312. to determine whether they @i{satisfy the test}.
  2313. The first argument to the @t{:test} or @t{:test-not}
  2314. function is an element of @i{list-1}; the second argument is an
  2315. element of @i{list-2}.
  2316. If @t{:test} or @t{:test-not} is not supplied, @b{eql}
  2317. is used.
  2318. It is an error if @t{:test} and @t{:test-not} are supplied in
  2319. the same function call.
  2320. If @t{:key} is supplied (and not @b{nil}), it is used to
  2321. extract the part to be tested from the @i{list} element.
  2322. The argument to the @t{:key} function
  2323. is an element of either @i{list-1} or @i{list-2};
  2324. the @t{:key} function typically returns part of the supplied element.
  2325. If @t{:key} is not supplied or @b{nil}, the @i{list-1} and
  2326. @i{list-2} elements are used.
  2327. For every pair that @i{satifies the test},
  2328. exactly one of the two elements of the pair will be put in the result.
  2329. No element from either @i{list} appears in the result that does not
  2330. @i{satisfy the test} for
  2331. an element from the other @i{list}.
  2332. If one of the @i{lists} contains duplicate
  2333. elements, there may be duplication in the result.
  2334. There is no guarantee that the order of elements in the result will
  2335. reflect the ordering of the arguments in any particular way.
  2336. The result @i{list} may share cells with,
  2337. or be @b{eq} to, either @i{list-1} or @i{list-2}
  2338. if appropriate.
  2339. @subsubheading Examples::
  2340. @example
  2341. (setq list1 (list 1 1 2 3 4 a b c "A" "B" "C" "d")
  2342. list2 (list 1 4 5 b c d "a" "B" "c" "D"))
  2343. @result{} (1 4 5 B C D "a" "B" "c" "D")
  2344. (intersection list1 list2) @result{} (C B 4 1 1)
  2345. (intersection list1 list2 :test 'equal) @result{} ("B" C B 4 1 1)
  2346. (intersection list1 list2 :test #'equalp) @result{} ("d" "C" "B" "A" C B 4 1 1)
  2347. (nintersection list1 list2) @result{} (1 1 4 B C)
  2348. list1 @result{} @i{implementation-dependent} ;@i{e.g.}, (1 1 4 B C)
  2349. list2 @result{} @i{implementation-dependent} ;@i{e.g.}, (1 4 5 B C D "a" "B" "c" "D")
  2350. (setq list1 (copy-list '((1 . 2) (2 . 3) (3 . 4) (4 . 5))))
  2351. @result{} ((1 . 2) (2 . 3) (3 . 4) (4 . 5))
  2352. (setq list2 (copy-list '((1 . 3) (2 . 4) (3 . 6) (4 . 8))))
  2353. @result{} ((1 . 3) (2 . 4) (3 . 6) (4 . 8))
  2354. (nintersection list1 list2 :key #'cdr) @result{} ((2 . 3) (3 . 4))
  2355. list1 @result{} @i{implementation-dependent} ;@i{e.g.}, ((1 . 2) (2 . 3) (3 . 4))
  2356. list2 @result{} @i{implementation-dependent} ;@i{e.g.}, ((1 . 3) (2 . 4) (3 . 6) (4 . 8))
  2357. @end example
  2358. @subsubheading Side Effects::
  2359. @b{nintersection} can modify @i{list-1},
  2360. but not @i{list-2}.
  2361. @subsubheading Exceptional Situations::
  2362. Should be prepared to signal an error of @i{type} @b{type-error}
  2363. if @i{list-1} and @i{list-2} are not @i{proper lists}.
  2364. @subsubheading See Also::
  2365. @ref{union; nunion}
  2366. ,
  2367. @ref{Compiler Terminology},
  2368. @ref{Traversal Rules and Side Effects}
  2369. @subsubheading Notes::
  2370. The @t{:test-not} parameter is deprecated.
  2371. Since the @b{nintersection} side effect is not required,
  2372. it should not be used in for-effect-only
  2373. positions in portable code.
  2374. @node adjoin, pushnew, intersection, Conses Dictionary
  2375. @subsection adjoin [Function]
  2376. @code{adjoin} @i{item list {&key} key test test-not} @result{} @i{new-list}
  2377. @subsubheading Arguments and Values::
  2378. @i{item}---an @i{object}.
  2379. @i{list}---a @i{proper list}.
  2380. @i{test}---a @i{designator} for a @i{function} of two @i{arguments}
  2381. that returns a @i{generalized boolean}.
  2382. @i{test-not}---a @i{designator} for
  2383. a @i{function} of two @i{arguments}
  2384. that returns a @i{generalized boolean}.
  2385. @i{key}---a @i{designator} for a @i{function} of one argument,
  2386. or @b{nil}.
  2387. @i{new-list}---a @i{list}.
  2388. @subsubheading Description::
  2389. Tests whether @i{item} is the same as an existing element of @i{list}.
  2390. If the @i{item} is not an existing element,
  2391. @b{adjoin} adds it to @i{list} (as if by @b{cons})
  2392. and returns the resulting @i{list};
  2393. otherwise, nothing is added and the original @i{list} is returned.
  2394. The @i{test}, @i{test-not}, and @i{key}
  2395. affect how it is determined whether @i{item} is the same as an @i{element} of @i{list}.
  2396. For details, see @ref{Satisfying a Two-Argument Test}.\ifvmode\else\endgraf
  2397. \ifdim \prevdepth>-1000pt
  2398. \NIS\parskip \normalparskip\relax\fi
  2399. @subsubheading Examples::
  2400. @example
  2401. (setq slist '()) @result{} NIL
  2402. (adjoin 'a slist) @result{} (A)
  2403. slist @result{} NIL
  2404. (setq slist (adjoin '(test-item 1) slist)) @result{} ((TEST-ITEM 1))
  2405. (adjoin '(test-item 1) slist) @result{} ((TEST-ITEM 1) (TEST-ITEM 1))
  2406. (adjoin '(test-item 1) slist :test 'equal) @result{} ((TEST-ITEM 1))
  2407. (adjoin '(new-test-item 1) slist :key #'cadr) @result{} ((TEST-ITEM 1))
  2408. (adjoin '(new-test-item 1) slist) @result{} ((NEW-TEST-ITEM 1) (TEST-ITEM 1))
  2409. @end example
  2410. @subsubheading Exceptional Situations::
  2411. Should be prepared to signal an error of @i{type} @b{type-error}
  2412. if @i{list} is not a @i{proper list}.
  2413. @subsubheading See Also::
  2414. @ref{pushnew}
  2415. ,
  2416. @ref{Traversal Rules and Side Effects}
  2417. @subsubheading Notes::
  2418. The @t{:test-not} parameter is deprecated.
  2419. @example
  2420. (adjoin item list :key fn)
  2421. @equiv{} (if (member (fn item) list :key fn) list (cons item list))
  2422. @end example
  2423. @node pushnew, set-difference, adjoin, Conses Dictionary
  2424. @subsection pushnew [Macro]
  2425. @code{pushnew} @i{item place {&key} key test test-not}@*
  2426. @result{} @i{new-place-value}
  2427. @subsubheading Arguments and Values::
  2428. @i{item}---an @i{object}.
  2429. @i{place}---a @i{place}, the @i{value} of which is a @i{proper list}.
  2430. @i{test}---a @i{designator} for a @i{function} of two @i{arguments}
  2431. that returns a @i{generalized boolean}.
  2432. @i{test-not}---a @i{designator} for
  2433. a @i{function} of two @i{arguments}
  2434. that returns a @i{generalized boolean}.
  2435. @i{key}---a @i{designator} for a @i{function} of one argument,
  2436. or @b{nil}.
  2437. @i{new-place-value}---a @i{list} (the new @i{value} of @i{place}).
  2438. @subsubheading Description::
  2439. @b{pushnew} tests whether @i{item} is the same as any existing
  2440. element of the @i{list} stored in @i{place}. If @i{item} is not,
  2441. it is prepended to the @i{list}, and the new @i{list} is stored in
  2442. @i{place}.
  2443. @b{pushnew} returns the new @i{list} that is stored in @i{place}.
  2444. Whether or not @i{item} is already a member of the @i{list} that is
  2445. in @i{place} is determined by comparisons using @t{:test} or @t{:test-not}.
  2446. The first argument to the @t{:test} or @t{:test-not}
  2447. function is @i{item}; the second argument is
  2448. an element of the @i{list} in @i{place} as returned by
  2449. the @t{:key} function (if supplied).
  2450. If @t{:key} is supplied, it is used to extract the part to be tested from
  2451. both @i{item} and the @i{list} element,
  2452. as for @b{adjoin}.
  2453. The argument to the @t{:key} function
  2454. is an element of the @i{list} stored in
  2455. @i{place}. The @t{:key} function typically returns part
  2456. part of the element of the @i{list}.
  2457. If @t{:key} is not supplied or @b{nil}, the @i{list}
  2458. element is used.
  2459. For information about the @i{evaluation} of @i{subforms} of @i{place},
  2460. see @ref{Evaluation of Subforms to Places}.
  2461. It is @i{implementation-dependent} whether or not @b{pushnew}
  2462. actually executes the storing form for its @i{place} in the
  2463. situation where the @i{item} is already a member of the @i{list}
  2464. held by @i{place}.
  2465. @subsubheading Examples::
  2466. @example
  2467. (setq x '(a (b c) d)) @result{} (A (B C) D)
  2468. (pushnew 5 (cadr x)) @result{} (5 B C)
  2469. x @result{} (A (5 B C) D)
  2470. (pushnew 'b (cadr x)) @result{} (5 B C)
  2471. x @result{} (A (5 B C) D)
  2472. (setq lst '((1) (1 2) (1 2 3))) @result{} ((1) (1 2) (1 2 3))
  2473. (pushnew '(2) lst) @result{} ((2) (1) (1 2) (1 2 3))
  2474. (pushnew '(1) lst) @result{} ((1) (2) (1) (1 2) (1 2 3))
  2475. (pushnew '(1) lst :test 'equal) @result{} ((1) (2) (1) (1 2) (1 2 3))
  2476. (pushnew '(1) lst :key #'car) @result{} ((1) (2) (1) (1 2) (1 2 3))
  2477. @end example
  2478. @subsubheading Side Effects::
  2479. The contents of @i{place} may be modified.
  2480. @subsubheading See Also::
  2481. @ref{push}
  2482. ,
  2483. @ref{adjoin}
  2484. ,
  2485. @ref{Generalized Reference}
  2486. @subsubheading Notes::
  2487. The effect of
  2488. @example
  2489. (pushnew item place :test p)
  2490. @end example
  2491. is roughly equivalent to
  2492. @example
  2493. (setf place (adjoin item place :test p))
  2494. @end example
  2495. except that the @i{subforms} of @t{place} are evaluated only once,
  2496. and @t{item} is evaluated before @t{place}.
  2497. @node set-difference, set-exclusive-or, pushnew, Conses Dictionary
  2498. @subsection set-difference, nset-difference [Function]
  2499. @code{set-difference} @i{list-1 list-2 {&key} key test test-not} @result{} @i{result-list}
  2500. @code{nset-difference} @i{list-1 list-2 {&key} key test test-not} @result{} @i{result-list}
  2501. @subsubheading Arguments and Values::
  2502. @i{list-1}---a @i{proper list}.
  2503. @i{list-2}---a @i{proper list}.
  2504. @i{test}---a @i{designator} for a @i{function} of two @i{arguments}
  2505. that returns a @i{generalized boolean}.
  2506. @i{test-not}---a @i{designator} for
  2507. a @i{function} of two @i{arguments}
  2508. that returns a @i{generalized boolean}.
  2509. @i{key}---a @i{designator} for a @i{function} of one argument,
  2510. or @b{nil}.
  2511. @i{result-list}---a @i{list}.
  2512. @subsubheading Description::
  2513. @b{set-difference} returns a @i{list}
  2514. of elements of @i{list-1}
  2515. that do not appear in @i{list-2}.
  2516. @b{nset-difference} is the destructive
  2517. version of @b{set-difference}.
  2518. It may destroy @i{list-1}.
  2519. For all possible ordered pairs consisting of
  2520. one element from @i{list-1} and one element from @i{list-2}, the
  2521. @t{:test} or @t{:test-not} function is
  2522. used to determine whether they @i{satisfy the test}.
  2523. The first argument to the @t{:test} or @t{:test-not} function
  2524. is the part of an element of @i{list-1} that is returned by
  2525. the @t{:key} function (if supplied); the second argument is the part of
  2526. an element of @i{list-2} that is
  2527. returned by the @t{:key} function (if supplied).
  2528. If @t{:key} is supplied, its argument is a @i{list-1} or
  2529. @i{list-2} element. The @t{:key} function
  2530. typically returns part of
  2531. the supplied element.
  2532. If @t{:key} is not supplied, the @i{list-1} or @i{list-2}
  2533. element is used.
  2534. An element of @i{list-1}
  2535. appears in the result if and only if it does not match any element
  2536. of @i{list-2}.
  2537. There is no guarantee that the order of elements in the result will
  2538. reflect the ordering of the arguments in any particular way.
  2539. The result @i{list}
  2540. may share cells with, or be @b{eq} to, either of @i{list-1}
  2541. or @i{list-2},
  2542. if appropriate.
  2543. @subsubheading Examples::
  2544. @example
  2545. (setq lst1 (list "A" "b" "C" "d")
  2546. lst2 (list "a" "B" "C" "d")) @result{} ("a" "B" "C" "d")
  2547. (set-difference lst1 lst2) @result{} ("d" "C" "b" "A")
  2548. (set-difference lst1 lst2 :test 'equal) @result{} ("b" "A")
  2549. (set-difference lst1 lst2 :test #'equalp) @result{} NIL
  2550. (nset-difference lst1 lst2 :test #'string=) @result{} ("A" "b")
  2551. (setq lst1 '(("a" . "b") ("c" . "d") ("e" . "f")))
  2552. @result{} (("a" . "b") ("c" . "d") ("e" . "f"))
  2553. (setq lst2 '(("c" . "a") ("e" . "b") ("d" . "a")))
  2554. @result{} (("c" . "a") ("e" . "b") ("d" . "a"))
  2555. (nset-difference lst1 lst2 :test #'string= :key #'cdr)
  2556. @result{} (("c" . "d") ("e" . "f"))
  2557. lst1 @result{} (("a" . "b") ("c" . "d") ("e" . "f"))
  2558. lst2 @result{} (("c" . "a") ("e" . "b") ("d" . "a"))
  2559. @end example
  2560. @example
  2561. ;; Remove all flavor names that contain "c" or "w".
  2562. (set-difference '("strawberry" "chocolate" "banana"
  2563. "lemon" "pistachio" "rhubarb")
  2564. '(#\c #\w)
  2565. :test #'(lambda (s c) (find c s)))
  2566. @result{} ("banana" "rhubarb" "lemon") ;One possible ordering.
  2567. @end example
  2568. @subsubheading Side Effects::
  2569. @b{nset-difference} may destroy @i{list-1}.
  2570. @subsubheading Exceptional Situations::
  2571. Should be prepared to signal an error of @i{type} @b{type-error}
  2572. if @i{list-1} and @i{list-2} are not @i{proper lists}.
  2573. @subsubheading See Also::
  2574. @ref{Compiler Terminology},
  2575. @ref{Traversal Rules and Side Effects}
  2576. @subsubheading Notes::
  2577. The @t{:test-not} parameter is deprecated.
  2578. @node set-exclusive-or, subsetp, set-difference, Conses Dictionary
  2579. @subsection set-exclusive-or, nset-exclusive-or [Function]
  2580. @code{set-exclusive-or} @i{list-1 list-2 {&key} key test test-not} @result{} @i{result-list}
  2581. @code{nset-exclusive-or} @i{list-1 list-2 {&key} key test test-not} @result{} @i{result-list}
  2582. @subsubheading Arguments and Values::
  2583. @i{list-1}---a @i{proper list}.
  2584. @i{list-2}---a @i{proper list}.
  2585. @i{test}---a @i{designator} for a @i{function} of two @i{arguments}
  2586. that returns a @i{generalized boolean}.
  2587. @i{test-not}---a @i{designator} for
  2588. a @i{function} of two @i{arguments}
  2589. that returns a @i{generalized boolean}.
  2590. @i{key}---a @i{designator} for a @i{function} of one argument,
  2591. or @b{nil}.
  2592. @i{result-list}---a @i{list}.
  2593. @subsubheading Description::
  2594. @b{set-exclusive-or} returns a @i{list} of elements that appear
  2595. in exactly one of @i{list-1} and @i{list-2}.
  2596. @b{nset-exclusive-or}
  2597. is the @i{destructive} version of @b{set-exclusive-or}.
  2598. For all possible ordered pairs consisting of
  2599. one element from @i{list-1} and one element from @i{list-2}, the
  2600. @t{:test} or @t{:test-not} function is
  2601. used to determine whether they @i{satisfy the test}.
  2602. If @t{:key} is supplied, it is used to
  2603. extract the part to be tested from the @i{list-1} or @i{list-2} element.
  2604. The first argument to the @t{:test} or @t{:test-not} function
  2605. is the part of an element of @i{list-1} extracted by the @t{:key}
  2606. function (if supplied); the second argument is the part of an
  2607. element of @i{list-2} extracted by the @t{:key} function (if supplied).
  2608. If @t{:key} is not supplied or @b{nil}, the @i{list-1} or
  2609. @i{list-2} element is used.
  2610. The result contains precisely
  2611. those elements of @i{list-1} and @i{list-2}
  2612. that appear in no matching pair.
  2613. The result @i{list} of @b{set-exclusive-or}
  2614. might share storage with one of @i{list-1} or @i{list-2}.
  2615. @subsubheading Examples::
  2616. @example
  2617. (setq lst1 (list 1 "a" "b")
  2618. lst2 (list 1 "A" "b")) @result{} (1 "A" "b")
  2619. (set-exclusive-or lst1 lst2) @result{} ("b" "A" "b" "a")
  2620. (set-exclusive-or lst1 lst2 :test #'equal) @result{} ("A" "a")
  2621. (set-exclusive-or lst1 lst2 :test 'equalp) @result{} NIL
  2622. (nset-exclusive-or lst1 lst2) @result{} ("a" "b" "A" "b")
  2623. (setq lst1 (list (("a" . "b") ("c" . "d") ("e" . "f"))))
  2624. @result{} (("a" . "b") ("c" . "d") ("e" . "f"))
  2625. (setq lst2 (list (("c" . "a") ("e" . "b") ("d" . "a"))))
  2626. @result{} (("c" . "a") ("e" . "b") ("d" . "a"))
  2627. (nset-exclusive-or lst1 lst2 :test #'string= :key #'cdr)
  2628. @result{} (("c" . "d") ("e" . "f") ("c" . "a") ("d" . "a"))
  2629. lst1 @result{} (("a" . "b") ("c" . "d") ("e" . "f"))
  2630. lst2 @result{} (("c" . "a") ("d" . "a"))
  2631. @end example
  2632. @subsubheading Side Effects::
  2633. @b{nset-exclusive-or} is permitted to modify any part,
  2634. @i{car} or @i{cdr}, of the @i{list structure} of @i{list-1} or @i{list-2}.
  2635. @subsubheading Exceptional Situations::
  2636. Should be prepared to signal an error of @i{type} @b{type-error}
  2637. if @i{list-1} and @i{list-2} are not @i{proper lists}.
  2638. @subsubheading See Also::
  2639. @ref{Compiler Terminology},
  2640. @ref{Traversal Rules and Side Effects}
  2641. @subsubheading Notes::
  2642. The @t{:test-not} parameter is deprecated.
  2643. Since the @b{nset-exclusive-or} side effect is not required,
  2644. it should not be used in for-effect-only
  2645. positions in portable code.
  2646. @node subsetp, union, set-exclusive-or, Conses Dictionary
  2647. @subsection subsetp [Function]
  2648. @code{subsetp} @i{list-1 list-2 {&key} key test test-not} @result{} @i{generalized-boolean}
  2649. @subsubheading Arguments and Values::
  2650. @i{list-1}---a @i{proper list}.
  2651. @i{list-2}---a @i{proper list}.
  2652. @i{test}---a @i{designator} for a @i{function} of two @i{arguments}
  2653. that returns a @i{generalized boolean}.
  2654. @i{test-not}---a @i{designator} for
  2655. a @i{function} of two @i{arguments}
  2656. that returns a @i{generalized boolean}.
  2657. @i{key}---a @i{designator} for a @i{function} of one argument,
  2658. or @b{nil}.
  2659. @i{generalized-boolean}---a @i{generalized boolean}.
  2660. @subsubheading Description::
  2661. @b{subsetp} returns @i{true} if every element of @i{list-1}
  2662. matches some element of @i{list-2},
  2663. and @i{false} otherwise.
  2664. Whether a list element is the same as another list element is
  2665. determined by the functions specified by the keyword arguments.
  2666. The first argument to the @t{:test} or @t{:test-not}
  2667. function is
  2668. typically
  2669. part of an element of @i{list-1} extracted by
  2670. the @t{:key} function; the second argument is typically part of
  2671. an element of @i{list-2} extracted by
  2672. the @t{:key} function.
  2673. The argument to the @t{:key} function is an element of either
  2674. @i{list-1} or @i{list-2}; the return value is part of the element
  2675. of the supplied list element.
  2676. If @t{:key} is not supplied or @b{nil},
  2677. the @i{list-1} or @i{list-2}
  2678. element itself is supplied to the @t{:test} or @t{:test-not}
  2679. function.
  2680. @subsubheading Examples::
  2681. @example
  2682. (setq cosmos '(1 "a" (1 2))) @result{} (1 "a" (1 2))
  2683. (subsetp '(1) cosmos) @result{} @i{true}
  2684. (subsetp '((1 2)) cosmos) @result{} @i{false}
  2685. (subsetp '((1 2)) cosmos :test 'equal) @result{} @i{true}
  2686. (subsetp '(1 "A") cosmos :test #'equalp) @result{} @i{true}
  2687. (subsetp '((1) (2)) '((1) (2))) @result{} @i{false}
  2688. (subsetp '((1) (2)) '((1) (2)) :key #'car) @result{} @i{true}
  2689. @end example
  2690. @subsubheading Exceptional Situations::
  2691. Should be prepared to signal an error of @i{type} @b{type-error}
  2692. if @i{list-1} and @i{list-2} are not @i{proper lists}.
  2693. @subsubheading See Also::
  2694. @ref{Traversal Rules and Side Effects}
  2695. @subsubheading Notes::
  2696. The @t{:test-not} parameter is deprecated.
  2697. @node union, , subsetp, Conses Dictionary
  2698. @subsection union, nunion [Function]
  2699. @code{union} @i{list-1 list-2 {&key} key test test-not} @result{} @i{result-list}
  2700. @code{nunion} @i{list-1 list-2 {&key} key test test-not} @result{} @i{result-list}
  2701. @subsubheading Arguments and Values::
  2702. @i{list-1}---a @i{proper list}.
  2703. @i{list-2}---a @i{proper list}.
  2704. @i{test}---a @i{designator} for a @i{function} of two @i{arguments}
  2705. that returns a @i{generalized boolean}.
  2706. @i{test-not}---a @i{designator} for
  2707. a @i{function} of two @i{arguments}
  2708. that returns a @i{generalized boolean}.
  2709. @i{key}---a @i{designator} for a @i{function} of one argument,
  2710. or @b{nil}.
  2711. @i{result-list}---a @i{list}.
  2712. @subsubheading Description::
  2713. @b{union} and @b{nunion} return a @i{list}
  2714. that contains every element that occurs in either @i{list-1}
  2715. or @i{list-2}.
  2716. For all possible ordered pairs consisting of one
  2717. element from @i{list-1}
  2718. and one element from @i{list-2}, @t{:test} or @t{:test-not} is used
  2719. to determine whether they @i{satisfy the test}.
  2720. The first argument to the @t{:test} or @t{:test-not}
  2721. function is the part of the element of @i{list-1} extracted by the
  2722. @t{:key} function (if supplied); the second argument
  2723. is the part of the element of @i{list-2} extracted by the
  2724. @t{:key} function (if supplied).
  2725. The argument to the @t{:key} function is an element of
  2726. @i{list-1} or @i{list-2}; the return value is part of the supplied
  2727. element.
  2728. If @t{:key} is not supplied or @b{nil},
  2729. the element of @i{list-1} or @i{list-2}
  2730. itself is supplied to the @t{:test} or @t{:test-not} function.
  2731. For every matching pair,
  2732. one of the two elements of the pair will be in the result. Any
  2733. element from either @i{list-1} or @i{list-2}
  2734. that matches no element of the other will appear
  2735. in the result.
  2736. If there is a duplication between @i{list-1}
  2737. and @i{list-2},
  2738. only one of the duplicate instances will be in the result.
  2739. If either @i{list-1}
  2740. or @i{list-2} has duplicate entries within it,
  2741. the redundant entries
  2742. might or might not appear in the result.
  2743. The order of elements in the result do not have to
  2744. reflect the ordering of @i{list-1} or @i{list-2} in any way.
  2745. The result @i{list} may be @b{eq} to either
  2746. @i{list-1} or @i{list-2} if appropriate.
  2747. @subsubheading Examples::
  2748. @example
  2749. (union '(a b c) '(f a d))
  2750. @result{} (A B C F D)
  2751. @i{OR}@result{} (B C F A D)
  2752. @i{OR}@result{} (D F A B C)
  2753. (union '((x 5) (y 6)) '((z 2) (x 4)) :key #'car)
  2754. @result{} ((X 5) (Y 6) (Z 2))
  2755. @i{OR}@result{} ((X 4) (Y 6) (Z 2))
  2756. (setq lst1 (list 1 2 '(1 2) "a" "b")
  2757. lst2 (list 2 3 '(2 3) "B" "C"))
  2758. @result{} (2 3 (2 3) "B" "C")
  2759. (nunion lst1 lst2)
  2760. @result{} (1 (1 2) "a" "b" 2 3 (2 3) "B" "C")
  2761. @i{OR}@result{} (1 2 (1 2) "a" "b" "C" "B" (2 3) 3)
  2762. @end example
  2763. @subsubheading Side Effects::
  2764. @b{nunion} is permitted to modify any part, @i{car} or @i{cdr},
  2765. of the @i{list structure} of @i{list-1} or @i{list-2}.
  2766. @subsubheading Exceptional Situations::
  2767. Should be prepared to signal an error of @i{type} @b{type-error}
  2768. if @i{list-1} and @i{list-2} are not @i{proper lists}.
  2769. @subsubheading See Also::
  2770. @ref{intersection; nintersection}
  2771. ,
  2772. @ref{Compiler Terminology},
  2773. @ref{Traversal Rules and Side Effects}
  2774. @subsubheading Notes::
  2775. The @t{:test-not} parameter is deprecated.
  2776. Since the @b{nunion} side effect is not required,
  2777. it should not be used in for-effect-only positions in portable code.
  2778. @c end of including dict-conses
  2779. @c %**end of chapter