Lec2 - CMPSC461:ProgrammingLanguage Concepts Lecture2

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CMPSC461:ProgrammingLanguage Concepts Lecture2 Instructor:Prof.SwaratChaudhuri Introduc>ontoScheme:values Values: Integers,frac>ons,complexnumbers Boolean:#t,#f Strings Symbols Customdatatypes pictures! (onlyintheModuleslanguage) ... Procedures InC,youcallaprocedurebyfoo(x,y) InScheme,(fooxy).Ingeneral: (procarg1arg2...) +,,*,/areallprocedures.Nothingspecial aboutthem. ((x+y)z)becomes((+xy)z) (notx),(orxy),(andxy) Procedures (circlex),(filledrectanglexy),(filledellipsexy) (colorizepictcolor) (hcappendxy...),(vcappendxyz...) Drawa4x4checkerboard Compute(4)tothe4th. Howdowedrawannxncheckerboard??? Foodforthought:picturesthesameasintegers, floats,string,oranybasicdatatype Value("two")vsrepresenta>on("2","II",...). Expressions Basicvalues Datastructures Lists(mostimportant) Aside:SchemederivedfromLISP:ListProcessor Vectors ... Parenthesizedprocedurecalls Condi>ons ... Condi>ons (ifxconseqalt) InC:(x?conseq:alt) (if(>21)34)=3 Byconven>on,only#fisfalse.Everythingelseis treatedastrue. (cond[c1conseq1][c2conseq2]...) Defini>ons Bindingnamestovalues (defineidval) Schemeisveryliberalwithnames.*,+,/,etc.areallnames. Definingprocedures (define(procarg1arg2...)exp) Predefinedprocedures: +,*,/,*,abs,sqrt,zero? and,not,or realpart,imagpart stringappend,substring,stringlength complex?,integer?,pict? ... Defini>ons Defineaproceduretocreatea4x4 arrangementofpictures. Coloredsquaregivenadimension Cubinganumber Compu>ngdistanceofacomplexnumber fromorigin. Useatesttoreturn"OMGLOL!!!"ifinputnotof righttype. Recursion Aprocedurecallsitself. Factorial: fact(n)=1*2*...*(n1)*1 fact(0)=1 Howwouldwewriteafunc>onforthisinScheme? Recursivedefini>on(translatesdirectly): fact(0)=1 fact(n)=n*fact(n1) Mathema>calinduc>on Induc>vedefini>onofafunc>onf: Definefforthebasecase e.g.,f(k+1)=2*f(k) Ifftakesanaturalnumberasinput,n=0 Definef(k+1)intermsoff(k) Translatesimmediatelytoaprogramusingrecursion Paiern: (definef(n) (if(=n0) (blah) (blahblahinvolving(f(n1))) )) Wewillspend2classesjusttryingtothinkrecursively. Othersideofthecoin:induc>veproofs. ...
View Full Document

Ask a homework question - tutors are online