ebook img

Programming Languages: An Active Learning Approach PDF

289 Pages·2008·3.92 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Programming Languages: An Active Learning Approach

PROGRAMMING LANGUAGES: AN ACTIVE LEARNING APPROACH PROGRAMMING LANGUAGES: AN ACTIVE LEARNING APPROACH Kent D. Lee Luther College 123 Kent D. Lee Luther College Department of Computer Science 700 College Drive Decorah, Iowa 52101-1045 [email protected] ISBN: 978-0-387-79421-1 e-ISBN: 978-0-387-79422-8 DO I: 10.1007/ 978-0-387-79421-1 Library of Congress Control Number: 2008924915 © 2008 Springer Science+Business Media, LLC All rights reserved. This work may not be translated or copied in whole or in part without the written permission of the publisher (Springer Science+Business Media, LLC, 233 Spring Street, New York, NY 10013, USA), except for brief excerpts in connection with reviews or scholarly analysis. Use in connection with any form of information storage and retrieval, electronic adaptation, computer soft- ware, or by similar or dissimilar methodology now known or hereafter developed is forbidden. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. Printed on acid-free paper 9 8 7 6 5 4 3 2 1 springer.com To Denise. Preface ComputerSciencehasmaturedintoaverylargediscipline.Youwillnotbetaught everything you will need in your career while you are a student. The goal of a Computer Science education is to prepare you for a life of learning. The creativ- ityencouragedbyalifetimeoflearningmakesComputerScienceoneofthemost excitingfieldstodayaccordingtoMoneyMagazine’sBestJobsinAmericain2006. ThewordsComputerSciencedon’treallyreflectwhatmostcomputerprogram- mersdo.ThefieldreallyshouldbenamedsomethinglikeComputerProgramEngi- neering.Itismoreofanengineeringfieldthanascience.That’swhyprogrammers are often called Software Engineers. There is science in Computer Science gener- ally relating to the gathering of empirical evidence pertaining to performance of algorithmsandhardware.Therearealsotheoreticalaspectsofthediscipline,some of which this text will explore. However, the bulk of what computer scientists do is related to the creative process of building programs. As computer scientists we knowsomethingsabouthowtowritegoodprograms,butitisstillaverycreative process. Given that programming is such a creative process, it is imperative that we be abletopredictwhatourprogramswilldo.Topredictwhataprogramwilldo,you mustunderstandhowtheprogramworks.Theprogramsofalanguageexecuteac- cordingtoamodelofcomputation.Amodelmaybeimplementedinmanydifferent waysdependingonthetargetedhardwarearchitecture.However,itisnotnecessary to understand all the different architectures out there to understand the model of computationusedbyalanguage. Forseveralyearsinthelate1980’sand1990’sIworkedatIBMontheoperating systemfortheAS/400(whatisnowcalledSystemiandthei5/OS).Myunderstand- ing of compilers and language implementation helped me make better decisions about how to write code and several times helped me find problems in code I was testing.AnunderstandingofthemodelsofcomputationusedbythelanguagesIpro- grammedinaidedmeinpredictingwhatmyprogramswoulddo.Aftercompletinga courseinprogramminglanguages,youshouldunderstandsomeofthebasicsoflan- guageimplementation.Thisbookisnotintendedtobeacompletetextoncompiler orinterpreterimplementation,butthereareaspectsoflanguageimplementationthat areincludedinit.Wearebetterusersoftoolswhenweunderstandhowthetoolswe usework. Ihopeyouenjoylearningfromthistextandthecourseyouareabouttotake.The textismeanttobeusedinteractively.Youshouldreadasectionandasyoureadit, dothepracticeexerciseslistedinthegrayboxes.Eachoftheexercisesaremeantto giveyouagoalinreadingasectionofthetext. The text has a website where code and other support files may be downloaded. Seehttp://www.cs.luther.edu/˜leekent/ProgrammingLanguagesforallsupportfiles. viii Preface ForTeachers This book was written to fulfill two goals. The first is to introduce students to three programming paradigms: object-oriented/imperative, functional, and logic programming.Tobereadyforthecontentofthisbookstudentsshouldhavesome background in an imperative language, probably an object-oriented language like PythonorJava.TheyshouldhavehadanintroductorycourseandacourseinData Structuresasaminimum.Whilethepreparedstudentwillhavewrittenseveralpro- grams, some of them fairly complex, most probably still struggle with predicting exactlywhattheirprogramwilldo.Itisassumedthatideaslikepolymorphism,re- cursion,andlogicalimplicationarerelativelynewtothestudentreadingthisbook. Thefunctionalandlogicparadigms,whilenotthemainstream,havetheirplaceand havebeensuccessfullyusedininterestingapplications. TheObject-OrientedlanguagespresentedinthisbookareC++andRuby.Teach- ersmaychoosebetweenthechapteronRubyorthechapteronC++,ormayassign both.Itmightbeusefultoreadbothchaptersifyouwishtocompareandcontrast astaticallytyped,compiledlanguagetoadynamicallytyped,interpretedlanguage. The same project is presented in both chapters with the C++ chapter requiring a little more explanation in terms of the compiler and organization of the code. Ei- therlanguageisinterestingtochoosefromandthechaptersdocross-referenceeach other to compare and contrast the two styles of programming so if you only have timetocoveroneortheother,thatispossibletoo. C++hasmanynuancesthatareworthyofseveralchaptersinaprogramminglan- guagesbook.NotablythepassbyvalueandpassbyreferencemechanismsinC++ createconsiderablecomplexityinthelanguage.Polymorphismisanotherinterest- ingaspectofObject-Orientedlanguagesthatisstudiedinthistext. Ruby is relatively new to the programming language arena, but is widely ac- ceptedandisalargelanguagewhencomparedtoPythonandJava.Inaddition,its object-centeredapproachisverysimilartoSmalltalk.Rubyisalsointerestingdue totherecentdevelopmentof“RubyonRails”asacodegenerationtool. The text uses Standard ML as the functional language. ML has a polymorphic type inference system to statically type programs of the language. In addition, the typeinferencesystemofMLisformallyprovensoundandcomplete.Thishassome implicationsinwritingprograms.WhileML’scrypticcompilererrormessagesare sometimes hard to understand at first, once a program compiles it will often work correctlythefirsttime.That’sanamazingstatementtomakeifyourpastexperience isinadynamicallytypedlanguagelikeLisp,Scheme,Ruby,orPython. ThelogiclanguageisProlog.WhilePrologisanArtificialIntelligencelanguage, it originated as a meta-language for expressing other languages. The text concen- trates on using Prolog to implement other languages. Students learn about logical implicationandhowaproblemtheyarefamiliarwithcanbere-expressedinadif- ferentparadigm. Thesecondgoalofthetextistobeinteractive.Thisbookisintendedtobeusedin andoutsideofclass.Itismyexperiencethatwealmostalllearnmorebydoingthan byseeing.Tothatend,thetextencouragesteacherstoactivelyteach.Eachchapter Preface ix followsapatternofpresentingatopicfollowedbyapracticeexerciseorexercises thatencouragestudentstotrywhattheyhavejustread.Theseexercisescanbeused inclasstohelpstudentschecktheirunderstandingofatopic.Teachersareencour- agedtotakethetimetopresentatopicandthenallowstudentstimetopracticewith theconceptjustpresented.Inthiswaythetextbecomesalectureresource.Students gettwothingsoutofthis.Itforcesthemtobeinteractivelyengagedinthelectures, notjustpassiveobservers.Italsogivesthemimmediatefeedbackonkeyconcepts tohelpthemdetermineiftheyunderstandthematerialornot.Thisencouragesthem to ask questions when they have difficulty with an exercise. Tell students to bring the book to class along with a pencil and paper. The practice exercises are easily identified.Lookforthelightgraypracticeproblemboxes. Thebookpresentsseveralprojectstoreinforcetopicsoutsidetheclassroom.Each chapter of the text suggests several non-trivial programming projects that accom- panytheparadigmbeingcoveredtodrivehometheconceptscoveredinthatchapter. The projects described in this text have been tested in practice and documentation andsolutionsareavailableuponrequest. Finally, it is expected that while teaching a class using this text, lab time will be liberally sprinkled throughout the course as the instructor sees fit. Reinforcing lectures with experience makes students appreciate the difficulty of learning new paradigmswhilemakingthemstrongerprogrammers,too. Supplementarymaterialsincludingsamplelecturenotes,lectureslides,answers to exercises, and programming assignment solutions are available to instructors uponrequest. Acknowledgments I have been fortunate to have good teachers throughout high school, college, and graduate school. Good teachers are a valuable commodity and we need more of them. Ken Slonneger was my advisor in graduate school and this book came into beingbecauseofhim.Heinspiredmetowriteatextthatsupportsthesameteaching style he uses in his classroom. Encouraging students to interact during lecture by givingthemshortproblemstosolvethatreflectthematerialjustcoveredisavery effectivewaytoteach.Itmakestheclassroomexperienceactiveandenergizesthe students. Ken graciously let me use his lecture notes on Programming Languages when I started this book and some of the examples in this text come from those notes. He also provided me with feedback on this text and I appreciate all that he did.Thankyouverymuch,Ken! OthergreatteacherscometomindaswellincludingDennisTackwhotaughtme thebeautyofagoodproof,KarenNancewhotaughtmetowrite,AlanMacdonald who introduced me to programming languages as a field of study, Walt Will who taughtmehowtowritemyfirstassembler,andlastbutnotleastSteveHubbardwho still inspires me with his ability to teach complex algorithms and advanced data structurestoComputerSciencemajorsatLutherCollege!Thankstoyouall. Contents Preface............................................................ vii 1 Introduction................................................... 1 1.1 HistoricalPerspective ...................................... 2 1.2 ModelsofComputation..................................... 4 1.3 TheOriginsofaFewProgrammingLanguages ................. 7 1.4 LanguageImplementation................................... 12 1.5 Wheredowegofromhere? ................................. 17 1.6 Exercises................................................. 18 1.7 SolutionstoPracticeProblems ............................... 19 1.8 AdditionalReading ........................................ 20 2 SpecifyingSyntax .............................................. 21 2.1 Terminology .............................................. 21 2.2 BackusNaurForm(BNF)................................... 23 2.3 TheEWELanguage........................................ 24 2.4 Context-FreeGrammars .................................... 28 2.5 Derivations ............................................... 29 2.6 ParseTrees ............................................... 30 2.7 Parsing................................................... 31 2.8 ParserGenerators.......................................... 32 2.9 Bottom-UpParsers......................................... 33 2.10 Top-DownParsers ......................................... 33 2.11 OtherFormsofGrammars .................................. 33 2.12 AbstractSyntaxTrees ...................................... 37 2.13 Infix,Postfix,andPrefixExpressions.......................... 38 2.14 LimitationsofSyntacticDefinitions........................... 38 2.15 Exercises................................................. 40 2.16 SolutionstoPracticeProblems ............................... 41 2.17 AdditionalReading ........................................ 46 xii Contents 3 Object-OrientedProgrammingwithC++ ......................... 47 3.1 ApplicationDevelopment ................................... 48 3.2 TheTokenClass........................................... 55 3.3 ImplementingaClass....................................... 57 3.4 InheritanceandPolymorphism............................... 59 3.5 AHistoricalLookatParameterPassing ....................... 62 3.6 ConstinC++.............................................. 67 3.7 TheASTClasses .......................................... 70 3.8 TheScanner .............................................. 73 3.9 TheParser................................................ 75 3.10 PuttingItAllTogether...................................... 81 3.11 Exercises................................................. 84 3.12 SolutionstoPracticeProblems ............................... 87 3.13 AdditionalReading ........................................ 90 4 Object-OrientedProgrammingwithRuby ........................ 91 4.1 DesigningCalc............................................ 93 4.2 TheTokenClass........................................... 94 4.3 ParameterPassinginRubyvsC++ ........................... 95 4.4 AccessorandMutatormethodsinRuby ....................... 96 4.5 Inheritance ............................................... 97 4.6 TheASTClasses .......................................... 97 4.7 PolymorphisminRuby .....................................100 4.8 TheScanner ..............................................102 4.9 TheParser................................................103 4.10 PuttingItAllTogether......................................107 4.11 StaticvsDynamicTypeChecking ............................109 4.12 Exercises.................................................112 4.13 SolutionstoPracticeProblems ...............................114 4.14 AdditionalReading ........................................116 5 FunctionalProgramminginStandardML ........................117 5.1 ImperativevsFunctionalProgramming........................118 5.2 TheLambdaCalculus ......................................119 5.3 GettingStartedwithStandardML ............................122 5.4 Expressions,Types,Structures,andFunctions ..................123 5.5 RecursiveFunctions........................................125 5.6 Characters,Strings,andLists ................................127 5.7 PatternMatching ..........................................129 5.8 Tuples ...................................................130 5.9 LetExpressionsandScope ..................................131 5.10 Datatypes.................................................133 5.11 ParameterPassinginStandardML............................136 5.12 EfficiencyofRecursion .....................................136 5.13 TailRecursion.............................................138

Description:
Programming Languages: An Active Learning Approach introduces students to three programming paradigms: object-oriented/imperative languages using C++ and Ruby, functional languages using Standard ML, and logic programming using Prolog. This interactive textbook is intended to be used in and outside
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.