CSCI 320, Homework 1

Collaboration policy in effect for this assignment: SEE last page before starting the assignment.11. Before starting DrRacket Read through the following code and predict what the interpreter will print in
response to evaluation of each of the following expressions. Assume that the sequence is evaluated in the orderin which it is presented here. After jotting down all predictions, check your answers by typing the statementsinto Dr. Racket –record the results.Prediction Actual___________________(- 8 9) _____________________________________(- 7 2 3) (ops can take more than 2 operands) _____________________________________(/ 8 3) (real or int result?) _____________________________________( 3.7 4.4) _____________________________________( 7 3 11) _____________________________________(- (if ( 3 4) 7 10) (/ 16 10)) _____________________________________(define b 13) _____________________________________13 _____________________________________b _____________________________________(define (square x) (* x x)) _____________________________________(square 13) _____________________________________(square b) _____________________________________(square (square (/ b 1.3))) _____________________________________(define a b) _____________________________________(= a b) _____________________________________(if (= (* b a) (square 13))(< a b)(- a b)) _____________________________________(cond ((= a 2) b)((< (square b) (square a)) (/ 1 0))(else (abs (- (square a) b)))) _____________________________________(* 5 (- 2 (/ 4 2) (/ 8 3))) _____________________________________(* 5 (- 2 (/ 4 2)) (/ 8 3)) _____________________________________What do the last two examples tell you in terms of indenting code so that you can “see” the logic?Collaboration policy in effect for this assignment: SEE last page before starting the assignment.2Creating a file in Dr. Racket Since the Dr. Racket Interaction buffer (bottom portion of the environmentwindow) will chronologically list all the expressions you evaluate, and since you will generally have to try morethan one version of the same procedure as part of the coding and debugging process, it is usually better to keepyour procedure definitions in a separate editing buffer (at the top, sometimes call the definitions window), ratherthan to work only in the interaction buffer. You can save this other buffer in a file so you can split your workover more than one session.The basic idea is that you type your programs into the editor buffer, and then use the Check Syntax button tosee if they have correct syntax. The Check Syntax button will also highlight your code with colors indicatingwhich words are function names (by virtue of their position), which are variable names, etc. It will also create avariable flow overlay that shows where variable values come from in a function, and where they are going to.This happens whenever you pass your mouse over the variable name. You don't have to click. NOTE: thefeedback that you get depends on what you have the language level set to.After Check Syntax, you would then click on Run. This loads the editing buffer into the Scheme Interactionbuffer. You can then start running your program by typing into the Interaction buffer.To practice these ideas, go to the empty editing buffer. In this buffer, create a definition for a simple procedure,by typing in the following (verbatim):;; computer square of a number(define (square x) (*x x))Now, click on Check Syntax.If you actually typed in the definition exactly as printed above, you should see a bright red Check Syntax ErrorMessage saying ''*x'' (with no space) has a problem. This is not a syntax error, but DrRacket is warning you that''*x'' is currently undefined (it's a perfectly valid identifier name). Let's see what happens when we try to run thiscode. Click on Run to load this incorrect definition into the Racket Interpreter buffer. You will see that you geta red error message here too. Ignore it and type ''(square 4)'' and return. The interpreter prints''reference to an undefined identifier: square''. At this point we see that the definition ofsquare did not work because of the problem with *x.Edit the definition to insert a space between * and x. Re-Syntax-Check and Re-run the definition. Tryevaluating (square 4) again. Save this working definition along with calls to it to show that it works, in a filecalled hw1-answers.rkt. Each call should be preceded by a comment indicating what the correct result ofthe call would be.You can put all of your later answers in this file to print out or submit.Printing a File FYI, You can print either the definitions buffer or the interactions buffer, under the FILE menu.Stepped Evaluation: Using The Debugger Load the file code0.rkt it into Racket. This simply contains afew definitions and some function calls. After doing a Check-Syntax, press the Debug button. The Debuggerwill automatically process definitions up until the first function call, then wait for you to “step” through theprogram. Press the Step button and watch the window and the stack change as the program is executed or moredefinitions are processed. Make sure that you understand exactly what is happening at each point in theevaluation!!Another Debugging example While you work, you will often need to debug programs.Load the code for this problem set from code1.rkt into the editor buffer. This will load definitions of thefollowing three procedures p1, p2, and p3:Collaboration policy in effect for this assignment: SEE last page before starting the assignment.3(define (p1 x y)(+ (p2 x y) (p3 x y)))(define (p2 z w)(* z w))(define (p3 a b)(+ (p2 a) (p2 b)))Click on Check Syntax. Check out the flow of information display---move your cursor over, for example, thevarious occurrences of p2 in the code. Click on Run to load the code into the interpreter.In the interaction buffer, evaluate the expression (p1 1 2). This should signal an error, with the message:Procedure p2: expects 2 arguments, given 1: 1You can fix the error and re-execute the code. Note: if you fix the code, and forget to click Run and try to rerun(p1 1 2) in the interpreter window, you'll see that DrRacket warns you that you've changed the code buthaven't re-loaded it by printing a yellow-and black warning message "WARNING: The definitions window haschanged. Click Run." Place a fixed version of the code and a call to it in your hw1-answers.rkt file. Makesure that there is a Racket comment right before the function definition and each call to it.Paper exercise:Suppose + were not defined, but the more primitive procedures INC and DEC were. (INC n) returns the integerone greater than n, and (DEC n) returns the number one less than n. The following are two possible definitionsof +, written using INC and DEC and using + recursively.; two different definitions for ‘+’ add two numbers(define (+ a b)(if (= a 0)b(inc (+ (dec a) b))))(define (+ a b)(if (= a 0)b(+ (dec a) (inc b))))Assignment: Write the steps of the evolution of the evaluation of (+ 4 5) for each of the two definitions of +.DO THIS ON A SEPARATE SHEET OF PAPER.For example, the steps of the first version of + for (+ 3 6) are(+ 3 6)(inc (+ 2 6))(inc (inc (+ 1 6)))(inc (inc (inc (+ 0 6))))(inc (inc (inc 6 )))(inc (inc 7))(inc 8)9Note: this is a pencil and paper exercise, no online computing required.Coding exercises - using recursionAny positive number n can be written as a sequence of digits dk dk-1 ...d1 d0, where dk is non-zero. Thus the number n is thesum of di times 10 to the i power, for i = 0 to k. For example, if n = 456, then k is 2 and d2 is 4, d1 is 5, and d0 is 6. A Racketprogram can access the individual digits by using the following basic functions:;; Returns the units digit of the integer n(define (units-digit n)(remainder n 10));; Returns all but the units digit of the integer nCollaboration policy in effect for this assignment: SEE last page before starting the assignment.4(define (all-but-units-digit n)(quotient n 10))With those definitions in place, for example,(unit-digit 456) -- 6 (all-but-units-digit 456) -- 45By combining these functions, you can access the rightmost (units) digit, the second digit, the third digit, etc. ultimatelyreaching the most significant (leftmost) digit. If (all-but-the-units-digit n) is zero, you know n is a one-digitnumber.Using these access functions, define the following functions:1. (decimal-length n) returns k+1, where dk is the leading digit of n. For example,(decimal-length348567) -- 62. (ith-digit n i) returns di of n. For example (ith-digit 671835629 3) -- 53. (leading-digit n) returns dk, the most significant digit. (leading-digit 534) -- 54. (occurances d n) returns the number of times digit d occurs in n. (occurrances 65494576548754987587654655478563) -- 45. (digit-sum n) returns the sum of the digits in n. (digit-sum 2354) -- 14, (digit-sum 8) -- 86. (digital-root n) returns the result of repeatedly applying digit-sum until the result is a singledigit. (digital-root 341943) -- 6 (via 3+4+1+9+4+3=24, then 2+4=6)7. (backwards n) returns the digits in reverse order. You do not need to handle leading or trailing zeros. For thisproblem you may use multiplication. For example: (backwards 329145) -- 541923Add the definition for these routines along with calls that show that they work to hw1-answers.rkt. Do not forget toprovide appropriate documentation of each procedure – minimally, a one-liner explaining what problem is being solved.Hint: Each of these functions should be fairly short and simple. If they get long, re-examine the problem!Include your name on all of your work and in all of your files.Submit hw1-answers.rkt via OAKS by the due date/time.Submit a completed page 1 and separate sheet for the Paper Exercise (page 3) to Dr. McCauley or Vanathi (or you mayscan and upload these as well) by due date/time – see below.Due date/time: Thursday, September 8, 3pmWARNING: Solutions (and the next assignment) will be posted immediatelyafter the due date/time, so late assignments will not be accepted.EARLY submissions are always accepted.Collaboration policy in effect for this assignment: SEE last page before starting the assignment.• You may only submit solutions generated by your own work.• You may discuss DrRacket with classmates and instructors.• You may discuss Racket with classmates and instructors.• You may discuss these problems with classmates and instructors. If it helps you to learn from othershow to solve the problem, that is fine. The objective is that you CAN and DO solve the problem.• You MAY NOT copy the work of others.• You MAY NOT give your work to others.• You should not look for solutions on the internet, you should craft your own.
Powered by