程序代写代做代考 CMSI 185 – Computer Programming

CMSI 185 – Computer Programming
Spring 2017

Homework #4

Part 1

#|

Below is some Racket code. The numbers in the left margin are line numbers. Submit
your hardcopy answers to the questions 1, 2a, 2b, 3a, 3b, 4a, and 4b.

QUESTIONS

1. For each line below, state the line number and describe what that
line does.

2a. What is the purpose of the function “lookup”?
2b. How does the function “lookup” work?

3a. What is the purpose of the function “translate”?
3b. How does the function “translate” work?

4a. What is the purpose of the function “translate_sentence”?
4b. How does the function “translate_sentence? work?

|#

1 #lang racket

2 (require racket/trace)

3 (define ( translate_sentence sentence )

4 ( displayln sentence )

5 ( if [null? sentence]
;then
6 empty
;else
7 (cons ( translate ( first sentence) )
8 ( translate_sentence ( rest sentence) )
)
) )

9 (define ( translate word )

10 (lookup word ‘((window fenetre) (book livre) (computer ordinateur)
11 (house maison) (closed ferme) (pate pate) (liver foie)
12 (faith foi) (weekend (fin de semaine))
13 ((practical joke) attrape) (pal copai)
14 (I Je) (love amore)

)
) )

15 (define ( lookup word dictionary )

16 (cond ([null? dictionary] (cons word ‘(unknown word) ))

17 ([equal? word (first (first dictionary))] (first (rest (first dictionary))))

18 (else (lookup word (rest dictionary)))
) )

;;;TURN ON TRACING
;(trace translate_sentence)

;;;TEST CASES
19 (translate ‘computer)

20 (translate ‘(practical joke))

21 (displayln (translate_sentence
22 ‘(I love Racket faith window liver pate house weekend)))

Part 2

A rational number as you’ve learned in high school actually has two components. The numbers 1/2,
3/4, 2/243, and 323/478 are all rational numbers.

A rational number that can be made by dividing two integers, a/b, where a and b are integers and b is
not zero. Sometimes when a