PLAI No. 1

1. Define the function "area-square'', which consumes an integer number denoting the length of two sides and produces the area of the square. ;;Q1 ;;area-square: number -> number 【Contract】 ;;to compute the area of a square 【Purpose】 (define (area-square a) (* a a)) ;;【Test】 (test (area-square 2) 4) 2. Define the function ''volume-cuboid'', which consumes three integer numbers denoting lengths of three sides and produces the volume of the cuboid. ;;Q2 ;;volumn-cuboid: number number number -> number 【Contract】 ;;given the length of the three sides to compute the volumn of a cuboid 【Purpose】 (define (volume-cuboid a b c) (* a b c)) ;;【Test】 (test (volume-cuboid 1 2 3) 6) 3. Define the function ''is-multiple-of?'', which consumes two integer numbers and returns whether the first number is a multiple of the second one. ;;Q3 ;;is-multiple-of?: number number -> boolean 【Contract】 ;;consumes two integer numbers and returns whether the first number is a multiple of the second one 【Purpose】 (define (is-multiple-of? x y) (= (/ x y) 2)) ;;【Test】 (test (is-multiple-of? 8 4) true) (test (is-multiple-of? 8 5) false) 4. Define the function ''factorial'', which consumes an integer number and returns the result of the factorial operation. ;;Q4 ;;factorial: number -> number 【Contract】 ;;which consumes an integer number and returns the result of the factorial operation【Purpose】 (define (factorial n) (if (= n 1) 1 (* n (factorial (- n 1))))) ;;【Test】 (test (factorial 4) 24) (test (factorial 5) 120) 5. Define the function ''fibonacci'', which consumes an integer number ''n'' and returns the ''n''-th fibonacci number. The 3rd fibonacci number is 2. ;;Q5 ;;fibonacci: number -> number 【Contract】 ;; consumes an integer number ''n'' and returns the ''n''-th fibonacci number 【Purpose】 (define (fibonacci n) (cond [ (= n 1) 1] [ (= n 2) 1] [else (+ (fibonacci (- n 1)) (fibonacci (- n 2)))])) ;;【Test】 (test (fibonacci 2) 1) (test (fibonacci 3) 2) (test (fibonacci 4) 3) 6. Define the type ''COURSE'', which is either ''CS320'', ''CS311'', or ''CS330''. ''CS320'' has two attributes: ''quiz'' for the number of quizzes and''homework'' for the number of programming assignments. ''CS311'' has one attribute: ''homework'' which is the number too. ''CS330'' has two attributes: ''projects'' for the number of projects and ''homework''. ;;Q6 ;;define the type "COURSE" 【Purpose】 (define-type COURSE [CS320 (homework integer?) (quiz integer?)] [CS311 (homework integer?)] [CS330 (homework integer?) (project integer?)]) (define CS320_Course (CS320 5 6)) (define CS311_Course (CS311 6)) (define CS330_Course (CS330 6 7)) ;;【test】 (test (CS320? CS320_Course) true) (test (CS311? CS311_Course) true) (test (CS330? CS330_Course) true) 7. Define the function ''total-assignments'', which consumes a single course and produces the total number of quizzes, homework, and projects for the given course. ;;Q7 ;;total-assignments: COURSE -> number 【Contract】 ;;consumes a single course and produces thetotal number of quizzes, homework, and projects for the given course【Purpose】 (define (total-assignments to-num) (type-case COURSE to-num [CS320 (l r) (+ l r)] [CS311 (l) l] [CS330 (l r) (+ l r)])) ;;【test】 (test (total-assignments CS320_Course) 11) (test (total-assignments CS311_Course) 6) (test (total-assignments CS330_Course) 13) 8. Define the function ''total-homework'', which consumes a list of courses and produces the total number of homework of the courses in the given list. ;;Q8 ;;total-homework: a list of course -> number 【Contract】 ;;consumes a list of courses and produces the total number of homework of the courses in the given list【Purpose】 (define (total-homework g) (type-case COURSE g [CS320 (l r) l] [CS311 (r) r] [CS330 (l r) l])) (define HomeWorkTotal_1 (+ (total-homework CS320_Course) (total-homework CS311_Course) (total-homework CS330_Course))) (define HomeWorkTotal_2 (+ (CS320-homework CS320_Course) (CS311-homework CS311_Course) (CS330-homework CS330_Course))) (define (is? HomeWorkTotal_1 HomeWorkTotal_2) (= HomeWorkTotal_1 HomeWorkTotal_2)) ;;【test】 (test (is? HomeWorkTotal_1 HomeWorkTotal_2) true) (cons 'COURSE-1 (cons CS320_Course (cons CS311_Course(cons CS330_Course (cons HomeWorkTotal_1 empty))))) 9. Define the function ''my-map'', which consumes a function ''f'' and a list of numbers ''l'', and produces a list of numbers generated by applying the input function ''f'' to each element of ''l''. Do not use the ''map'' function provided by DrRacket. For example, (my-map (lambda (x) (+ 1 x)) (cons 1 (cons 2 (cons 3 empty))))) produces '(2 3 4) ;;Q9 ;;my-map: function list -> list【Contract】 ;;consumes a function ''f'' and a list of numbers ''l'', and produces a list of numbers generated by applying the input function ''f'' to each element of ''l''【Purpose】 (define (my-map f l) (if (null? l) '() (cons (f (car l)) (my-map f (cdr l))))) ;;【test】 (my-map (lambda (x) (+ 1 x)) (cons 1 (cons 2(cons 3 empty))))

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwwyxg.html