You must implement three functions from each of the “List,” “Set,” and “Math” se
ID: 3605750 • Letter: Y
Question
You must implement three functions from each of the “List,” “Set,” and “Math” sections below. You must also implement the three functions from the final “Required Functions” section. So twelve (12) functions total are required. In some cases, these functions are already defined in Racket. To avoid confusion, you can name yours differently, for example “myreverse” for “reverse”. Or you can just override the built-in definition. You may use other builtin functions, such as if, cond, car (first), cdr (rest), and cons in your functions. But you may not, of course, use a builtin function in your answer for that same function. (code must be in DrRacket)
You must implement at least three of these list functions:
1. Append two lists (append ’(1 3 x a) ’(4 2 b)) => ’(1 3 x a 4 2 b)
2. Reverse a list (reverse ’(a b c d)) => ’(d c b a)
3. Map a function to every element in a list (define (add3 x) (+ 3 x)) (map add3 ’(1 2 3 4)) => ’(4 5 6 7)
4. Remove duplicates from a list (nub ’(1 1 2 4 1 2 5)) => ’(1 2 4 5)
5. Fold-left (arguments are: initial value, function, list) (fold 10 - ’(1 3 2)) => 4 3
6. Filter (define (lessthan3 x) (< x 3)) (filter lessthan3 ’(1 4 5 2 1 6)) => ’(1 2 1)
7. Merge two sorted lists (merge ’(1 3 4 7) ’(2 3 6)) => ’(1 2 3 3 4 6 7)
8. Add an element to the end of a list. Cool hint: try using reverse (addtoend ’d ’(a b c)) => ’(a b c d)
9. Index of (indexof ’a ’(b c a d)) => 2 (indexof ’a ’(b c d f)) => -1
10. Remove-all (remove-all ’a ’(b a c a a d a)) => ’(b c d)
Set functions use lists to represent sets, but of course sets may not contain duplicate elements. You must implement at least three of these set functions.
1. Set membership (member ’a ’(b c a d)) => #t
2. Insert element into set (insert ’a ’(b c d)) => ’(a b c d) (insert ’a ’(a b c d)) => ’(a b c d)
3. Set intersection (intersection ’(a b c) ’(a c d)) => ’(a c) 4
4. Set union (union ’(a b c) ’(a c d)) => ’(a b c d)
5. Set difference (difference ’(a b c) ’(a c d)) => ’(b) (difference ’(a c d) ’(a b c)) => ’(d)
6. Symmetric difference (disjunctive union) (symdiff ’(a b c) ’(a c d)) => ’(b d)
7. Check if subset or equal () (subset? ’(a b) ’(a b c d)) => #t
8. Check if superset or equal () (superset? ’(a b c d) ’(a b)) => #t
9. Cardinality (cardinality ’(a b c)) => 3
10. Power set (if you need a hint, check Wikipedia) (powerset ’()) => (()) (powerset ’(a b c)) => ’(() (a) (b) (c) (a b) (a c) (b c) (a b c))
You must implement at least three of these math functions.
1. Absolute value (abs 7) => 7 (abs -7) => 7
2. Factorial (factorial 5) => 120
3. Check if 3 integers can be the lengths of the two sides and the hypoteneuse of a right triangle (in that order) (right-tri 3 4 5) => #t (right-tri 1 2 3) => #f
4. Greatest Common Divisor (GCD) (gcd 8 12) => 4
5. Least Common Multiple (LCM) (lcm 4 6) => 12
6. Nth Fibonacci number (the first two Fibonacci numbers are both 1) (nth-fibo 6) => 8 (nth-fibo 10) => 55
7. Test if a number is prime (prime? 5) => #t (prime? 6) => #f
8. Nth prime number (the first prime number is 2) (nth-prime 6) => 13 (nth-prime 26) => 101
You must implement all three of these mathematical functions on integers.
1. Check if a number is perfect: a number is perfect if the sum of its factors other than itself is equal to itself. (perfect? 5) => #f (perfect? 6) => #t
2. Check if a number is abundant: an abundant number’s sum of factors other than itself is greater than the number. (abundant? 5) => #f (abundant? 12) => #t
3. Check if a number is deficient: a deficient number’s sum of factors is less than itself. (deficient? 5) => #t (deficient? 12) => #f
These can all be written very concisely using a functional language like Racket. It will probably be helpful to use some of the functions you implemented from the lists above. .
Explanation / Answer
1.
> (define (myappend l1 l2) (if (null? l1) l2 (cons (car l1) (myappend (cdr l1) l2) ) ))
> (define (myreverse l) (if (null? l) '() (myappend (myreverse (cdr l)) (list (car l) ) )))
> (define (mymap f l) (if (null? l) '() (cons (f (car l)) (mymap f (cdr l) ))))
> (define (addtoend e l) (if (null? l) (list e) (cons (car l) (addtoend e (cdr l)) ) ))
> (define (remove-all e l) (if (null? l) '() (if (equal? e (car l)) (remove-all e (cdr l)) (cons (car l) (remove-all e (cdr l) ) ) )))
> (define (indexof e l) (define (index-Of i e l) (if (null? l) -1 (if (equal? e (car l)) i (index-Of (+ i 1) e (cdr l) ) )) ) (index-Of 0 e l) )
2.
> (define (member e l) (if (null? l) #f (if (equal? e (car l)) #t (member e (cdr l) )) ))
> (define (insert e l) (if (null? l) (list e) (if (member e l) l (cons e l) ) ))
> (define (intersection l1 l2) (if (or (null? l1) (null? l2)) '() (if (member (car l1) l2) (cons (car l1) (intersection (cdr l1) l2)) (intersection (cdr l1) l2)) ))
3.
> (define (myabs e) (if (< e 0) (- 0 e) e ))
> (define (factorial e) (if (= e 0) 1 (* e (factorial (- e 1) ) ) ))
> (define (prime n) (define (prime? n i) (if (= i 1) #f (if (= (modulo n i) 0) #t (prime? n (- i 1) ) ) ) ) (prime? n (- n 1) ) )
4.
> (define (perfect n) (define (getDivisorsSum n i ) (if (= i 0) 0 (if (= (modulo n i) 0) (+ i (getDivisorsSum n (- i 1))) (getDivisorsSum n (- i 1)))))(if (= n (getDivisorsSum n (- n 1))) #t #f ))
>(define (abundant? n) (define (getDivisorsSum n i ) (if (= i 0) 0 (if (= (modulo n i) 0) (+ i (getDivisorsSum n (- i 1))) (getDivisorsSum n (- i 1)))))(if (< n (getDivisorsSum n (- n 1))) #t #f ))
> (define (deficient? n) (define (getDivisorsSum n i ) (if (= i 0) 0 (if (= (modulo n i) 0) (+ i (getDivisorsSum n (- i 1))) (getDivisorsSum n (- i 1)))))(if (> n (getDivisorsSum n (- n 1))) #t #f ))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.