Dr. Racket Homework:

Please submit these all in one file. These should all be functions that are no more than a few lines.

texts to read if you need some help

http://picturingprograms.com/ - chapters 1 - 6

OR

http://learnxinyminutes.com/docs/racket/ - a list of functions with examples

OR

http://docs.racket-lang.org/guide/syntax-overview.html - lookup functions you are interested in

 

Simple function definitions

In all functions, avoid defining variables - instead use parameters to the functions instead


1.Write a function convert-3-digits which takes in three numbers representing the hundreds, tens, and ones digits (respectively) of a three-digit number, and returns that three-digit number. For example, (convert-3-digits 5 0 4) should return the number 504.

Hint: remember that the hundreds place is the number * 100. Remember that in racket, * is a function.


2.The nation of Progressiva has a simple tax code. The tax you pay is your salary times the tax rate, and the tax rate is 0.5% per thousand dollars of salary. For example, if you make $40,000, your tax rate is 0.5% times 40, which is 20, so you pay 20% of $40,000, which is $8,000. If you make $40,100, your tax rate is .5% times 40.1 which is 20.05, so you pay 20.05% of 40,100, which is $8040.05.

For testing you can use 0 pay -> 0 net pay; 40000 pay -> 32,000 net pay; 40100 pay -> 32059.95 net pay

Write a function to compute the net pay (i.e. pay after taxes) of a person with a given salary.

Hint: I recommend writing two helper functions as well as net-pay itself. (Try one function to get the rate. Then, another to calculate the tax given a rate and finally another to calculate the net pay given a tax. Look at the answer in moodle for the prior racket temperature homework which shows how to use one function's result as input to another.)

3.Write a function teenage? that takes in a person's age in years and returns a Boolean telling whether the person is a teenager. (do not use if)

Helps:

remember how to use the cond function:

(define (condTester x) (cond [ (< x 7 ) "x is less than 7"]
[ (< x 20) "x is less than 20" ]
[ else "x is 20 or greater"]))

Remember that the last result in the function is its return value, so in this case, it will be a statement like "x is less than 7". To return true or false, just write #f or #t instead of the sentence.

Remember that and & or are functions, so you can use:

(define (condTester2 x) (cond [ (and (> x 7 ) (< x 20)) "x between 20 and 7"]
[ else "x is not between 20 and 7 "]))

 

4.Write a function rough-age that takes in a person's age in years and returns one of the strings "child", "teenager", or "adult" as appropriate.

Hint: you can use your teenager function in this


5.Write a function smallest-of-3 that takes in three numbers and returns the smallest of them. (How many test cases do you need for this?)

Recursion:

6. The fibonacci sequence is a famous bit of mathematics, and it happens to have a recursive definition. The first two values in the sequence are 0 and 1 (essentially 2 base cases). Each subsequent value is the sum of the previous two values, so the whole sequence is: 0, 1, 1, 2, 3, 5, 8, 13, 21 and so on. Define a recursive fibonacci(n) method that returns the nth fibonacci number, with n=0 representing the start of the sequence.

Reminder of a recursion template:

(define (function parameter )
(cond
[ (handle the base case) what happens for the base case ]
[else do the work of his case and then call the function with a different parameter (and that difference will eventually reach the base case) ]))

Note that you can have more than one base case (meaning more than one branch [ ] )

Note that you can have more than one recursive case (again meaning more than one branch [ ] )

(fibonacci 0)
"expect 0 for (fibonacci 0) "
(fibonacci 1)
"expect 1 for (fibonacci 1) "
(fibonacci 2)
"expect 1 for (fibonacci 2) "
(fibonacci 3)
"expect 2 for (fibonacci 3) "
(fibonacci 4)
"expect 3 for (fibonacci 4) "
(fibonacci 5)
"expect 5 for (fibonacci 5) "
(fibonacci 6)
"expect 8 for (fibonacci 6) "

7. Given a non-negative int n, compute recursively (no loops) the count of the occurrences of 8 as a digit, except that an 8 with another 8 immediately to its left counts double, so 8818 yields 4. Remember that the remainder when you divide by 10 is the last digit. The remainder when you divide by 100 is the last 2 digits. You can drop the last digit from a number by dividing by 10 and truncating.

2 Racket commands you will need:

Truncate: remove decimal portion after division - used to chop digits from the end of a number:

Remainder: remainder after integer division - used to examine the last digits of a number:.

How to use Racket functions to move through a number one digit at a time - look only after you tried to use truncate and remainder to figure this out yourself.

Read more about how to code recursive functions:

Recursion Template: See the recursion template in question #6. What is your base case(s)?

Recommendation: First make it work for an 8 = 1 and forget 88 = 3. Only after that works should you add the 88 logic, and if you have a great deal of trouble, turn it in without that.


(count8 11111111111111111)
"expect 0"
(count8 8818)

"expect 4"
(count8 818)
"expect 2"
(count8 8)
"expect 1"
(count8 81818181838189808)
"expect 9"
(count8 8888)
"expect 7"


8.Write a function any-matches? which takes in a string and a list of strings, and tells whether the string matches any of the strings in the list. Do not use the member function - as you are creating a member function.

Helps:

(any-matches "cat" (list "dog" "cat" "bird"))
"expect true to (any-matches cat (list dog cat bird))"
(any-matches 'cat (list 'dog 'hog 'bird))
"expect false to (any-matches cat (list dog hog bird))"
(any-matches 'cat (list 'cat))
"expect true to (any-matches cat (list cat))"
(any-matches 'cat (list 'dog))
"expect false to (any-matches cat (list dog))"
(any-matches 'cat (list ))
"expect false to (any-matches cat (list ))"
(any-matches 'cat null)
"expect false to (any-matches cat null))"
(any-matches 'cat '() )
"expect false to (any-matches cat ()))"
(any-matches 'cat '(null ) )
"expect false to (any-matches cat (null)))"
(any-matches 'cat (list null ) )
"expect false to (any-matches cat (null)))"

(any-matches 'cat (cons 'cat (cons 'dog (cons 'horse null))))
"expect true to (any-matches 'cat (cons 'cat (cons 'dog (cons 'horse null))))"


9. Write a function count-matches which takes in a string and a list of strings, and tells how many list elements match the string (possibly 0).

(count-matches "cat" (list "dog" "cat" "cat" "bird" "cat"))
"expect 3 to (count-matches cat (list dog cat cat bird cat)) "
(count-matches 'cat (list 'dog 'hog 'bird))
"expect 0 to (count-matches 'cat (list 'dog 'hog 'bird))"
(count-matches 'cat (list 'cat))
"expect 1 to (count-matches cat (list cat))"
(count-matches 'cat (list 'dog))
"expect 0 to (count-matches cat (list dog))"
(count-matches 'cat (list ))
"expect 0 to (count-matches cat (list ))"

credited to : Dr. Stephen Bloch