# Scheme代写 | CSE 240 Homework 5

50 Points
Introduction
The aim of this assignment is to make sure that you understand and are familiar with the concepts covered in the lectures. By the end of the assignment, you should have
 Understood the concepts of functional programming paradigm.
 Written functional programs in Dr. Racket Scheme.
 Understood names and procedures in functional programming paradigm.
Text Chapter 4 and course notes (slides). This is a complete new language, and you need to spent more time to read and to program.
Practice Exercises (non-graded, no submission required) Tutorial: Getting Started with DrRacket
1.2 StarttheprogramDrRacket.
DrRacket menu: language →choose language →Other Languages – R5RS.
1.4 Enteryourprograms/formsintheupperwindowofDrRacketandclickonthe
“run” button to execute your programs/forms, e.g., enter:
(display “hello world”)
(newline)
(display (+ (* 3 8) 10))
(- 20 5)
(display (read)) ; input a number from keyboard
(write “hello world”)
(newline)
(write (+ (* 3 8) 10))
(- 20 5)
Click on run, the following results should appear in the lower window:
hello world
34
15

Use DrRacket to calculate the following expressions/forms.
1)(3 + (5 + (7 + (9 + (11 + 13))))). 2)(((((3 + 5) + 7) + 9) + 11) + 13) 3) ((2+4)+(3+5)+(6+8))
4)(2 + 4 + 6 + 8 + 10 + 12)
5)(2 + 3 * 5 + 4 * 6 + 7)
6) 125187
Write Scheme programs/forms to:
(1) Find the second element of the list ‘(2 4 6 8 10 12). Your form should work for any list containing two or more elements.
(2) Find the last element of the list ‘(2 4 6 8 10 12). Your form only needs to work for lists of six elements.
(3) Merge the two lists ‘(1 2 3 4) and ‘(5 7 9) into a single list ‘(1 2 3 4 5 7 9)
(4) Obtain the length of the list ‘(a b x y 10 12)
(5) Check whether ‘(+ 2 4) is a symbol
(6) Check whether ‘+ is a member of the list ‘(+ 3 4 6) (7) Check whether “+”, ‘(+ 3 5), “(* 4 6)” are strings
(8) Check whether (* 3 5), ‘(/ 3 7), (1 2 3 4), “(+ 2 8) and “(1 2 3)” are strings

Using Scheme
In this assignment, you will be learning Scheme through the use of Dr. Racket. We would like to start with some basic concepts; trying to under prefix notation and the use procedure in Scheme. You will also implement nested procedures and recursive procedures.
Convert to Scheme and Run:
Using Dr. Racket & R5RS Scheme to compute the following expressions.
1) 3 + 5 – 7
2) 2 * ( 8 + 5 + 4 ) – 25
3) 10 – ( ( 3 * 5 ) + ( 2 + ( 0 * 5 ) ) )
4) 5 * ( 4 + ( ( ( 10 + 10 ) + ( 5 * 8 ) ) / ( 10 + 2 ) ) )
5) ( ( ( ( ( ( 3 + 5 ) * ( 6 + 4 ) ) / 2 ) / 2 ) – 5 ) / 3) + ( ( ( ( 2 * 10 ) +
(5*4))/2)+(4*5))
Create a function for each called run1, run2, run3, run4 and run5 Use a lambda statement that takes no parameters.
(define (lambda () …))
Multiplication
A) Write a function called RecursiveMultiply that takes two parameters and calculates the multiplication of the two.
(RecursiveMultiply 6 2)
12
Hint:
 Remember multiplication is just adding over and over and over.
 Let your return do the work for you with negatives
B) Create a second function called ReadAndMult that uses your Recursive multiplication but takes two reads of input from the user. Make sure you confirm that the two values are numbers!
>2
> 12
Hint: Remember, you don’t have to print the result, just return it.

Sum Number List
Part 1 – Sum-num-list building up the logic
Our goal here is to make a series of functions that will help you solve a problem. I’m going to walk you through the analysis bit by bit.
Goal:
Problems:
We are explicitly trying to code a robust, type-safe function to sum a list of integers.
The function should return the sum of the list if it is a homogenous list of integers otherwise it should return false.
Problems to solve:
1) What is the sum of this list of numbers? a. Is this a homogenous list of numbers?
i. Is this item a number? – this is solved for us (number? …)
Part A:
Write a function named number-list? that takes a list and returns #t or #f that it is a list of ONLY numbers.
(number-list? ‘(1 2 3 4))  #t (number-list? ‘(1 2 (3) 4)  #f (number-list? ‘(1 2 a 4)  #f
Part B:
Create the function sum-num-list to make use of your number-list? function and sum the number list.
Note: you can/should use a helper function to do the work and use sum-num-list as the interface function that the user would use.
(sum-num-list ‘(1 2 3 4 5))  15 (sum-num-list ‘(1 (2)))  #f (sum-num-list ‘(a b c))  #f
(sum-num-list ‘(1 2 3 4 5))  15 (sum-num-list ‘(1 (2)))  #f (sum-num-list ‘(a b c))  #f

Part C:
Now to make an input function.
Create a function called read-int-list. This function will continuously read values from the user until a q is entered.
Note: this function will need to use the (let …) statement since you will need to capture the input in a local identifier.
Note: when you check for the input being “q” you’ll check for the symbol q – that is ‘q
5
6
7
8
q
 (5 6 7 8)
5 6 7 8 q  26
a b c d q  #f
Using Map
Create a function called PairOff that takes two parameters and returns a list containing those two parameters
(PairOff 1 ‘a)
> (1 a)
Use the map higher order function to create a second function called Combiner that takes two lists and returns a list of the items contained with paired together as sub-lists
(Combiner ‘(1 2 3) ‘(a b c))
> ((1 a) (2 b) (3 c))

Don’t worry be happy
A happy number is defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits in base-ten, and repeat the process until the number either equals 1 (where it will stay), or it loops endlessly in a cycle that does not include 1. Those numbers for which this process ends in 1 are happy numbers, while those that do not end in 1 are unhappy numbers (or sad numbers).
For example, 19 is happy, as the associated sequence is
12 +92 =82,
82 +22 =68,
62 +82 =100, 12 +02 +02 =1.
If the cycle gets into this loop, then the number is not happy!
4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4 →
It’s safe to say — If the cycle hits the value 4 at any time then it’s an Sad Number.
Is 91 happy?
92 +12 =82
82 +22 =20
Create a function happy? that takes one parameter and returns #t or #f if the number is happy!
(Yes, you can/should use certain code made earlier in this assignment)
(happy? 863)  #t (happy? 55562)  #f
Hints:
 write a function to take apart a number into a list of digits
 (quotient …) gives the result of integer division
 (modulo …) is how we do modulus (% operator) in scheme
 You might use a higher-order function such as apply or map to help out
REMOVE ALL TEST CASES BEFORE SUBMITTING YOUR SCHEME FILE!

Testing Script:
(download the plain text file provided with the assignment – then copy paste this to the bottom of your file – if you can pass all these tests, you should be in the clear for grading)
(display “Testing Convert and Run”)(newline) (display “(run1) – answer should be 1 – tested: “) (run1)
(display “(run2) – answer should be 9 – tested: “) (run2)
(display “(run3) – answer should be -7 – tested: “) (run3)
(display “(run4) – answer should be 45 – tested: “) (run4)
(display “(run5) – answer should be 45 – tested: “) (run5)
(display “Testing RecursiveMultiply”)(newline)
(display “(RecursiveMultiply 6 2) – expected 12 – got: “) (RecursiveMultiply 6 2)
(display “(RecursiveMultiply -6 -2) – expected 12 – got: “) (RecursiveMultiply -6 -2)
(display “(RecursiveMultiply 6 -2) – expected -12 – got: “) (RecursiveMultiply 6 -2)
(display “(RecursiveMultiply -6 2) – expected -12 – got: “) (RecursiveMultiply -6 2)
(display “30 <– correct answer”) (newline)
(display “Testing number-list?”)(newline)
(display “(number-list? ‘(1
(number-list? ‘(1 2 3 4))
(display “(number-list? ‘(1
(number-list? ‘(1 2 (3) 4))
(display “(number-list? ‘(1
(number-list? ‘(1 2 a 4))
2 3 4) – epxected #t – got: “)
2 (3) 4) – expected #f – got: “)
2 a 4) – expected #f – got: “)
(newline)
(display “Testing sum-number-list”)(newline)
(display “(sum-number-list ‘(1 (sum-number-list ‘(1 2 3 4 5)) (display “(sum-number-list ‘(1 (2))) (sum-number-list ‘(1 (2)))
(display “(sum-number-list ‘(a b c)) (sum-number-list ‘(a b c))
“) “) “)
(newline)
(display “Input 1 2 3 4 5 q –> “)
(display “(1 2 3 4 5) <– expected”)(newline)(newline) (display “Testing (sum-numberlist (read-int-list)”)(newline) (display “Input 1 2 3 4 5 q –> “)
(display “15 <– expected”)
(newline)
(display “Testing PairOff & Combiner”)(newline)
(display “(PairOff 1 ‘a) – expected (1 a) – got: “) (PairOff 1 ‘a)
(display “(Combiner ‘(1 2 3) ‘(a b c)) – expected: ((1 a) (2 (Combiner ‘(1 2 3) ‘(a b c))
b) (3 c)) – got: “)
2 3 4 5) – expected 15 – got:
– expected #f – got:
(newline)
(newline)
(display ” o(^▽^)o HAPPY NUMBER TEST o(;△;)o “)
(newline)
(display “Is 863 happy? (happy? 863) – expected: #t – got: “) (happy? 863)
(display “Is 55562 happy? (happy? 55562) – expected #f – got: “) (happy? 55562)
– expected #f – got:

(1) Compile the code. If it does not compile you will receive a U on the
Specifications in the Rubric
(2) The TA will read your program and give points based on the points allocated to each component, the readability of your code (organization of the code and comments), logic, inclusion of the required functions, and correctness of the implementations of each function.
Rubric:
What to Submit?
You are required to submit your solutions in a compressed format (.zip). Zip all files into a single zip file. Make sure your compressed file is labeled correctly – lastname_firstname_hw5.zip.
For this home assignment, the compressed file MUST contain the following:
Lastname_hw5.rkt (Scheme program)
No other files should be in the compressed folder.
If multiple submissions are made, the most recent submission will be graded, even if the assignment is submitted late.
Where to Submit?
All submissions must be electronically submitted to the respected homework link in the course web page where you downloaded the assignment.

You are encouraged to cooperate in study group on learning the course materials. However, you may not cooperate on preparing the individual assignments. Anything that you turn in must be your own work: You must write up your own solution with your own understanding. If you use an idea that is found in a book or from other sources, or that was developed by someone else or jointly with some group, make sure you acknowledge the source and/or the names of the persons in the write-up for each problem. When you help your peers, you should never show your work to them. All assignment questions must be asked in the course discussion board. Asking assignment questions or making your assignment available in the public websites before the assignment due will be considered cheating.
The instructor and the TA will CAREFULLY check any possible proliferation or plagiarism. We will use the document/program comparison tools like MOSS (Measure Of Software Similarity: http://moss.stanford.edu/) to check any assignment that you submitted for grading. The Ira A. Fulton Schools of Engineering expect all students to adhere to ASU’s policy on Academic Dishonesty. These policies can be found in the Code of Student Conduct:
ALL cases of cheating or plagiarism will be handed to the Dean’s office. Penalties include a failing grade in the class, a note on your official transcript that shows you were punished for cheating, suspension, expulsion and revocation of already awarded degrees. E-mail: [email protected]  微信:itcsdx 