________ _______ __ __ _______ ___ / ____/ |/ / __ \/ / / / < / __ < / Spring 2019 Section 54 / / / /|_/ / /_/ / / / /_____/ / / / / / / /___/ / / / ____/ /_/ /_____/ / /_/ / / Assignment 03 \____/_/ /_/_/ \____/ /_/\____/_/ Thursday, 14th February, 2018 ASSIGNMENT IS DUE AT THE BEGINNING OF THE NEXT CLASS. ===================================================================================== Before you begin, press CTRL+ALT+T. This will bring up a terminal window, which is a quick way to access your network directory. Once it's open, type: mkdir asmts cd asmts mkdir asmt3 cd asmt3 When you save your work today (which you should do often), you should make sure that you save it in the asmts/asmt3 directory. THIS IS IMPORTANT! ------------------------------------------------------------------------------------- Please provide AT LEAST THREE examples of your function running. Don't forget to use ASMT-HELPER.TXT. Don't forget to use the PROBLEM function before each of your solutions. ------------------------------------------------------------------------------------ Problems may be found in the Problems document on the course website. Remember to provide contracts for your functions, even though they are in the Problems document. 1. Write a function called SQUARE?. It takes as input one integer and outputs #t if this input has an integer square root, else it outputs #f. If the input is negative, it also outputs #f. Don't forget to write a contract for this function. Here are some examples: > (square? 225) #t > (square? -6) #f > (square? 49.0) #t 2. A compass has 8 main directions, each spaced 45 degrees apart: North 0 N North-east 45 NE East 90 E South-east 135 SE South 180 S South-west 225 SW West 270 W North-west 315 NW Write a function called DIRECTION, which takes one input, VAL. Here is a contract for DIRECTION: ;; ============================================================================= ;; DIRECTION ;; ============================================================================= ;; Converts a number in degrees to a compass symbol, and vice versa. ;; ----------------------------------------------------------------------------- ;; INPUT: VAL, an integer OR a symbol. ;; OUTPUT: An integer if VAL is a symbol, or a symbol if VAL is an integer. ;; NOTE: Only the following integers and symbols will be recognised: ;; ;; N, NE, E, SE, S, SW, W, NW ;; 0, 45, 90, 135, 180, 225, 270, 315 ;; ============================================================================= Note that VAL may be either an integer *or* a symbol. If VAL is a number in the table above, DIRECTION should output the corresponding symbol for the direction. If it is not recognised, DIRECTION should output #f. If VAL is a one- or two-letter direction symbol, DIRECTION should output the corresponding integer value. If there is no such symbol, DIRECTION should output #f. If VAL is neither an integer nor a symbol, DIRECTION should output #f. Here are some examples of DIRECTION being used: > (direction 'SE) 135 > (direction 135) SE > (direction 43) #f > (direction 'NT) #f > (direction -54.8) #f > (direction 225) SW > (direction (direction 'NW)) NW HINT: Which is the most fundamental decision to make first? (It is to do with data types.) HINT: This is really good place to use COND. 3. Do Problem 12.1 in the Problem Book. HINT: Any number raised to the power 0 is 1. 4. The function PRINT-SOME is defined as follows: ;; ============================================================= ;; PRINT-SOME ;; ============================================================= ;; Prints something a given number of times. ;; ------------------------------------------------------------- ;; INPUTS: NUM, a non-negative integer. ;; STUFF, a symbol or string. ;; OUTPUTS: None. ;; SIDE-EFFECT: Prints STUFF, NUM times. At the end of the ;; repeated output, print a newline. ;; ============================================================= Here are some examples of PRINT-SOME being used: > (print-some 4 5) 5555 > (print-some 4 '%%%) %%%%%%%%%%%% > (print-some 3 "red lorry yellow lorry, ") red lorry yellow lorry, red lorry yellow lorry, red lorry yellow lorry, > (print-some -1 "banana") > (print-some 6 "hello!") hello!hello!hello!hello!hello!hello! You will need to use recursion to solve this problem. 5. A sequence of numbers which is guaranteed to terminate can be generated using a recursive function ADD-AND-DIVIDE, which takes one integer, NUM, as input. Here is its contract: ;; ============================================================= ;; ADD-AND-DIVIDE ;; ============================================================= ;; Uses side-effect printing to display a recursively generated ;; sequence of integers. ;; ------------------------------------------------------------- ;; INPUTS: NUM, an integer. ;; OUTPUTS: None. ;; SIDE-EFFECT: Prints NUM, followed by a space, at each step; ;; if NUM is less than 2, print "+++ Done." and ;; a newline, then terminate. ;; ============================================================= At each step, ADD-AND-DIVIDE should use side-effect printing to display NUM, and then do one of the following things: 1. If NUM is less than 2, display "+++ Done." and stop. Otherwise: a. If NUM is even, call ADD-AND-DIVIDE with NUM / 2. b. If NUM is odd, call ADD-AND-DIVIDE with NUM + 1. Here are some samples of ADD-AND-DIVIDE in action: > (add-and-divide 27) 27 28 14 7 8 4 2 1 +++ Done. > (add-and-divide 127) 127 128 64 32 16 8 4 2 1 +++ Done. > (add-and-divide 5) 5 6 3 4 2 1 +++ Done. > (add-and-divide -1) -1 +++ Done. > (add-and-divide 1024) 1024 512 256 128 64 32 16 8 4 2 1 +++ Done. > (add-and-divide 1025) 1025 1026 513 514 257 258 129 130 65 66 33 34 17 18 9 10 5 6 3 4 2 1 +++ Done. HINT: Remember that the current input is *always* printed, so that should happen before anything else. ------------------------------------------------------------------------------------------ Don't forget to submit your work using the SUBMIT101 command! submit101 e-asmt3 asmt3 (If the name of your directory is different from 'asmt3', change 'asmt3' to whatever the name of your directory is.) ========================================================================================== ==========================================================================================