Lec5 - CMPSC461:ProgrammingLanguage Concepts Lecture5

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 Lecture5 Instructor:Prof.SwaratChaudhuri Induc>vedefini>onoflists Listofintegers(listofint): Theemptylistisalistofint Ifnisanintegerandlisalistofint,thenthe pair(n,l)isalistofint Nothingelseisalistofint Theabovedefinesthesetofdataitemsoftype listofint. BoKomupdefini>onoflists BoKomupdefini>onoflists Infact,consjustconstructspairs. car:firstcomponentofpair cdr:secondcomponent Thelist(124)isthesameas (1,(2,(4,`()))) Andonthatnote Whathappensifyoutype(cons23) Canyoudocarandcdronit? (2.3)isjustScheme'swayofwri>ngthepair (2,3) Only,Schemeissmartenoughtodetectpairs thatarelistsandprinttheminadifferentway YoucanfindmoreonthisinyourScheme book. Topdowndefini>onoflists Adataitemisalistofintif: Itistheemptylist Ifitisapair(n,l)whosecarisanintegerand cdrisalistofint Rou>ne"list?"thatchecksifadataitemisa properlistornot. AliKlebitmoreScheme:quo>ng Tellstheinterpretertotreattheexpressionimmediately a]erthequoteasdata. (quote(abc)) `(abc) `(1(23)(123)) Here,a,b,c,+aresymbolsratherthannames (iden>fiers)forexpressions. Asymbolissimplyavaluethatprintslikeaniden>fier. Whenquoteisappliedtoexpressionwithin parentheses,itisinterpretedasalist. ScopingusingLet Namesgivenusing"define"s>ckforeverunless overruled. Thenthereareanonymousvalues/func>ons Whataboutlocalnames? Localnamescanbeboundinparallelvia"let" Scopingisnested. (let([x20])(+x30)) (let([x20][y30])(+xy)) Func>onscanbeboundtoo. (let([square(lambda(x)(*xx))] [cube(lambda(x)(*xxx))]) (cube(square5))) Ingeneral, (let([n1e1][n2e2]...)body) Sequen>albinding:Let* Trydoing: (let([x20][yx])(+xy)) Reasonfortrouble:parallelbinding. Sequen>albinding:gole]toright,bindingone byone. (let([x20][yx])(+xy)) Letrec Evenlet*cannothelpyouwithrecursion.For thatyouneedletrec. (letrec([fact(lambda(x) (if(=n0) 1 (fact(n1))))])(fact5)) Moreaboutloopsvs.recursion (define (length l) (if (null? l) 0 (+ 1 (length (cdr l))))) Letustakeasampleevalua>on (length(list123))(+1(length(list23))) (+1(+1(length(list3)))) (+1(+1(+1(length`()))) (+1(+1(+10)))(+1(+11))(+12) 3 Needastacktokeeptrackofvaluestobeusedonway back. Moreaboutloopsvs.recursion Contrastthatwith curr = 0; while (cdr(l) != null) { curr = curr + 1; l = cdr(l); } Letusdefineanitera7veversionofthefunc>on: (define iter (l curr) (if (null? l) curr (iter (cdr l) (+ curr 1)))) (define (iter-length l) (iter l 0) Evalua>onofitera>veversion (iterlength(list123)) (iter(list123)0) (iter(list23)1) (iter(list3)2) (iter`()3) 3 Tailrecursion Noneedforstack!Noneedtoaddonwayback. Reason:returnvalueofrecursionisnotusedfor anothercomputa>on.Thelastthingthe func7ondoesiscallanotherfunc>on(perhaps itself). Thisiscalledtailrecursion. Schemecanimplementtailrecursivefunc>ons moreefficiently. Sideeffects:set! Impera>vevs.func>onal Forthemostpart,Schemeisfunc>onal,but thereareafew"impera>ve"features. (set!nameexp) Changesthebindingof"name"tothevalueof "exp".Here,"name"mustbeboundinthe presentenvironment. Returnsnovalue.Justhasasideeffect. Sowhatcanwedowithsideeffects? I/O:e.g.,prin>ngisasideeffect! Howcanwemodelsequencesofstatementsina Clikelanguage? Schemehassequen>alcomposi>ontoo.Evaluate theexpressionsinorder,returnthelastone. (begin(+56)21) (let([x20][y30]) ((set!x40)(set!y50)(+xy))) Herethe"begin"isimplicit. Themostimportantthingaboutset! Don'tuseitifpossible. Reason:sideeffectsbreakmodularityand composi>onalityandmakeyouthinkglobally ratherthanlocally. Inpar>cular,nouseof"set!"inAssignment1. ...
View Full Document

This note was uploaded on 09/30/2008 for the course CMPSC 461 taught by Professor Chaudhuri during the Fall '08 term at Penn State.

Ask a homework question - tutors are online