Post on 17-Oct-2015
description
2/26/14 CSE 230, Winter 2011 - Homework #1, Due Friday, January 14th
cseweb.ucsd.edu/classes/wi11/cse230/homeworks/hw1.html 1/7
CSE230W11Homework#1,DueFriday,January14th
Home Lectures Assignments Links WebCT
PART1:DEFININGANDMANIPULATINGSHAPES
PRELIMINARIES
Beforestartingthispartoftheassignment:
1. Readchapters13ofTheHaskellSchoolofExpression.2. DownloadandinstalltheGlasgowHaskellCompiler(GHC).
ASSIGNMENT(PART1)
1. Createafile hw1.hswiththedefinitionsofshapesandshapemanipulatingfunctionsfromChapter2ofSOE.Testthesedefinitionsusingghci.
2. Inthesamefile,definefunctions rectangleand rtTriangleassuggestedattheendofSection2.1(Exercise2.1).EachshouldreturnaShapebuiltwiththePolygonconstructor.
3. Defineafunction
> sides :: Shape -> Integer
whichreturnsthenumberofsidesagivenshapehas.Forthepurposesofthisexercise,anellipsehas42sides,andemptypolygons,singlepoints,andlineshavezerosides.
4. Defineafunction
> bigger :: Shape -> Float -> Shape
thattakesashape sandexpansionfactor eandreturnsashapewhichisthesameas sbutbiggerbyafactorof e.
5. TheTowersofHanoiisapuzzlewhereyouaregiventhreepegs,ononeofwhicharestackedndiscsinincreasingorderofsize.Tosolvethepuzzle,youmustmoveallthediscsfromthestartingpegtoanother
2/26/14 CSE 230, Winter 2011 - Homework #1, Due Friday, January 14th
cseweb.ucsd.edu/classes/wi11/cse230/homeworks/hw1.html 2/7
bymovingonlyonediscatatimeandneverstackingalargerdiscontopofasmallerone.
Tomovendiscsfrompegatopegbusingpegcastemporarystorage:
1. Moven1discsfrompegatopegc.2. Movetheremainingdiscfrompegatopegb.3. Moven1discsfrompegctopegb.
Writeafunction
> hanoi :: Int -> String -> String -> String -> IO ()
that,giventhenumberofdiscsnandpegnamesa,b,andc,whereaisthestartingpeg,emitstheseriesofmovesrequiredtosolvethepuzzle.Forexample,running
> hanoi 2 "a" "b" "c"
shouldemitthetext
move disc from a to cmove disc from a to bmove disc from c to b
PART2:DRAWINGFRACTALS
PRELIMINARIES
Beforestartingthispartoftheassignment:
1. DownloadtheSOEcodebundlefromtheHaskellSchoolofExpressionpage.
2. Verifythatitworksbychangingintothe SOE/srcdirectoryandrunning ghci Draw.lhs,thentyping main0attheprompt:
cd SOE/srcghci Draw.lhs*Draw> main0
2/26/14 CSE 230, Winter 2011 - Homework #1, Due Friday, January 14th
cseweb.ucsd.edu/classes/wi11/cse230/homeworks/hw1.html 3/7
Youshouldseeawindowwithsomeshapesinit.
NOTE:IfyouhavetroubleinstallingSOE,seethispage
ASSIGNMENT(PART2)
1. TheSierpinskiCarpetisarecursivefigurewithastructuresimilartotheSierpinskiTrianglediscussedinChapter3:
Writeafunction sierpinskiCarpetthatdisplaysthisfigureonthescreen(inthesamefileasyourcodeforPart1).
Notethatyoueitherneedtorunyourprogramin SOE/srcoraddthispathtoGHCssearchpath.Also,theorganizationofSOEhaschangedabit,sothatnowyouuse import SOEinsteadof import SOEGraphics.
2. Writeafunction myFractalwhichdrawsafractalpatternofyourowndesign.Becreative!Theonlyconstraintisthatitshowssomepatternofrecursiveselfsimilarity.
PART3:TRANSFORMINGXMLDOCUMENTS
WARMUP
1. Readchapters5and7ofSOE.2. Doproblem5.2fromSOE(butdonotturnin).3. Doproblems5.3,5.4,5.5,5.6,7.1,and7.2fromSOE,andturnthemisaspartofthesourcecodeyou
createbelow.4. Writethefunction mapintermsof foldr,andturnitininthesameway.
PRELIMINARIES
ThisassignmentinvolvestransformingXMLdocuments.Tokeepthingssimple,wewillnotdealwiththefull
2/26/14 CSE 230, Winter 2011 - Homework #1, Due Friday, January 14th
cseweb.ucsd.edu/classes/wi11/cse230/homeworks/hw1.html 4/7
generalityofXML,orwithissuesofparsing.Instead,wewillrepresentXMLdocumentsasinstancesofthefollowingsimpliedtype:
> data SimpleXML => PCDATA String> | Element ElementName [SimpleXML]> deriving Show> > type ElementName = String
Thatis,a SimpleXMLvalueiseithera PCDATA(parsedcharacterdata)nodecontainingastringorelseanElementnodecontainingatagandalistofsubnodes.
First,downloadtherequiredfilesforthisassignment:hw1.tar.gz.Unpackthefilesandmakesurethatyoucansuccessfullyrunthemainprogram(in Main.hs).Weeprovideda Makefile,whichyoucanuseifyoulike.Youshouldseethisoutput:
Converting... Results differ: WRITE ME! vs A Mi
Next,havealookattheprovidedfiles:
Main.hsisthemainprogramitsjustatestharnessfortherest.Mine.hsisyourpart.Thisiswhereyouwillputthecodeyouwrite,anditstheonlyleyouwillturnin.Atthemoment,itcontainsjuststubs.XMLTypes.hscontainsthetypedenitionsforoursimpliedXMLtrees.Play.hscontainsasampleXMLvalue.ToavoidgettingintodetailsofparsingactualXMLconcretesyntax,wellworkwithjustthisonevalueforpurposesofthisassignment.TheXMLvaluein Play.hshasthefollowingstructure(instandardXMLsyntax):
TITLE OF THE PLAY PERSON1 PERSON2 ... -- MORE PERSONAE TITLE OF FIRST ACT
2/26/14 CSE 230, Winter 2011 - Homework #1, Due Friday, January 14th
cseweb.ucsd.edu/classes/wi11/cse230/homeworks/hw1.html 5/7
TITLE OF FIRST SCENE PERSON1 LINE1 LINE2 ... -- MORE LINES ... -- MORE SPEECHES ... -- MORE SCENES ... -- MORE ACTS
sample.htmlcontainsa(verybasic)HTMLrenditionofthesameinformationas Play.hs.Youmaywanttohavealookatitinyourfavoritebrowser.TheHTMLin sample.htmlhasthefollowingstructure(withwhitespaceaddedforreadability):
TITLE OF THE PLAY Dramatis Personae PERSON1 PERSON2 ... TITLE OF THE FIRST ACT TITLE OF THE FIRST SCENE PERSON1 LINE1 LINE2 ... PERSON2 LINE1 LINE2 ... TITLE OF THE SECOND SCENE PERSON3
2/26/14 CSE 230, Winter 2011 - Homework #1, Due Friday, January 14th
cseweb.ucsd.edu/classes/wi11/cse230/homeworks/hw1.html 6/7
LINE1 LINE2 ...
ASSIGNMENT(PART3)
Writeafunction formatPlaythatconvertsanXMLstructurerepresentingaplaytoanotherXMLstructurethat,whenprinted,yieldstheHTMLspeciedabove(butwithnowhitespaceexceptwhatsinthetextualdataintheoriginalXML).
Themainactionthatweveprovidedwilluseyourfunctiontogenerateale dream.htmlfromthesampleplay.Thecontentsofthisleafteryourprogramrunsmustbecharacterforcharacteridenticaltosample.html.Ourmainactionteststhis.
Important:Thepurposeofthisassignmentisnotjusttogetthejobdonei.e.,toproducetherightHTML.Amoreimportantgoalistothinkaboutwhatisagoodwaytodothisjob,andjobslikeit.Tothisend,yoursolutionshouldbeorganizedintotwoparts:
1. acollectionofgenericfunctionsfortransformingXMLstructuresthathavenothingtodowithplays,plus
2. ashortpieceofcode(asingledenitionoracollectionofshortdenitions)thatusesthegenericfunctionstodotheparticularjoboftransformingaplayintoHTML.
Obviously,therearemanywaystodotherstpart.Themainchallengeoftheassignmentistondacleandesignthatmatchestheneedsofthesecondpart.
Youwillbegradednotonlyoncorrectness(producingtherequiredoutput),butalsoontheeleganceofyoursolutionandtheclarityandreadabilityofyourcodeanddocumentation.Stylecounts.Itisstronglyrecommendedthatyourewritethispartoftheassignmentacoupleoftimes:getsomethingworking,thenstepbackandseeifthereisanythingyoucanabstractoutorgeneralize,rewriteit,thenleaveitaloneforafewhoursorovernightandrewriteitagain.Trytousesomeofthehigherorderprogrammingtechniqueswevebeendiscussinginclass.
SUBMISSIONINSTRUCTIONS
Ifworkingwithapartner,youshouldbothsubmityourassignmentsindividually.IncludeyoursolutionstothewarmupexercisesofPart3in Mine.hs.Makesurethatcompilingandrunning Mainprints Success.Makesure Mine.hsisacceptedbyGHCwithouterrorsorwarnings.Attachbothyour hw1.hsand Mine.hsfilesinanemailto cse230@goto.ucsd.eduwiththesubjectHW1(minusthequotes)thisemailmustbesentfromaucsd.eduemailaddress.Thisaddressisunmonitored!Ifyouhaveanyconcernsorquestions,emailthemtoPat( prondon@cs.ucsd.edu).
2/26/14 CSE 230, Winter 2011 - Homework #1, Due Friday, January 14th
cseweb.ucsd.edu/classes/wi11/cse230/homeworks/hw1.html 7/7
CREDITS
ThishomeworkisessentiallyHomeworks1&2fromUPennsCIS552.
Sitegeneratedwithhakyll