Com S 342 --- Principles of Programming Languages HOMEWORK 0: GETTING STARTED (File $Date: 1999/08/27 16:53:25 $) Due: problems 1-4, 6-7 at beginning of class, August 31. In this homework, you will get around a bit on the Com S department machines, send us vital information about your login, and get a bit of experience running Scheme and Java. For code hand in *both* your printout of the code and a transcript of testing; handwritten code is *not* acceptable unless the problem specifically states otherwise. The section headings below give the readings related to the problems. COURSE POLICIES AND PROCEDURES (HANDOUT) section 3.6 1. (10 points) a. If you haven't done this already, obtain a Com S department user account. To do this, go to the ``Unix Account Activation Terminal'' in 116 Atanasoff Hall. Then follow the directions on that terminal to get your account. If you have problems with this, contact the System Support Group (ssg@cs.iastate.edu) at 294-0179 or go to their office in 108 Atanasoff Hall. If you cannot complete this by the due date, let us know you are having problems, and hand this problem in as soon as you get your account. b. 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 (i.e., your social security number), your Com S login name, your local phone number (if you have one), and time of the discussion/lab section for which you are registered. The format should be tab-separated, looking like the following example Family Name Given Last 6 login Phone Discussion Sect Leavens, Gary 123456 leavens 294-1580 T 4 We will use this information for grading reports, class lists, and as a means to contact you. (We won't let anyone see your phone number except the course staff.) Also 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, 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. (20 points) This problem is about the course directory structure and the course documents in the docs directory. You need to log in to a Com S department machine to do it. Write brief answers to the following questions on a piece of paper and hand them in. a. Change to the directory /home/course/cs342/public. What are the names of the directories in /home/course/cs342/public? b. One of the directories in /home/course/cs342/public is called docs. Change to that directory. What is in the file ``running-scheme.txt''? (Just give a general description in one sentence.) c. In what file in /home/course/cs342/public/docs are the office hours for the course? What are the office hours of your TA? d. What is in the file ``printing.txt''? e. What is in the file ``getting-to-coms.txt''? f. What is in the file ``policies-complete.txt''? g. When is the final exam for the course, according to ``syllabus.txt''? h. What is the late policy for homework? Where does it say that? i. Do you have to turn in all the problems when a homework is due? Or can you turn in the ones that you have finished and then turn in the rest later? j. How is extra credit work used in grading? Where does it say that? The rest of this homework is found in the file /home/course/cs342/public/homework/hw0.txt or from the course web page http://www.cs.iastate.edu/~leavens/ComS342.html 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. 3. Read the file ``/home/course/cs342/public/docs/reading-news.txt''. Following the instructions, read the news for our class (in the newsgroup isu.coms.342), but ignore all messages from before this semester that may be there. We suggest that you read the newsgroup isu.coms.342 fairly often. Especially if you're working on a homework, as hints are often posted there. In the newsgroup, there should be a message whose subject is: ``The message for HW0, Fall 1999''. After reading the message briefly answer the following questions. a. (5 points) What is more important in Lisp: statements or expressions? a. (5 points) Did McCarthy think that the S-expression notation used in Lisp was ideal? Why or why not? 4. (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 allow Java to access files in the course library, put the following in your ~/.login file. setenv CLASSPATH ".:${PUB}" (If you already have a definition of CLASSPATH in your .login file, then you may need to just add $PUB to the CLASSPATH.) c. To access the programs in the course directory /home/course/cs342/public/bin easily, put the following line in your ~/.login file setenv PATH ${PATH}:/home/course/cs342/public/bin Then log out and log back in for this to take effect. d. To use the ascii editor ``emacs,'' as we encourage you to do, put the lines from $PUB/docs/sample-.emacs at the end of your file ~/.emacs. If you don't already have a file ~/.emacs, then this can be done easily by typing the following command to the Unix shell: cp -i $PUB/docs/sample-.emacs ~/.emacs If this asks if you want to overwrite your .emacs file, respond with ``n'' (without the quotes), and instead edit in our .emacs file with yours. (You must do this, even if you don't (currently) use emacs. It will help us if you come to us for help with homework.) e. Print your files ~/.login and ~/.emacs and hand them in. (These have to be printed on a printer, preferably a Com S department printer, so you know how to use these.) 5. (suggestions only) To make life easier on the Com S Unix machines, you may want to personalize your environment by doing the following. a. To save yourself retyping information about the class for Scheme code, make a file ~/.me.scm containing ;;; Name: ;;; Section: where you fill in the information following the colons as appropriate. (Your section should be the number and time.) b. Similarly for Java code, make a file ~/.me.java containing // Name: // Section: c. 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. This makes pwd give you shorter strings. 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 -i' alias rename 'mv -i' d. The news readers on Unix, and some other programs use a file named ~/.signature. Create a file .signature in your home directory. It should contain your street address, your e-mail address, and (if you wish) your phone number. It should be no more than 4 lines long. e. For more information about details of using the Com S department machines, such as how to forward your mail to/from project Vincent, how to get your files to/from Vincent (use ftp), how to customize your X windows or how to display X windows stuff from a Vincent workstation, use the program ``netscape'' or ``mosaic'' on a Com S department workstation (or use the URL ``http://www.cs.iastate.edu/'') and click on the topic ``How Do I Do That'' in the first list of things, and then when you're in that page, click on the topic ``CS user FAQ'' (which is under the subheading ``Questions and Answers''). (The exact URL is ``http://www.cs.iastate.edu/help/faq.html''.) RUNNING JAVA 6. (10 points) In this problem you will edit and run a simple Java program. You will hand in a printout of the program when you are done. Create a file, let's call it ``SimpleInterpreter.java'' (leave off the quotes), using emacs or your favorite editor. In this file place the following text to define the Java class SimpleInterpreter, filling in your own name and section information (see problem 5b above). // Name: // Section: import lib.SimpleArithExpr; import java.io.*; public class SimpleInterpreter { /** prompt, read, interpret, and print values of expressions. This uses the syntax: ::= | ( ) ::= + | - | * */ public static void main(String argv[]) { BufferedReader inputreader = new BufferedReader(new InputStreamReader(System.in)); while (true) { try { System.out.print("Expression? "); String input = inputreader.readLine(); if (input.equals("quit")) { System.out.println("Bye"); System.exit(0); } else { System.out.println( interpret(SimpleArithExpr.parse(input))); } } catch (Exception e) { System.out.println(e.getMessage()); } } } /** returns the value of exp */ public static int interpret(SimpleArithExpr exp) throws Exception { if (exp.isLiteral()) { return exp.literalToNumber(); } else if (exp.isOpCall()) { String op = exp.opCallToOp(); if (op.equals("+")) { return interpret(exp.opCallToLeftArg()) + interpret(exp.opCallToRightArg()); } else if (op.equals("-")) { return interpret(exp.opCallToLeftArg()) - interpret(exp.opCallToRightArg()); } else if (op.equals("*")) { return interpret(exp.opCallToLeftArg()) * interpret(exp.opCallToRightArg()); } else { return interpret(exp.opCallToLeftArg()) / interpret(exp.opCallToRightArg()); } } throw new Exception("missing case in interpret!"); } } Now compile this code using the command javac -g SimpleInterpreter.java at the Unix prompt. Note: You need to have your CLASSPATH set properly for this to work. If you get an error on line 4 about: Class lib.SimpleArithExpr not found in import. then the problem is that your CLASSPATH environment variable is not set properly. Go back to problem 4b to fix it. The code for SimpleArithExpr is in $PUB/lib/SimpleArithExpr.java. (end Note) Now make a transcript. At the shell prompt in Unix (often %), type the following. script SimpleInterpreter.out this should start a new shell, after giving you a message like ``Script started, file is SimpleInterpreter.out''. Type in to the shell comments to identify yourself: echo Name: echo Section: Then type the following command to start the Java interpreter java SimpleInterpreter to load in the code that you wrote in the file ``SimpleInterpreter.java''. When prompted, enter an expression such as (+ 3 4), (+ 5 (* 7 8)), or the word ``quit'' (without the quotes). Then stop the transcript by typing at the shell prompt: exit You should see a message like ``Script done, file is SimpleInterpreter.out'' that tells you that the shell is no longer making a transcript on the file ``SimpleInterpreter.out''. Hand in a printout of your files ``SimpleInterpreter.java'' and the transcript ``SimpleInterpreter.out''. RUNNING-SCHEME.TXT See the file $PUB/docs/running-scheme.txt for details about running scheme. 7. (10 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), using emacs or your favorite editor. (If you're a vi user, you can try using viper-mode in emacs instead, however, by taking off the semicolon in the place indicated in the sample-.emacs file, and then running emacs.) In this file place the following Scheme procedure definitions, filling in your own name and section information (see problem 5a above). ;;; Name: ;;; Section: (load-from-lib "simple-arith-expr.scm") (define (simple-interpreter) ;; TYPE: (-> () void) ;; EFFECT: prompt, read, interpret, and print values of expressions ;; using the syntax: ;; ::= ;; | ( ) ;; ::= + | - | * (display "Expression? ") (let ((exp (read))) (if (and (symbol? exp) (eq? exp 'quit)) (displayln 'Bye) (begin (displayln (interpret (parse-simple-arith-expr exp))) (simple-interpreter))))) (define (interpret exp) ;; TYPE: (-> (simple-arith-expr) number) ;; ENSURES: result is the value of exp (cond ((literal? exp) (literal->number exp)) ((eq? (op-call->op exp) '+) (+ (interpret (op-call->left-arg exp)) (interpret (op-call->right-arg exp)))) ((eq? (op-call->op exp) '-) (- (interpret (op-call->left-arg exp)) (interpret (op-call->right-arg exp)))) (else (* (interpret (op-call->left-arg exp)) (interpret (op-call->right-arg exp)))))) 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, which are not in other versions of Scheme. If `scheme342' does not work, then your PATH has not be set up properly; go back to problem 4c to fix it. Get help from the course staff if you are having other problems with scheme342. The code for simple-arith-expr.scm is in $PUB/lib/simple-arith-expr.scm. (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 simple-interpreter.scm If you get an error, go back emacs, correct the file, and write it out again. Then try again. Note: see $PUB/lib/chez-scheme-error-messages.txt for how to interpret any error messages you get from Chez Scheme. Type in to Scheme comments to identify yourself: ;;; Name: ;;; Section: After typing the last two lines above, which are comments, Scheme won't prompt you (as it doesn't consider a comment to be real input). You can just keep typing, or enter an expression, such as 342 to get it to prompt you again. 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. However, the typed Scheme interpreter doesn't seem to work well with emacs, as the outputs come after the corresponding inputs they are prompted for. 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, but these sometimes make slightly jumbled transcripts. 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)