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.
Reading:
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.1 Tocompletethisassignment,youwillneedtodownloadandinstallacopyof Dr Racket (http://racket-lang.org/download/) on your local PC.
1.2 StarttheprogramDrRacket.
1.3 Choosethe“R5RS”fromthelanguagemenu:
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)
(write (read))
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
7) Input two integers: (* (read) (read))
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
Programming Exercises (GRADED)
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.
Use your conditionals
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!
(ReadAndMult) >6
>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
(read-int-list)
5
6
7
8
q
(5 6 7 8)
Use this with your sum-num-list:
(sum-num-list (read-int-list))
5 6 7 8 q 26
(sum-num-list (read-int-list))
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
22 +02 =4Sad
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!