Com S 342 --- Principles of Programming Languages HOMEWORK 0: GETTING STARTED (File $Date: 2001/09/06 17:59:51 $) Due: problems 1-2, 5, 8 at beginning of class August 30, 2001; problems 10-11 at beginning of class September 6, 2001; problem 12 at beginning of class September 11, 2001. 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 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 on the departmental Unix machines. From this account (just to prove you have it), send email to your TA with the subject "HW0, my information" Put in the body of the message the following information: your family name, your given name, the last 6 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. Also, if you like, you can use a random 6 digit number instead of your University ID number, but remember it.) The format should look like the following: Family Name Given Name Last 6 of ID login Phone Leavens, Gary, 12-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: APL, Ada, Algol W, Awk, BASIC, C, C++, CLU, COBOL, Cecil, FORTRAN, Haskell, Java, Lisp, lambda Prolog, OBJ3, PL/1, Pascal, Perl, Prolog, SML, SNOBOL, SR, 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. 2. (24 points) This problem is about the course directory and the course web site. Write brief answers to the following questions on a piece of paper and hand them in. a. On the department Unix machines, 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 your teaching assistant, and what are your teaching assistant's 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 seven 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 six in? g. What is the penalty for handing in a home or 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. How is extra credit work used in grading? k. How should extra credit homework problems be turned in? l. What's on the "Q & A" web page? The rest of this homework is found in the file /home/course/cs342/public/homework/hw0.txt You should work on-line from this file. (You can access it from the course web page also.) You might want to print a copy if you'd rather have it in front of you. 3. (suggested practice) Read the course "Q & A" web page fairly often. If you're working on a homework check it for hints. 4. (suggested practice) Read the "Running Scheme" web page. If you have a home computer, install Scheme on it. You can handwrite your answers to the following. 5. (10 points) This is to help configure your account for the course. a. To avoid retyping the name of the course public directory, /home/course/cs342/public, you'll need to use an abbreviation for it. Put the following line in your ~/.login 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 following command, to demonstrate that it's working. ls $PUB b. To access the programs in the course directory /home/course/cs342/public/bin easily, put the following line at the end of your ~/.login 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 ``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.) d. Print your files ~/.login and ~/.emacs and hand them in. (These have to be printed on a printer, preferably a Com S department printer.) 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: ") (displayln "Section: , ") where you fill in your name and section information following the colons as appropriate. b. The file ~/.cshrc can be used to customize the Unix (c)shell. Create this file 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 ESSENTIALS OF PROGRAMMING LANGUAGES: CHAPTER 1 and RUNNING SCHEME WEB PAGE See the "Running Scheme" for details about running scheme. 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. Create a file, let's call it ``simple-interpreter.scm'' (leave off the quotes). We recommend that you do this using emacs, or if you're a vi user, using viper-mode in emacs. In this file place the following Scheme procedure definitions, filling in your own name and section information (or insert ~/me.scm from problem 7a above). (displayln "Name: ") (displayln "Section: , ") (load-from-lib "simple-arith-expr.scm") (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 (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))))))) Now start up Scheme, using the command scheme342 at the unix prompt. Note: For this problem, you will need various features that are provided by scheme342 or scm342, which are not in other versions of Scheme. If scheme342 or scm342 don't work for you, go back to problem 5b. The problem is likely to be that your PATH has not be set up properly in problem 5b. Get help from the course staff if you are having other problems with scheme342 or scm342. (end Note) In the scheme interpreter you can play with expressions. For example, if you type lines like the following: 'car (car (list 3 4 5)) something happens. (Try these.) Get out of the Scheme interpreter by typing the following: (exit) Now make a transcript. At the shell prompt in Unix (often %), type the following. script simple-interpreter.out this should start a new shell, after giving you a message like ``Script started, file is simple-interpreter.out''. Then type the command to start the interpreter scheme342 Now type (load "simple-interpreter.scm") to load in the code that you wrote in the file ``simple-interpreter.scm''. If you get an error, go back emacs, correct the file, and write it out again. Then do (load "simple-interpreter.scm") again. Note: If you get an error message of the form: Error in read: unexpected end-of-file at line 43, char 1 of ./simple-interpreter.scm. or "simple-interpreter.scm", line 43: ERROR: list: end of file in ((lambda (exp) (cases simple-arith-expr exp (literal (n ... then your file has either a missing right parenthesis ')' or a missing double quote character ("). If you're working on a Windows PC, it's important to exit the scheme interpreter before fixing this bug, as Windows will not allow you to write the file while Scheme is still reading it... (End of note) After successfully loading the file, call the procedure simple-interpreter by typing at the Scheme prompt (simple-interpreter) and proceed to, when prompted, enter an expression such as (+ 3 4), (+ 5 (* 7 8)), or the word ``quit'' (without the quotes). When you are finished, exit Scheme by typing in the following line: (exit) Then stop the transcript by typing at the shell prompt: exit You should see a message like ``Script done, file is simple-interpreter.out'' that tells you that the shell is no longer making a transcript on the file ``simple-interpreter.out''. Hand in a printout of your files ``simple-interpreter.scm'' and the transcript ``simple-interpreter.out''. Note (other ways to do transcripts): if you are not using our Unix systems, you can produce a transcript in other ways. One way is to run Scheme from inside emacs (use M-x run-scheme), then write out the emacs buffer. Another way is to run Scheme from within a terminal window and to copy and paste the output into a file. If all else fails you can use the transcript-on and transcript-off procedures in Scheme. To do this, start the transcript by using (transcript-on "simple-interpreter.out") and when you are done use (transcript-off) before calling exit. (end of Note) 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. CLASS LANGUAGE DESIGN DISCUSSION Related to this discussion you should read pages 1-16 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] The in-class design of our language (`Simple') had 3 goals: simplicity, generality, and the ability to manipulate programs as data. Read a discussion of the design goals of Java on the web at the URL: http://java.sun.com/docs/white/langenv/ Then answer 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 anything that you directly quote in your answer. a. (10 points) What are the main design goals of Java? b. (10 points) Both our design of Simple and Java have simplicity as a goal. Why doesn't the syntax of Java look like Simple (or Scheme) then? c. (10 points) How do the design goals of Java differ from those of C++? 11. [means of combination] What does Java use for its means of combination that resemble the following in Scheme: a. (4 points) if-expressions of the form (if b e1 e2). Be careful... b. (3 points) begin c. (3 points) combinations (i.e., applications) 12. (10 points) [means of abstraction] Briefly describe two means of abstraction in Java. 13. (10 points, extra credit) In the course docs directory, which is also available from the resources web page, you'll find a document that compares C++ and Scheme. Adapt this to compare Java and Scheme. What are the differences between C++ and Java that make differences here.