Com S 342 --- Principles of Programming Languages HOMEWORK 0: GETTING STARTED (File $Date: 2006/08/21 21:43:27 $) Due: problems 1-2, 3, 8, 10-12 at beginning of class August 29, 2007. In this homework, you will get around a bit on the Com S department machines, send us vital information about your e-mail address, learn how to run Scheme, and learn learn some basic terms and ideas about programming language design, For code hand in *both* your printout of the code and a transcript of testing; handwritten code is *never* acceptable in this class unless the problem specifically states otherwise. The section headings below give the readings related to the problems. COURSE GRADING POLICIES AND PROCEDURES (HANDOUT AND WEB PAGE) 1. (10 points) You need to have an account in the computer science department, where you can read and send e-mail. From this account (just to prove you have it), send email to cs342s@cs.iastate.edu with the subject "HW0, my information" Put in the body of the message the following information: your family name, your given name, the last 4 digits of your University ID number, your email address, and your local phone number). (Your local phone number is optional; we won't let anyone see it except the course staff, but it may help us contact you.) The format should look like the following: Family Name Given Name Last 4 of ID login Phone Leavens, Gary, 3456, leavens@cs.iastate.edu, 294-1580 (Including the commas will help us put this in a grading spreadsheet.) We will use this information for grading reports, class lists, and as a means to contact you. After this information write a list of the languages that you have programmed with previously. Use a format like the following example, but change the list to match your actual experience. I have programmed in: AspectJ, Awk, C, C++, CLU, FORTRAN, Haskell, Java, Lisp, lambda Prolog, OBJ3, Pascal, Perl, Prolog, SML, SNOBOL, SR, Scala, Scheme, Smalltalk, IBM 360 Assembly language, DEC VAX Assembly Language. We will use your list to help us plan the course better, and to tie the course material to your previous experience. Please omit any language you would not feel comfortable programming in. The rest of this homework is found in the file /home/course/cs342/public/homework/hw0.txt which is also on the course web site at: http://www.cs.iastate.edu/~cs342/homework/hw0.txt You should work on-line from this file. You might want to print a copy if you'd rather have it in front of you. 2. (28 points) This problem is about the course directory and the course web site. Type brief answers to the following questions, print, and hand them in. a. On the computer science department Linux machines (which you can reach by using ssh to popeye from off campus), change to the directory /home/course/cs342/public. What are the names of the directories in /home/course/cs342/public? b. In your favorite Web browser, bring up the web page for the course, http://www.cs.iastate.edu/~cs342/index.shtml. From the a "Contacting Us" page, what are the office hours for your instructor? c. Who is the teaching assistants, and what are his office hours? d. Look at the "Grading Policies" page. How much is each test worth as a percentage of your grade? e. What percentage of your final grade is determined by homeworks? If homeworks are worth so little, why should you do them? f. Supposes 10 problems are due at the beginning of lecture on Monday, and you have finished six of them. Should you wait until the seventh is done to hand the other four in? g. What is the penalty for handing in a homework problem by noon of the next day? h. How do extra credit problems fit into your final grade? i. If you e-mail program code to a friend who turns it in, are you cheating also? j. Is it okay to work with someone else on a homework problem and turn in a joint solution? k. Is it okay to find a solution on the internet for a homework problem and turn that in? l. How is extra credit work used in grading a particular homework? m. How should extra credit homework problems be turned in? n. What's on the "Q & A" web page? 3. (10 points) [Setting up DrScheme] Read the "Running Scheme" web page. (If you have a home computer or laptop and have installed DrScheme on it (good idea!), then you need to also follow the directions for downloading and customizing the library on the course library page before proceeding.) Following the directions on the "Running Scheme" web page, set the language in DrScheme to "Typedscm". Then using DrScheme's Language menu, select "Add Teachpack...", and add the teachpack named "drscheme-342-teachpack.scm". After pressing the "Run" button at the top of the DrScheme window, you should see that the "Language" is Typedscm and that the "drscheme-342-teachpack.scm" teachpack is loaded. a. Now evaluate, in the interactions window, evaluate the two forms below, after changing to your own name: (define my-name "") (displayln "My name is " my-name) (Evaluation consists of typing each form into the system, and pressing the enter key.) You should see a line of output after the second of these. (If you get an error, that means you aren't using the Typedscm language, see the "Running Scheme" web page to fix it.) b. Next evaluate the following expression: (test-hw0 "setup") You should also see some output. (If you get an error, that means you either aren't using the Typedscm language, or you haven't selected the course teachpack "drscheme-342-teachpack.scm".) c. Print the interactions window and hand in your printout. 4. (suggested practice) Read the course "Q & A" web page fairly often. If you're working on a homework check it for hints. You can handwrite your answers to the following. 5. (suggested practice) This is problem has a grab-bag of some conveniences for your account for the course. Do the subparts of this problem that seem useful to you. a. To avoid retyping the name of the course public directory, /home/course/cs342/public, we'll use an abbreviation for it. If you use the bash shell, which is the default on the Com S Linux machines, then put the following lines in your ~/.bash_profile file, so that when you type $PUB in the shell it expands to /home/course/cs342/public PUB="/home/course/cs342/public" export PUB To see this working, you'll have to log out and then log back in. Do that, then try the following command, to demonstrate that it's working. ls $PUB (If you use tcsh or csh instead of bash, then put the following line in your ~/.cshrc file, so that when you type $PUB in the shell it expands to /home/course/cs342/public setenv PUB "/home/course/cs342/public" To see this working, you'll have to log out and then log back in. Do that, then try the ls $PUB command, to demonstrate that it's working.) b. To access the programs in the course directory /home/course/cs342/public/bin easily, put the following line at the end of your ~/.bash_profile file (assuming you use bash) PATH="${PATH}:/home/course/cs342/public/bin" export PATH (Or if you use tcsh or csh, put, at the end of your ~/.cshrc file, setenv PATH "${PATH}:/home/course/cs342/public/bin" Then log out and log back in for this to take effect.) c. For this course we recommend that you use the text editor in DrScheme, or if you don't use that, use emacs. If you're already an emacs user, merge the file $PUB/docs/sample-.emacs into your file `~/.emacs'. We suggest you put them at the end of your ~/.emacs file. If you are a vi user, and if you don't already have a file `~/.emacs', make one by typing the following command to the Unix shell: cp -i $PUB/docs/sample-.emacs-for-vi ~/.emacs Otherwise, make your file `~/.emacs' by typing the following command to the Unix shell: cp -i $PUB/docs/sample-.emacs ~/.emacs You must have a ~/.emacs file on the department machines, even if you don't use or plan to use emacs. At the very least, having one will help us if you come to us for help with homework. But emacs is a nice way to edit Scheme code (and also has good support for Java, C++, etc.) 6. (suggested practice) If you aren't a vi user, or if you want to learn emacs, run the emacs tutorial. To do this type the following to the Unix shell: emacs and then either select from the help menu the emacs tutorial, or type the function key f1 followed by the character t. (If that doesn't work, type the escape (ESC) character, followed by a question mark (?), and a t: ESC ? t.) The tutorial is self-paced. If you are a vi user, you may want to read the documentation for Emacs's viper-mode. To do this start emacs and select "Manuals" from the help menu, and then "Browse Manuals with info". On unix, you can select sections from the list by using the middle mouse button, which you can do by clicking both mouse buttons on a PC at the same time. You may want to read about info mode itself first. After orienting yourself in that for a bit, you can go back up (select up at the top of the page) and then read about viper mode from the top menu. (It's also possible to use function key f1 and then i to get into info mode if you can't use the menus.) 7. (suggested practice) To make life easier, you may want to personalize your environment by doing the following. a. To save yourself retyping information about the class for assignments, make a file ~/.me.scm containing (displayln "Name: ") where you fill in your name following the colon as appropriate. b. The file ~/.bashrc can be used to customize the Unix bash shell (see below if you use the csh or tcsh shells). Create this file if it doesn't exist and put in it the following line. alias rm='rm -i' Make sure the file ends in a newline. This says to make the rm command (for removing files) always ask for confirmation; it may save your neck sometime. Aliases can also be used to shorten commands. For example, if you are a DOS user, you can put other lines in the file to alias your favorite DOS commands to their Unix equivalents. For example, you might want to use the following. alias dir=ls alias copy=cp alias rename=mv If you use the csh or tcsh shell instead of the bash shell, then instead of the above you should edit the file ~/.cshrc. Create this file if it doesn't exist and put in it the following line. alias rm 'rm -i' Make sure the file ends in a newline. This says to make the rm command (for removing files) always ask for confirmation; it may save your neck sometime. Aliases can also be used to shorten commands. For example, if you are a DOS user, you can put other lines in the file to alias your favorite DOS commands to their Unix equivalents. For example, you might want to use the following. alias dir ls alias copy cp alias rename mv RUNNING SCHEME WEB PAGE See the "Running Scheme" web page for details about running Scheme. Be sure you got problem 3 to work, or this problem won't work for you. 8. (5 points) In this problem you will edit and run a simple Scheme program. You will hand in a printout of the program when you are done. Open DrScheme and use it to enter following module into the definitions window. Save it into the file ``simple-interpreter-mod.scm'' (in a directory that you own). You can edit using another editor (such as emacs), but we recommend that you do this using DrScheme. (require (lib "simple-arith-expr-mod.scm" "lib342")) (deftype simple-interpreter (-> () void)) (define simple-interpreter (lambda () ;; EFFECT: prompt, read, interpret, and print values ;; of expressions using the syntax: ;; ::= ;; | ( ) ;; ::= + | - | * | / (display "Expression? ") (force-output) (let ((exp (has-type datum (read)))) (if (and (symbol? exp) (eq? exp 'quit)) (displayln 'Bye) (begin (displayln (interpret (parse-simple-arith-expr exp))) (force-output) (simple-interpreter)))))) (deftype interpret (-> (simple-arith-expr) number)) (define interpret (lambda (exp) ;; ENSURES: result is the value of exp (cases simple-arith-expr exp (literal (number) number) (op-call (op left-arg right-arg) (cond ((equal? op '+) (+ (interpret left-arg) (interpret right-arg))) ((equal? op '-) (- (interpret left-arg) (interpret right-arg))) ((equal? op '*) (* (interpret left-arg) (interpret right-arg))) ((equal? op '/) (/ (interpret left-arg) (interpret right-arg))) (else (error "missing case in interpret" op)))) (else (error "bad syntax for input " exp))))) Once you have saved your file, press the "Run" button. Note: For this problem, you will need various features that are provided by Typedscm. If when you press Run, DrScheme complains aobut bits of the syntax that you have typed correctly, check to make sure you are using the Typedscm language (see the "Language:" at the top of the interactions window). If not, then go back to problem 3 above. Get help from the course staff if you are having other problems with DrScheme. (end Note) Now, in the interactions window, call the procedure simple-interpreter by typing at the Scheme prompt in the interactions window: (simple-interpreter) and proceed to, when prompted, enter an expression such as (+ 3 4), (+ 5 (* 7 8)), or the word ``quit'' (without the quotes). Print the transcript of your interactions by printing out the interactions window before you exit DrScheme, and also make a printout of your file ``simple-interpreter.scm'' Hand both of these in. Note: if you are not using DrScheme, see the Running Scheme pages for other ways to run code and make transcripts. 9. (5 points, extra credit) What happens if you try division by 0 in the simple interpreter? What if you divide by 0.0? Look at the Revised(5) Report on Scheme (available from the course Resources web page) and explain what's happening. STRUCTURE AND INTERPRETATION OF COMPUTER PROGRAMS, Sections 1.1.1-1.1.6 REVISED^5 REPORT ON SCHEME SCALA RATIONALE Related to this discussion you should read Sections 1.1.1-1.1.6 of the recommended textbook Structure and Interpretation of Computer Programs, by Abelson and Sussman, which is on reserve and also on-line at http://mitpress.mit.edu/sicp/. For a reference on Scheme, see the Revised^5 Report on Scheme, which is available in several formats from the course resources web page. 10. [design goals] In class, we said that Scheme has three design goals: simplicity, generality, and the ability to manipulate programs as data. a. (10 points) From your experience with C++ (or Java, say which), what do you think its design goals are? b. (10 points) An interesting new programming language is "Scala." What are its main design goals? You can find out by reading the "Scala Rationale" on the web at the URL: http://scala.epfl.ch/docu/files/ScalaRationale.pdf Then answer the above and the following questions, writing no more than a half page for each. Also we'd prefer that you summarize, rather than just quote from this source, as it shows you're thinking. But be sure to set off in quotation marks (" and ") anything that you directly quote in your answer. c. (10 points) How do the design goals of Scala differ from those of Scheme? 11. [means of combination] What does C++ (or Java, say which) use for its means of combination that resemble the following in Scheme: a. (3 points) combinations (i.e., applications of Scheme procedures) b. (3 points) begin expressions c. (4 points) if-expressions of the form (if b e1 e2). Be careful... 12. (10 points) [means of abstraction] Briefly describe two means of abstraction in C++ (or Java, say which). 13. (10 points, extra credit) In the course docs directory, which is also available from the resources web page, you'll find a document, c++-translation.txt, that compares C++ and Scheme. Adapt this to compare Java and Scheme. What are the differences between C++ and Java that make differences here? THE LITTLE SCHEMER 14. (suggested practice) If you have time, start reading "The Little Schemer"!