Com S 342 --- Principles of Programming Languages EXERCISE 02: SCHEME CODING STYLE (File $Date: 2005/01/25 06:30:28 $) The purpose of this exercise is for you to learn basic good programming style in Scheme. As with all exercises, this is to be done individually. It is due the day this topic is planned to be discussed in class (see the syllabus: http://www.cs.iastate.edu/~cs342/syllabus.shtml). All of the programs in this exercise run correctly. If not, please contact the course staff for help. 1. [Poor Style I] J. E. is a programmer with excellent productivity but has the belief that short programs are always better. She believes comments are only for people who don't understand programming languages, and that long variable names are likewise superfluous. So, when asked to write a short program to do something, she fired up Notepad and slammed out the following code: ;l337 J girlz yo yo! m4d sk1llz hEr (define sq (lambda (a) (* a a))) (define sqeqsq? (lambda (a b) (cond ( (null? (cdr a)) #f) ((eq? (+ (sq (car a)) (sq (car (cdr a)))) (sq b)) #t ) (else (sqeqsq? (cdr a) b)) )) ) Imagine you are a person in charge of updating J's code (or the staff member charged with grading it) and need to figure out what J was up to. Answer these questions without running your Scheme interpreter and then use the interpreter to verify your answer (if possible). a. What is the type of "sqeqsq?"? That is, of what type must a and b be and what type of value does "sqeqsq?" output? Be as specific as you can (preferably using the type notation for the course, available from the course resources page or directly from http://www.cs.iastate.edu/~cs342/docs/typedscm_toc.html .) Hint: (sqeqsq? '(1 3 4 7) 5) is a good place to start figuring this out. b. What does "sqeqsq?" do? c. What could J. E. have done stylistically to indicate better to you what she was doing (assuming we could convince her to vary from his "short programs are always better" belief)? 2. [Poor Style II] Jacobathan Esthequopolis II believes that you can never have too many comments. So, when asked to write a short program to do something, he fired up DrScheme and crafted the following code: ;;; Function to put not between all the words in an input list. (define even-not-between-words-in-the-list (lambda (a-sentence-one-would-hope) ;; Call subfunction. (put-not-in-right-place a-sentence-one-would-hope 0))) ;;; Function to decrement index before inserting 'not (define put-not-in-right-place (lambda (a-sentence-that-is-indexed const) (cond ;; Check if input sentence is null ((null? a-sentence-that-is-indexed) '()) ((odd? const) (cons (car a-sentence-that-is-indexed) (cons 'not (put-not-in-right-place (cdr a-sentence-that-is-indexed) (+ 1 const))))) ;; Conses not between two words in the sentence (else (cons (car a-sentence-that-is-indexed) (put-not-in-right-place (cdr a-sentence-that-is-indexed) (+ 1 const))))))) ; As per above but different a. What is the type of even-not-between-words-in-the-list? Be as specific as you can. Be as specific as you can (preferably using the type notation for the course). Hint: (even-not-between-words-in-the-list '(I am silly and wordy)) is a good place to start. b. What does even-not-between-words-in-the-list do? c. What could Jacobathan have done stylistically to indicate better to you what he was doing? WHAT TO HAND IN You should have at the beginning of class, written answers to the above questions (or written out questions and problems you encountered for each part). Make sure your name is on these. Attach the printouts, if any, requested above. ADDITIONAL THOUGHTS What happens if you change the name "const" to "cons" in the function put-not-in-right-place? Why?