ProgrammingwithLisplLispBasicslAdvancedLisplInteractingWithLispLispBasicslS-ExpressionslAtomslnillListslFormslFunctionslMacroslSpecialFormslConslListlCar(first)lCdr(rest)lquotelprint/prin1/princlread-evaluate-printcyclelsetlsetqlsetflpredicatesliflcondlcommentsS-Expressions–InformalDefinitionl“SymbolicExpression”lComposedof:natomsncomposites-expression(e.g.lists)AtomslAtomscanbe:nnumbersnsymbol(I.e.identifierorvariable)ncharacternstringarraylusuallydescribedbystringofcharacterslmayalsoincludedigitsandmostpunctuationmarksBUTluseofthefollowingshouldbeavoided:?![]{}lLispmapslower-casecharacterstouppercaseAtomsValidNumbersl5l0.0003l-6.95ValidSymbolslAla(sameasA)lAAAAAAlaaaaaa(sameasAAAAAA)laAaAAa(sameasAAAAAA)lboblfile.txtl/homes/usrllong-file-namel*global-variable-1*l1+Atom-nillnilistheemptylistldesignatedbytheword“nil”orbyanemptylist“()”lnilisusedasFALSEinbooleanoperationsListslcomposites-expressionlzeroormores-expressionsenclosedinparenthesis“()”lProperListsn()n(a)n(abc)n(a(bc))n((ab)c)n(function-namearg1arg2)lImproperListsnnanabcn(ab)c)n1+1Formslalistwhosefirstelementnamesa“function,”“macro,”or“specialform”lExamplesn(quotevar-name)n(+11)n(cons‘anil)n(setfpets‘(dogcatfish))FormslFunctionalFormsn1stelementisafunctionnamenargumentsareevaluatednargumentsmaybefunctionalforms(nesting)nmaybeuserdefinedlMacroFormsn1stelementisamacronamenargumentsarenotalwaysevaluated(dependsonform)nmaybeuserdefinedlSpecialFormsn1stelementisaspecialformnamenargumentsarenotalwaysevaluated(dependsonform)FormslFunctionsn(+5(/93))returns8lMacron(notnil)returnsTlSpecialn(if(notnil)(print“It’sTrue”))returns“It’sTrue”quotelspecialformlsuppressesevaluationn(quotea)returnsan(+11)returns2n(quote(+11)returns(+11)n(quote(abc))returns(ABC)lcanbeabbreviatedbyasinglequote“’”n‘areturnsan‘(+11)returns(+11)n‘(abc)returns(ABC)Conslfunctionl“CONstruct”alistltakestwoarguments(andthesecondisusuallyalist)andcombinesthemtoformanewliststartingwiththefirstargumentlthewordconsissometimesusedasaverb(whydon’tyouconsthosesymbolstogether)andasanoun(Ibuiltaconsoutofthetwolists)lExamplesn(cons‘anil)returns(A)n(cons‘a(cons‘bnil))returns(AB)n(cons1(cons2(cons3nil)))returns(123)n(cons‘(12)‘(345))returns((12)345)n(consnilnil)returns(nil)Cons-Examplesl(cons‘xnil)l(cons(cons1nil)‘(2))l(cons1(cons2nil))l(cons‘()‘(abc))l(cons1(cons(cons2nil)‘(3)))l(X)l((1)2)l(12)l(NILABC)l(1(2)3)ListlfunctionltakesarbitrarynumberofargumentslreturnslistoftheevaluatedvalueofargumentslExamplesn(list123)returns(123)n(list(cons1nil)23)returns((1)23)n(list‘anil)returns(ANIL)Car(first)lFunctionltakesasingleargumentlreturnsthefirstelementlsameas“first”lExamplesn(car(list123))returns1n(first‘(abc))returnsACdr(rest)lFunctionltakesasingleargumentlreturnsalistofallbutthefirstelementlsameas“rest”lExamplesn(cdr(list123))returns(23)n(rest‘(abc))returns(BC)AndifyouthoughtthehistoryofCarandCdrwasbad…lU.S.standardrailroadgauge4’8”lfromEnglishrailroadstandardslfromEnglishtramwayslfromjigsandtoolsforbuildingwagonslfromwheel-rutsinEuropeanroadslfromRomanroadslfromRomanwarchariotslbigenoughfortwowarhorsesprint/prin1/princlalltakeasingleargumentlprintoutputstheargumentprecededbyanewline(thenreturnsthevalueoftheargument)lprin1outputstheargument(thenreturnsthevalueoftheargument)lprincoutputstheargumentANDignoressomespecialcharacters(thenreturnsthevalueoftheargument)PrintExamplesn(prog()(print“hello”)(print“there”))RETURNS–“hello”–“there”n(prog()(prin1“hello”)(prin1“there”))RETURNS–“hello”“there”n(prog()(princ“hello”)(princ“there”))RETURNS–hellotheresetlfunctionlassignsavaluetoanevaluatedsymbollExamplesn(set‘x349)n(set‘y(+22))n(set‘new-list‘(abc))n(set(carnew-list)999)setqlspecialformlstandsfor“setquote”lassignsavaluetoanunevaluatedsymbol(don’tneedtoaddaquote)lExamplesn(setqx349)n(setqy(+22))n(setqnew-list‘(abc))n(set(carnew-list)999)setflmacrolbehaveslikesetqundersimilarcircumstanceslamoregeneralconstruct-performsbothaccessandupdatelcanbeusedtoreplacesetandsetqlMayhavesomeunintendedsideeffects(seesetvssetf)lExamplesn(setfx349)n(setfy(+22))n(setfnew-list‘(abc))n(set(carnew-list)999)n(setfnew-list‘(abc))n(setf(symbol-value(carnew-list))888)n(setf(carnewlist)777)Setfprobleml(setfa1)l(setfb‘(abc))l(setfc(consa‘(z)))l(set(carb)99)l(setf(carb)a)lA=1lB=(ABC)lC=(1Z)lA=99lB=(99BC)AccessingCLISPlWeuseGNUclispCommonLisplAvailableforfreefromSourceforgelRunsonmultipleplatformslSeehandoutformoreinfonInstallingonwindowsnTipsforusingtheclispenvironmentlLispreferenceandClispdocumentationinDocsfoldernIwillpostacopyALispRiddlelThefollowingLispformcanbereadasacommoninspirationalsaying.Doyourecognizeit?l(eq‘today(car(cdr‘(yourlife))))MyotherCARisaCDRMoreListConstructions-AppendlfunctionltakesanarbitrarynumberoflistsandconcatenatesthemtoformanewlistlEmptylists(e.g.NIL)areignoredlExamplesn(append'(abc)'(def)'()'(g))=(ABCDEFG)n(setqlst'(abc))=(ABC)n(appendlst'(d))=(ABCD)MoreListAccesslFirst,second,third…tenthnReturnthedesignatedlistelementn(setqa‘(1234))n(firsta)RETURNS1n(fourtha)RETURNS4n(Fiftha)RETURNSnillCAAR,CAADRetcpredicateslret