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:
- example of truncate: remove the last 2 digits of a number:
- (define (trunc2Digits num) (truncate (/ num 100)) )
- 35533 yields 355
Remainder: remainder after integer division - used to examine the last digits of a number:.
- example of remainder: get the last 2 digits of a number
- (define (remainder2Digits num) (remainder num 100))
- 33354 yields 54
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.
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:
- Literal string: "cat" or 'cat
- Literal list; '("cat", "dog", "horse") or (list "cat", "dog", "horse") or (list 'cat 'dog 'horse) or '('cat 'dog 'horse)
or (cons 'cat (cons 'dog (cons 'horse null)))- First value of list : (first '(3 4 5)) gives 3
- Use this command to pick out the first item in the list in your recursion
- Remove the first item from the list: (drop '( 5 6 7 8 9) 2) gives '(7 8 9)
- Use this command to find rest of the list: (drop '( 5 6 7 8 9) 1)
- Check for empty list: (eq? list null) returns true when list = '() , (list ) or (list null) or '(null)
- Logic Template for recursing through lists looking for a target:
- Handle an empty list without recursing
- Check the first item to see if it meets the condition you are searching for
- If the first item matches, the answer is true
- If the first item does not match, the answer is the result of the function for the rest of the list
- to make the function work for the rest of the list, make the rest of the list by dropping the first item and then call the function with the smaller list
- Racket Template for recursing through a list:
(define (function list)
(cond [ (eq? list null) put what you should return here when the list is empty ]
[ (some test involving first(list) ) put what you should do if the test is true ]
[ else put what you should do if the tests above are false ]))
(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