├── 00-Prologue └── UFO-Landing.rkt ├── 01-Fixed-Size-Data ├── 01-Arithmetic │ ├── Exercise-01.rkt │ ├── Exercise-02.rkt │ ├── Exercise-03.rkt │ ├── Exercise-04.rkt │ ├── Exercise-05.rkt │ ├── Exercise-06.rkt │ ├── Exercise-07.rkt │ ├── Exercise-08.rkt │ ├── Exercise-09.rkt │ ├── Exercise-10.rkt │ └── images │ │ └── cat.png ├── 02-Functions-and-Programs │ ├── Exercise-11.rkt │ ├── Exercise-12.rkt │ ├── Exercise-13.rkt │ ├── Exercise-14.rkt │ ├── Exercise-15.rkt │ ├── Exercise-16.rkt │ ├── Exercise-17.rkt │ ├── Exercise-18.rkt │ ├── Exercise-19.rkt │ ├── Exercise-20.rkt │ ├── Exercise-21.rkt │ ├── Exercise-22.rkt │ ├── Exercise-23.rkt │ ├── Exercise-24.rkt │ ├── Exercise-25.rkt │ ├── Exercise-26.rkt │ ├── Exercise-27.rkt │ ├── Exercise-28.rkt │ ├── Exercise-29.rkt │ ├── Exercise-30.rkt │ └── Exercise-31.rkt ├── 03-How-to-Design-Programs │ ├── Exercise-34.rkt │ ├── Exercise-35.rkt │ ├── Exercise-36.rkt │ ├── Exercise-37.rkt │ ├── Exercise-38.rkt │ ├── Exercise-39.rkt │ ├── Exercise-40.rkt │ ├── Exercise-41.rkt │ ├── Exercise-42.rkt │ ├── Exercise-43-1.rkt │ ├── Exercise-43-2.rkt │ ├── Exercise-44.rkt │ ├── Exercise-45.rkt │ ├── Exercise-46.rkt │ ├── Exercise-47.rkt │ └── images │ │ ├── cat1.png │ │ ├── cat2.png │ │ └── cat3.png ├── 04-Intervals-Enumerations-Itemizations │ ├── Exercise-48.rkt │ ├── Exercise-49-1.rkt │ ├── Exercise-49-2.rkt │ ├── Exercise-50.rkt │ ├── Exercise-51.rkt │ ├── Exercise-52.rkt │ ├── Exercise-53.rkt │ ├── Exercise-54.rkt │ ├── Exercise-55.rkt │ ├── Exercise-56.rkt │ ├── Exercise-57.rkt │ ├── Exercise-58.rkt │ ├── Exercise-59.rkt │ ├── Exercise-60.rkt │ ├── Exercise-61.rkt │ ├── Exercise-62.rkt │ ├── UFO.rkt │ └── images │ │ └── rocket.png ├── 05-Adding-Structure │ ├── Exercise-63.rkt │ ├── Exercise-64.rkt │ ├── Exercise-65.rkt │ ├── Exercise-66.rkt │ ├── Exercise-67.rkt │ ├── Exercise-68.rkt │ ├── Exercise-69.rkt │ ├── Exercise-70.rkt │ ├── Exercise-71.rkt │ ├── Exercise-72.rkt │ ├── Exercise-73.rkt │ ├── Exercise-74.rkt │ ├── Exercise-75.rkt │ ├── Exercise-76.rkt │ ├── Exercise-77.rkt │ ├── Exercise-78.rkt │ ├── Exercise-79.rkt │ ├── Exercise-80.rkt │ ├── Exercise-81.rkt │ ├── Exercise-82.rkt │ ├── Exercise-83.rkt │ ├── Exercise-84.rkt │ ├── Exercise-85.rkt │ ├── Exercise-86.rkt │ ├── Exercise-87.rkt │ ├── Exercise-88.rkt │ ├── Exercise-89.rkt │ ├── Exercise-90.rkt │ ├── Exercise-91.rkt │ ├── Exercise-92.rkt │ ├── Exercise-93.rkt │ └── images │ │ ├── cat1.png │ │ ├── cat2.png │ │ ├── cat3.png │ │ ├── cham-v2-1.png │ │ ├── cham-v2-2.png │ │ ├── cham-v3-1.png │ │ ├── cham-v3-2.png │ │ ├── diagrams.png │ │ └── resources │ │ └── diagrams.html └── 06-Itemizations-and-Structures │ ├── Exercise-101.rkt │ ├── Exercise-102.rkt │ ├── Exercise-103.rkt │ ├── Exercise-104.rkt │ ├── Exercise-105.rkt │ ├── Exercise-106.rkt │ ├── Exercise-107.rkt │ ├── Exercise-108.rkt │ ├── Exercise-109.rkt │ ├── Exercise-110.rkt │ ├── Exercise-111.rkt │ ├── Exercise-112.rkt │ ├── Exercise-113.rkt │ ├── Exercise-114.rkt │ ├── Exercise-115.rkt │ ├── Exercise-94.rkt │ ├── Exercise-97.rkt │ ├── Space-Invader.rkt │ └── images │ ├── cat1.png │ ├── cat2.png │ ├── cat3.png │ ├── cham-v3-1.png │ ├── cham-v3-2.png │ ├── stop.png │ └── walk.png ├── 01-Intermezzo ├── Exercise-116.rkt ├── Exercise-117.rkt ├── Exercise-118.rkt ├── Exercise-119.rkt ├── Exercise-120.rkt ├── Exercise-121.rkt ├── Exercise-122.rkt ├── Exercise-123.rkt ├── Exercise-124.rkt ├── Exercise-125.rkt ├── Exercise-126.rkt ├── Exercise-127.rkt └── Exercise-128.rkt ├── 02-Arbitrarily-Large-Data ├── 08-Lists │ ├── Exercise-129.rkt │ ├── Exercise-130.rkt │ ├── Exercise-131.rkt │ ├── Exercise-132.rkt │ ├── Exercise-133.rkt │ ├── Exercise-134.rkt │ ├── Exercise-135.rkt │ └── Exercise-136.rkt ├── 09-Designing-with-Self-Referential-Data-Definitions │ ├── Exercise-137.rkt │ ├── Exercise-138.rkt │ ├── Exercise-139.rkt │ ├── Exercise-140.rkt │ ├── Exercise-141.rkt │ ├── Exercise-142.rkt │ ├── Exercise-143.rkt │ ├── Exercise-144.rkt │ ├── Exercise-145.rkt │ ├── Exercise-146.rkt │ ├── Exercise-147.rkt │ ├── Exercise-148.rkt │ ├── Exercise-149.rkt │ ├── Exercise-150.rkt │ ├── Exercise-151.rkt │ ├── Exercise-152.rkt │ ├── Exercise-153.rkt │ ├── Exercise-154.rkt │ ├── Exercise-155.rkt │ ├── Exercise-156.rkt │ ├── Exercise-157.rkt │ ├── Exercise-158.rkt │ ├── Exercise-159.rkt │ └── Exercise-160.rkt ├── 10-More-on-Lists │ ├── Editor.rkt │ ├── Exercise-161.rkt │ ├── Exercise-162.rkt │ ├── Exercise-163.rkt │ ├── Exercise-164.rkt │ ├── Exercise-165.rkt │ ├── Exercise-166-1.rkt │ ├── Exercise-166-2.rkt │ ├── Exercise-167.rkt │ ├── Exercise-168.rkt │ ├── Exercise-169.rkt │ ├── Exercise-170.rkt │ ├── Exercise-171.rkt │ ├── Exercise-172.rkt │ ├── Exercise-173.rkt │ ├── Exercise-174.rkt │ ├── Exercise-175.rkt │ ├── Exercise-176.rkt │ ├── Exercise-177.rkt │ ├── Exercise-178.rkt │ ├── Exercise-179.rkt │ ├── Exercise-180.rkt │ └── files │ │ └── ttt.txt ├── 11-Design-by-Composition │ ├── Exercise-181.rkt │ ├── Exercise-182.rkt │ ├── Exercise-183.rkt │ ├── Exercise-184.rkt │ ├── Exercise-185.rkt │ ├── Exercise-186.rkt │ ├── Exercise-187.rkt │ ├── Exercise-188.rkt │ ├── Exercise-189.rkt │ ├── Exercise-190.rkt │ ├── Exercise-191.rkt │ ├── Exercise-192.rkt │ ├── Exercise-193-1.rkt │ ├── Exercise-193-2.rkt │ └── Exercise-194.rkt └── 12-Projects-Lists │ ├── Exercise-195.rkt │ ├── Exercise-196.rkt │ ├── Exercise-197.rkt │ ├── Exercise-198-1.rkt │ ├── Exercise-198-2.rkt │ ├── Exercise-199.rkt │ ├── Exercise-200.rkt │ ├── Exercise-201.rkt │ ├── Exercise-202.rkt │ ├── Exercise-203.rkt │ ├── Exercise-204.rkt │ ├── Exercise-205.rkt │ ├── Exercise-206.rkt │ ├── Exercise-207.rkt │ ├── Exercise-208.rkt │ ├── Exercise-209.rkt │ ├── Exercise-210.rkt │ ├── Exercise-211.rkt │ ├── Exercise-212.rkt │ ├── Exercise-213.rkt │ ├── Exercise-214.rkt │ ├── Exercise-215.rkt │ ├── Exercise-216.rkt │ ├── Exercise-217.rkt │ ├── Exercise-218.rkt │ ├── Exercise-219.rkt │ ├── Exercise-220.rkt │ ├── Exercise-221.rkt │ ├── Exercise-222.rkt │ ├── Exercise-223.rkt │ ├── Exercise-224.rkt │ ├── Exercise-226.rkt │ ├── Exercise-227.rkt │ ├── Exercise-228.rkt │ ├── Exercise-229.rkt │ ├── Exercise-230.rkt │ └── files │ └── itunes.xml ├── 02-Intermezzo ├── Exercise-231.rkt ├── Exercise-232.rkt ├── Exercise-233.rkt └── Exercise-234.rkt ├── 03-Abstraction ├── 14-Similarities-Everywhere │ ├── Exercise-235.rkt │ ├── Exercise-236.rkt │ ├── Exercise-237.rkt │ ├── Exercise-238.rkt │ ├── Exercise-239.rkt │ ├── Exercise-240.rkt │ ├── Exercise-241.rkt │ ├── Exercise-242.rkt │ ├── Exercise-243.rkt │ ├── Exercise-244.rkt │ ├── Exercise-245.rkt │ ├── Exercise-246.rkt │ ├── Exercise-247.rkt │ ├── Exercise-248.rkt │ └── Exercise-249.rkt ├── 15-Designing-Abstractions │ ├── Exercise-250.rkt │ ├── Exercise-251.rkt │ ├── Exercise-252.rkt │ ├── Exercise-253.rkt │ ├── Exercise-254.rkt │ └── Exercise-255.rkt ├── 16-Using-Abstractions │ ├── Exercise-256.rkt │ ├── Exercise-257.rkt │ ├── Exercise-258.rkt │ ├── Exercise-259.rkt │ ├── Exercise-260.rkt │ ├── Exercise-261.rkt │ ├── Exercise-262.rkt │ ├── Exercise-267.rkt │ ├── Exercise-268.rkt │ ├── Exercise-269.rkt │ ├── Exercise-270.rkt │ ├── Exercise-271.rkt │ ├── Exercise-272.rkt │ ├── Exercise-273.rkt │ ├── Exercise-274.rkt │ ├── Exercise-275.rkt │ ├── Exercise-276.rkt │ ├── Exercise-277.rkt │ └── Exercise-278.rkt └── 17-Nameless-Functions │ ├── Exercise-279.rkt │ ├── Exercise-280.rkt │ ├── Exercise-281.rkt │ ├── Exercise-282.rkt │ ├── Exercise-283.rkt │ ├── Exercise-284.rkt │ ├── Exercise-285.rkt │ ├── Exercise-286.rkt │ ├── Exercise-287.rkt │ ├── Exercise-288.rkt │ ├── Exercise-289.rkt │ ├── Exercise-290.rkt │ ├── Exercise-291.rkt │ ├── Exercise-292.rkt │ ├── Exercise-293.rkt │ ├── Exercise-294.rkt │ ├── Exercise-295.rkt │ ├── Exercise-297.rkt │ ├── Exercise-298.rkt │ ├── Exercise-299.rkt │ └── images │ └── rocket.png ├── 03-Intermezzo ├── Exercise-300.rkt ├── Exercise-301.rkt ├── Exercise-302.rkt ├── Exercise-303.rkt ├── Exercise-304.rkt ├── Exercise-305.rkt ├── Exercise-306.rkt ├── Exercise-307.rkt ├── Exercise-308.rkt └── Exercise-309.rkt ├── 04-Intermezzo ├── Exercise-412.rkt ├── Exercise-413.rkt ├── Exercise-414.rkt ├── Exercise-415.rkt ├── Exercise-416.rkt ├── Exercise-417.rkt ├── Exercise-418.rkt ├── Exercise-419.rkt └── Exercise-420.rkt ├── 04-Intertwined-Data ├── 19-The-Poetry-of-S-expressions │ ├── Exercise-310.rkt │ ├── Exercise-311.rkt │ ├── Exercise-312.rkt │ ├── Exercise-313.rkt │ ├── Exercise-314.rkt │ ├── Exercise-315.rkt │ ├── Exercise-316.rkt │ ├── Exercise-317.rkt │ ├── Exercise-318.rkt │ ├── Exercise-319.rkt │ ├── Exercise-320.rkt │ ├── Exercise-321.rkt │ ├── Exercise-322.rkt │ ├── Exercise-323.rkt │ ├── Exercise-324.rkt │ ├── Exercise-325.rkt │ ├── Exercise-326.rkt │ ├── Exercise-327.rkt │ └── Exercise-328.rkt ├── 20-Iterative-Refinement │ ├── Exercise-329.rkt │ ├── Exercise-330.rkt │ ├── Exercise-331.rkt │ ├── Exercise-332.rkt │ ├── Exercise-333.rkt │ ├── Exercise-334.rkt │ ├── Exercise-335.rkt │ ├── Exercise-336.rkt │ ├── Exercise-337.rkt │ ├── Exercise-338.rkt │ ├── Exercise-339.rkt │ ├── Exercise-340.rkt │ ├── Exercise-341.rkt │ ├── Exercise-342.rkt │ ├── Exercise-343.rkt │ └── Exercise-344.rkt ├── 21-Refining-Interpreters │ ├── Exercise-345.rkt │ ├── Exercise-346.rkt │ ├── Exercise-347.rkt │ ├── Exercise-348.rkt │ ├── Exercise-349.rkt │ ├── Exercise-350.rkt │ ├── Exercise-351.rkt │ ├── Exercise-352.rkt │ ├── Exercise-353.rkt │ ├── Exercise-354.rkt │ ├── Exercise-355.rkt │ ├── Exercise-356.rkt │ ├── Exercise-357.rkt │ ├── Exercise-358.rkt │ ├── Exercise-359.rkt │ ├── Exercise-360.rkt │ ├── Exercise-361.rkt │ └── Exercise-362.rkt ├── 22-Project-The-Commerce-of-XML │ ├── Exercise-363.rkt │ ├── Exercise-364.rkt │ ├── Exercise-365.rkt │ ├── Exercise-366.rkt │ ├── Exercise-367.rkt │ ├── Exercise-368.rkt │ ├── Exercise-369.rkt │ ├── Exercise-370.rkt │ ├── Exercise-371.rkt │ ├── Exercise-372.rkt │ ├── Exercise-373.rkt │ ├── Exercise-374.rkt │ ├── Exercise-375.rkt │ ├── Exercise-376.rkt │ ├── Exercise-377.rkt │ ├── Exercise-378.rkt │ ├── Exercise-379.rkt │ ├── Exercise-380.rkt │ ├── Exercise-381.rkt │ ├── Exercise-382.rkt │ ├── Exercise-383.rkt │ ├── Exercise-384.rkt │ ├── Exercise-385.rkt │ ├── Exercise-386.rkt │ └── files │ │ └── Ford.html └── 23-Simultaneous-Processing │ ├── Exercise-387.rkt │ ├── Exercise-388.rkt │ ├── Exercise-389.rkt │ ├── Exercise-390.rkt │ ├── Exercise-391.rkt │ ├── Exercise-392.rkt │ ├── Exercise-393.rkt │ ├── Exercise-394.rkt │ ├── Exercise-395.rkt │ ├── Exercise-396.rkt │ ├── Exercise-397.rkt │ ├── Exercise-398.rkt │ ├── Exercise-399.rkt │ ├── Exercise-400.rkt │ ├── Exercise-401.rkt │ ├── Exercise-402.rkt │ ├── Exercise-403.rkt │ ├── Exercise-404.rkt │ ├── Exercise-405.rkt │ ├── Exercise-406.rkt │ ├── Exercise-407.rkt │ ├── Exercise-408.rkt │ ├── Exercise-409.rkt │ ├── Exercise-410.rkt │ └── Exercise-411.rkt ├── 05-Generative-Recursion ├── 25-Non-standard-Recursion │ ├── Exercise-421.rkt │ ├── Exercise-422.rkt │ ├── Exercise-423.rkt │ ├── Exercise-424.rkt │ ├── Exercise-425.rkt │ ├── Exercise-426.rkt │ ├── Exercise-427.rkt │ ├── Exercise-428.rkt │ ├── Exercise-429.rkt │ ├── Exercise-430.rkt │ └── images │ │ ├── quick-sort.png │ │ └── resources │ │ └── quick-sort.drawio ├── 26-Designing-Algorithms │ ├── Exercise-431.rkt │ ├── Exercise-432.rkt │ ├── Exercise-433.rkt │ ├── Exercise-434.rkt │ ├── Exercise-435.rkt │ ├── Exercise-436.rkt │ ├── Exercise-437.rkt │ ├── Exercise-438.rkt │ ├── Exercise-439.rkt │ ├── Exercise-440.rkt │ ├── Exercise-441.rkt │ ├── Exercise-442.rkt │ ├── Exercise-443.rkt │ └── Exercise-444.rkt ├── 27-Variations-on-the-Theme │ ├── Exercise-445.rkt │ ├── Exercise-446.rkt │ ├── Exercise-447.rkt │ ├── Exercise-448.rkt │ ├── Exercise-449.rkt │ ├── Exercise-450.rkt │ ├── Exercise-451.rkt │ ├── Exercise-452.rkt │ ├── Exercise-453.rkt │ ├── Exercise-454.rkt │ └── images │ │ ├── i-4.png │ │ ├── mi.png │ │ └── poly.png ├── 28-Mathematical-Examples │ ├── Exercise-455.rkt │ ├── Exercise-456.rkt │ ├── Exercise-457.rkt │ ├── Exercise-458.rkt │ ├── Exercise-459.rkt │ ├── Exercise-460.rkt │ ├── Exercise-461.rkt │ ├── Exercise-462.rkt │ ├── Exercise-463.rkt │ ├── Exercise-464.rkt │ ├── Exercise-465.rkt │ ├── Exercise-466.rkt │ ├── Exercise-467.rkt │ ├── Exercise-468.rkt │ ├── Exercise-469.rkt │ ├── Exercise-470.rkt │ └── images │ │ └── xx+x-2.png └── 29-Algorithms-that-Backtrack │ ├── Exercise-471.rkt │ ├── Exercise-472.rkt │ ├── Exercise-473.rkt │ ├── Exercise-474.rkt │ ├── Exercise-475.rkt │ ├── Exercise-476.rkt │ ├── Exercise-477.rkt │ ├── Exercise-478.rkt │ ├── Exercise-479.rkt │ ├── Exercise-480.rkt │ ├── Exercise-481.rkt │ ├── Exercise-482.rkt │ ├── Exercise-483.rkt │ └── images │ ├── graph-168.png │ └── graph-170.png ├── 05-Intermezzo ├── Exercise-484.rkt ├── Exercise-485.rkt ├── Exercise-486.rkt ├── Exercise-487.rkt └── Exercise-488.rkt ├── 06-Accumulators ├── 31-The-Loss-of-Knowledge │ ├── Exercise-489.rkt │ ├── Exercise-490.rkt │ ├── Exercise-491.rkt │ └── Exercise-492.rkt ├── 32-Designing-Accumulator-Style-Functions │ ├── Exercise-493.rkt │ ├── Exercise-494.rkt │ ├── Exercise-495.rkt │ ├── Exercise-496.rkt │ ├── Exercise-497.rkt │ ├── Exercise-498.rkt │ ├── Exercise-499.rkt │ ├── Exercise-500.rkt │ ├── Exercise-501.rkt │ ├── Exercise-502.rkt │ ├── Exercise-503.rkt │ ├── Exercise-504.rkt │ ├── Exercise-505.rkt │ ├── Exercise-506.rkt │ ├── Exercise-507.rkt │ ├── Exercise-508.rkt │ ├── Exercise-509.rkt │ ├── Exercise-510.rkt │ └── files │ │ └── 510.txt └── 33-Accumulators-and-Trees │ ├── Exercise-511.rkt │ ├── Exercise-512.rkt │ ├── Exercise-513.rkt │ ├── Exercise-514.rkt │ ├── Exercise-515.rkt │ ├── Exercise-516.rkt │ ├── Exercise-517.rkt │ ├── Exercise-518.rkt │ ├── Exercise-519.rkt │ ├── Exercise-520.rkt │ ├── Exercise-521.rkt │ ├── Exercise-522.rkt │ ├── Exercise-523.rkt │ ├── Exercise-524.rkt │ ├── Exercise-525.rkt │ ├── Exercise-526.rkt │ ├── Exercise-527.rkt │ └── Exercise-528.rkt ├── LICENSE └── README.md /01-Fixed-Size-Data/01-Arithmetic/Exercise-01.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-1) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 1. 6 | ;; Add the following definitions for x and y to DrRacket’s definitions area: 7 | ;; (define x 3) 8 | ;; (define y 4) 9 | ;; Now imagine that x and y are the coordinates of a Cartesian point. 10 | ;; Write down an expression that computes the distance of this point to the origin, 11 | ;; that is, a point with the coordinates (0,0). 12 | ;; To confirm that the expression works properly, change x to 12 and y to 5. 13 | 14 | ;; Definitions 15 | (define x 3) 16 | (define y 4) 17 | 18 | ;; Application 19 | (sqrt (+ (sqr x) (sqr y))) 20 | 21 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/01-Arithmetic/Exercise-02.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-2) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 2. 6 | ;; Add the following two lines to the definitions area: 7 | ;; (define prefix "hello") 8 | ;; (define suffix "world") 9 | ;; Then use string primitives to create an expression 10 | ;; that concatenates prefix and suffix and adds "_" between them. 11 | ;; When you run this program, you will see "hello_world" in the interactions area. 12 | 13 | ;; Definitions 14 | (define prefix "hello") 15 | (define suffix "world") 16 | 17 | ;; Application 18 | (string-append prefix "_" suffix) 19 | 20 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/01-Arithmetic/Exercise-03.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-3) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 3. 6 | ;; Add the following two lines to the definitions area: 7 | ;; (define str "helloworld") 8 | ;; (define i 5) 9 | ;; Then create an expression using string primitives that adds "_" at position i. 10 | ;; In general this means the resulting string is longer than the original one; 11 | ;; here the expected result is "hello_world". 12 | 13 | ;; Definitions 14 | (define str "helloworld") 15 | (define i 5) 16 | 17 | ;; Application 18 | (string-append (substring str 0 i) "_" (substring str i)) 19 | 20 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/01-Arithmetic/Exercise-04.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-4) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 4. 6 | ;; Use the same setup as in exercise 3 to create an expression that deletes the ith position from str. 7 | ;; Clearly this expression creates a shorter string than the given one. 8 | ;; Which values for i are legitimate? 9 | 10 | ;; Definitions 11 | (define str "helloworld") 12 | ;legitimate i 0123456789 13 | (define i 5) 14 | 15 | ;; Application 16 | (string-append (substring str 0 (- i 1)) (substring str i)) 17 | 18 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/01-Arithmetic/Exercise-05.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-5) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 5. 6 | ;; Use the 2htdp/image library to create the image of a simple boat or tree. 7 | ;; Make sure you can easily change the scale of the entire image. 8 | 9 | (require 2htdp/image) 10 | 11 | ;; Definitions 12 | (define WIDTH 250) 13 | (define HEIGHT 250) 14 | 15 | (define LEAVES (pulled-regular-polygon (/ HEIGHT 8) 20 1.5 120 "solid" "darkgreen")) 16 | (define TRUNK (isosceles-triangle (/ HEIGHT 2) 12 "solid" "brown")) 17 | (define TREE (overlay/align/offset "center" "bottom" LEAVES 0 (/ HEIGHT 8) TRUNK)) 18 | (define BACKGROUND 19 | (rectangle (+ (image-width TREE) 10) (+ (image-height TREE) 10) "solid" "lightblue")) 20 | 21 | ;; Application 22 | (overlay/align "center" "bottom" TREE BACKGROUND) 23 | 24 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/01-Arithmetic/Exercise-06.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-6) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 6. 6 | ;; Add the following line to the definitions area: 7 | ;; (define cat ) 8 | ;; Create an expression that counts the number of pixels in the image. 9 | 10 | (require 2htdp/image) 11 | 12 | ;; Definitions 13 | (define cat (bitmap "./images/cat.png")) ; load image to keep this file in a human-readable format 14 | 15 | ;; Application 16 | (* (image-width cat) (image-height cat)) 17 | 18 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/01-Arithmetic/Exercise-07.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-7) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 7. 6 | ;; Add these two lines to the definitions area of DrRacket: 7 | ;; (define sunny #true) 8 | ;; (define friday #false) 9 | ;; Now create an expression that computes whether sunny is false or friday is true. 10 | ;; So in this particular case, the answer is #false. (Why?) 11 | ;; How many combinations of Booleans can you associate with sunny and friday? 12 | 13 | (require 2htdp/image) 14 | 15 | ;; Definitions 16 | (define sunny #true) 17 | (define friday #false) 18 | 19 | ;; Application 20 | (or (not sunny) friday) ; outputs #false, because both (not sunny) and friday evaluate to #false. 21 | 22 | ;; Answer 23 | ;; Four combinations of Booleans can be associated with sunny and friday: 24 | ;; ------------------ 25 | ;; # | sunny | friday 26 | ;; ------------------ 27 | ;; 1 | true | true 28 | ;; 2 | true | false 29 | ;; 3 | false | true 30 | ;; 4 | false | false 31 | ;; ------------------ 32 | 33 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/01-Arithmetic/Exercise-08.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-8) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 8. 6 | ;; Add the following line to the definitions area: 7 | ;; (define cat ) 8 | ;; Create a conditional expression that computes whether the image is tall or wide. 9 | ;; An image should be labeled "tall" if its height is larger than or equal to its width; 10 | ;; otherwise it is "wide". 11 | ;; Replace the cat with a rectangle of your choice to ensure that you know the expected answer. 12 | ;; Now try the following modification. 13 | ;; Create an expression that computes whether a picture is "tall", "wide", or "square". 14 | 15 | (require 2htdp/image) 16 | 17 | ;; Definitions 18 | (define cat (bitmap "./images/cat.png")) ; load image to keep this file in a human-readable format 19 | 20 | (define shape (rectangle 40 40 "solid" "white")) 21 | (define shape-width (image-width shape)) 22 | (define shape-height (image-height shape)) 23 | 24 | ;; Application 25 | (if (>= (image-height cat) (image-width cat)) "tall" "wide") 26 | 27 | (if (> shape-height shape-width) 28 | "tall" 29 | (if (< shape-height shape-width) "wide" "square")) 30 | 31 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/01-Arithmetic/Exercise-09.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-9) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 9. 6 | ;; Add the following line to the definitions area of DrRacket: 7 | ;; (define in ...) 8 | ;; Then create an expression that converts the value of in to a positive number. 9 | ;; For a String, it determines how long the String is; 10 | ;; for an Image, it uses the area; 11 | ;; for a Number, it decrements the number by 1, unless it is already 0 or negative; 12 | ;; for #true it uses 10 and for #false 20. 13 | 14 | (require 2htdp/image) 15 | 16 | ;; Definitions 17 | (define in "42") 18 | 19 | ;; Application 20 | (if (string? in) 21 | (string-length in) 22 | (if (image? in) 23 | (* (image-width in) (image-height in)) 24 | (if (number? in) 25 | (if (> in 0) 26 | (- in 1) 27 | (abs in)) 28 | (if (boolean? in) 29 | (if in 10 20) 30 | (error "Not supported data type."))))) 31 | 32 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/01-Arithmetic/Exercise-10.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-10) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 10. 6 | ;; Now relax, eat, sleep, and then tackle the next chapter. 7 | 8 | "zzzzzz" 9 | 10 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/01-Arithmetic/images/cat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs-studies/htdp/225e7287a6bba1a60eb3bc0e96aeedb2f2d60bdc/01-Fixed-Size-Data/01-Arithmetic/images/cat.png -------------------------------------------------------------------------------- /01-Fixed-Size-Data/02-Functions-and-Programs/Exercise-11.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-11) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 11. 6 | ;; Define a function that consumes two numbers, x and y, 7 | ;; and that computes the distance of point (x,y) to the origin. 8 | 9 | ;; Definitions 10 | (define (calculate-distance x y) (sqrt (+ (sqr x) (sqr y)))) 11 | 12 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/02-Functions-and-Programs/Exercise-12.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-12) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 12. 6 | ;; Define the function cvolume, which accepts the length of a side of an equilateral cube 7 | ;; and computes its volume. If you have time, consider defining csurface, too. 8 | 9 | ;; Definitions 10 | (define (cvolume length) (expt length 3)) 11 | 12 | (define (csurface length) (* (sqr length) 6)) 13 | 14 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/02-Functions-and-Programs/Exercise-13.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-13) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 13. 6 | ;; Define the function string-first, which extracts the first 1String from a non-empty string. 7 | 8 | ;; Definitions 9 | (define (string-first str) 10 | (if (> (string-length str) 0) 11 | (substring str 0 1) 12 | "")) 13 | 14 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/02-Functions-and-Programs/Exercise-14.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-14) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 14. 6 | ;; Define the function string-last, which extracts the last 1String from a non-empty string. 7 | 8 | ;; Definitions 9 | (define (string-last str) 10 | (if (> (string-length str) 0) 11 | (substring str (- (string-length str) 1)) 12 | "")) 13 | 14 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/02-Functions-and-Programs/Exercise-15.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-15) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 15. 6 | ;; Define ==>. 7 | ;; The function consumes two Boolean values, call them sunny and friday. 8 | ;; Its answer is #true if sunny is false or friday is true. 9 | 10 | ;; Definitions 11 | (define (==> sunny friday) 12 | (or (not sunny) friday)) 13 | 14 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/02-Functions-and-Programs/Exercise-16.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-16) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 16. 6 | ;; Define the function image-area, which counts the number of pixels in a given image. 7 | 8 | (require 2htdp/image) 9 | 10 | ;; Definitions 11 | (define (image-area img) 12 | (* (image-width img) (image-height img))) 13 | 14 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/02-Functions-and-Programs/Exercise-17.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-17) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 17. 6 | ;; Define the function image-classify, which consumes an image 7 | ;; and conditionally produces "tall" if the image is taller than wide, 8 | ;; "wide" if it is wider than tall, 9 | ;; or "square" if its width and height are the same. 10 | 11 | (require 2htdp/image) 12 | 13 | ;; Definitions 14 | (define (image-classify img) 15 | (if (> (image-height img) (image-width img)) 16 | "tall" 17 | (if (> (image-width img) (image-height img)) 18 | "wide" 19 | "square"))) 20 | 21 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/02-Functions-and-Programs/Exercise-18.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-18) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 18. 6 | ;; Define the function string-join, which consumes two strings and appends them with "_" in between. 7 | 8 | ;; Definitions 9 | (define (string-join str1 str2) 10 | (string-append str1 "_" str2)) 11 | 12 | ;; Application 13 | (string-join "Funny" "Faces") 14 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/02-Functions-and-Programs/Exercise-19.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-19) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 19. 6 | ;; Define the function string-insert, 7 | ;; which consumes a string str plus a number i and inserts "_" at the ith position of str. 8 | ;; Assume i is a number between 0 and the length of the given string (inclusive). 9 | ;; See exercise 3 for ideas. Ponder how string-insert copes with "". 10 | 11 | ;; Definitions 12 | (define (string-insert str i) 13 | (string-append (substring str 0 i) "_" (substring str i))) 14 | 15 | ; Application 16 | (string-insert "FunnyFaces" 5) 17 | 18 | (string-insert "" 0) 19 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/02-Functions-and-Programs/Exercise-20.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-20) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 20. 6 | ;; Define the function string-delete, 7 | ;; which consumes a string plus a number i and deletes the ith position from str. 8 | ;; Assume i is a number between 0 (inclusive) and the length of the given string (exclusive). 9 | ;; See exercise 4 for ideas. Can string-delete deal with empty strings? 10 | 11 | ;; Definitions 12 | (define (string-delete str i) 13 | (if (> (string-length str) 0) 14 | (string-append (substring str 0 i) (substring str (+ i 1))) 15 | "")) 16 | 17 | ;; Application 18 | (string-delete "Funny_Faces" 5) 19 | 20 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/02-Functions-and-Programs/Exercise-21.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-21) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 21. 6 | ;; Use DrRacket’s stepper to evaluate (ff (ff 1)) step-by-step. 7 | ;; Also try (+ (ff 1) (ff 1)). 8 | ;; Does DrRacket’s stepper reuse the results of computations? 9 | 10 | ;; Definitions 11 | (define (ff a) 12 | (* 10 a)) 13 | 14 | ;; Application 15 | (ff (ff 1)) 16 | 17 | (+ (ff 1) (ff 1)) ; the stepper does not reuse the results of computation. 18 | 19 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/02-Functions-and-Programs/Exercise-22.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-22) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 22. 6 | ;; Use DrRacket’s stepper on this program fragment: 7 | ;; (define (distance-to-origin x y) 8 | ;; (+ (sqr x) (sqr y)))) 9 | ;; (distance-to-origin 3 4) 10 | ;; Does the explanation match your intuition? 11 | 12 | (define (distance-to-origin x y) 13 | (sqrt (+ (sqr x) (sqr y)))) 14 | 15 | (distance-to-origin 3 4) 16 | 17 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/02-Functions-and-Programs/Exercise-23.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-23) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 22. 6 | ;; The first 1String in "hello world" is "h". 7 | ;; How does the following function compute this result? 8 | ;; (define (string-first s) 9 | ;; (substring s 0 1)) 10 | ;; Use the stepper to confirm your ideas. 11 | 12 | (define (string-first s) 13 | (substring s 0 1)) 14 | 15 | (string-first "hello world") 16 | 17 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/02-Functions-and-Programs/Exercise-24.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-24) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 24. 6 | ;; Here is the definition of ==> 7 | ;; (define (==> x y) 8 | ;; (or (not x) y)) 9 | ;; Use the stepper to determine the value of (==> #true #false). 10 | 11 | (define (==> x y) 12 | (or (not x) y)) 13 | 14 | (==> #true #false) 15 | 16 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/02-Functions-and-Programs/Exercise-25.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-25) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 25. 6 | ;; Take a look at this attempt to solve exercise 17: 7 | ;; 8 | ;; Does stepping through an application suggest a fix? 9 | 10 | (require 2htdp/image) 11 | 12 | ;; Exercise code 13 | (define (image-classify img) 14 | (cond 15 | [(>= (image-height img) (image-width img)) "tall"] 16 | [(= (image-height img) (image-width img)) "square"] 17 | [(<= (image-height img) (image-width img)) "wide"])) 18 | 19 | ;; Square is not classified as "square" 20 | (image-classify (square 20 "outline" "blue")) 21 | 22 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/02-Functions-and-Programs/Exercise-26.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-26) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 26. 6 | ;; What do you expect as the value of this program: 7 | ;; 8 | ;; Confirm your expectation with DrRacket and its stepper. 9 | 10 | ;; Exercise code 11 | (define (string-insert s i) 12 | (string-append (substring s 0 i) 13 | "_" 14 | (substring s i))) 15 | 16 | (string-insert "helloworld" 6) 17 | 18 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/02-Functions-and-Programs/Exercise-27.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-27) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 27. 6 | ;; Collect all definitions in DrRacket’s definitions area and change them 7 | ;; so that all magic numbers are refactored into constant definitions. 8 | 9 | ;; Definitions 10 | (define BASE-PRICE 5.0) 11 | (define BASE-ATTENDEES 120) 12 | 13 | (define PRICE-CHANGE 0.1) 14 | (define ATTENDEES-CHANGE 15) 15 | 16 | (define PERFORMANCE-COST 180) 17 | (define ATTENDEE-COST 0.04) 18 | 19 | ;; Returns negative values for ticket-price > 5.8 20 | (define (attendees ticket-price) 21 | (- BASE-ATTENDEES (* (- ticket-price BASE-PRICE) (/ ATTENDEES-CHANGE PRICE-CHANGE)))) 22 | ;; same as 23 | ;;(- BASE-ATTENDEES (* (/ (- ticket-price BASE-PRICE) PRICE-CHANGE) ATTENDEES-CHANGE))) 24 | 25 | (define (revenue ticket-price) 26 | (* ticket-price (attendees ticket-price))) 27 | 28 | (define (cost ticket-price) 29 | (+ PERFORMANCE-COST (* ATTENDEE-COST (attendees ticket-price)))) 30 | 31 | (define (profit ticket-price) 32 | (- (revenue ticket-price) 33 | (cost ticket-price))) 34 | 35 | ;; Application 36 | (profit 1) 37 | (profit 2) 38 | (profit 3) 39 | (profit 4) 40 | (profit 5) 41 | 42 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/02-Functions-and-Programs/Exercise-28.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-28) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 28. 6 | ;; Determine the potential profit for these ticket prices: $1, $2, $3, $4, and $5. 7 | ;; Which price maximizes the profit of the movie theater? 8 | ;; Determine the best ticket price to a dime. 9 | 10 | ;; Answer 11 | ;; See implementation in the Exercise-27.rkt file. 12 | ;; ---------------------- 13 | ;; price ($) | profit ($) 14 | ;; ---------------------- 15 | ;; 1 | 511.2 16 | ;; 2 | 937.2 17 | ;; 3 | 1063.2 18 | ;; 4 | 889.2 19 | ;; 5 | 415.2 20 | ;; ---------------------- 21 | ;; $3 price maximizes the profit. 22 | 23 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/02-Functions-and-Programs/Exercise-30.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-30) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 30. 6 | ;; Define constants for the price optimization program at the movie theater 7 | ;; so that the price sensitivity of attendance (15 people for every 10 cents) 8 | ;; becomes a computed constant. 9 | 10 | ;; Definitions 11 | (define BASE-PRICE 5.0) ; dollars 12 | (define BASE-ATTENDEES 120) ; number of people 13 | (define PRICE-AFFORDABILITY (/ 15 0.1)) ; number of people per price change in dollars 14 | (define ATTENDEE-COST 1.5) ; dollars 15 | 16 | (define (attendees ticket-price) 17 | (- BASE-ATTENDEES (* (- ticket-price BASE-PRICE) PRICE-AFFORDABILITY))) 18 | 19 | (define (revenue ticket-price) 20 | (* ticket-price (attendees ticket-price))) 21 | 22 | (define (cost ticket-price) 23 | (* ATTENDEE-COST (attendees ticket-price))) 24 | 25 | (define (profit ticket-price) 26 | (- (revenue ticket-price) 27 | (cost ticket-price))) 28 | 29 | ;; Application 30 | (profit 3) 31 | (profit 4) 32 | (profit 5) 33 | 34 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/03-How-to-Design-Programs/Exercise-34.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-34) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 34. 6 | ;; Design the function string-first, 7 | ;; which extracts the first character from a non-empty string. 8 | ;; Don’t worry about empty strings. 9 | 10 | ;; 1. Express how you wish to represent information as data. 11 | 12 | ; We use String data type to represent textual information. 13 | 14 | 15 | ;; 2. Write down: 16 | ;; - a signature 17 | ;; - a statement of purpose 18 | ;; - a function header 19 | 20 | ; String -> 1String 21 | ; Extracts the first character from a string. 22 | ; (define (f str) "a") 23 | 24 | 25 | ;; 3. Illustrate the signature and the purpose statement 26 | ;; with some functional examples. 27 | 28 | ; "z" -> "z" 29 | ; "0-day" -> "0" 30 | ; "Fun Fact" -> "F" 31 | 32 | 33 | ;; 4. Write down function prototype: header and body template. 34 | 35 | ; (define (string-first s) (... s ...)) 36 | 37 | 38 | ;; 5. Write down the function. 39 | 40 | (define (string-first s) 41 | (substring s 0 1)) 42 | 43 | 44 | ;; 6. Test the function on the step 3 examples. 45 | 46 | (string-first "z") 47 | 48 | (string-first "0-day") 49 | 50 | (string-first "Fun Fact") 51 | 52 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/03-How-to-Design-Programs/Exercise-35.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-35) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 35. 6 | ;; Design the function string-last, 7 | ;; which extracts the last character from a non-empty string. 8 | 9 | ;; 1. Express how you wish to represent information as data. 10 | 11 | ; We use String data type to represent textual information. 12 | 13 | 14 | ;; 2. Write down: 15 | ;; - a signature 16 | ;; - a statement of purpose 17 | ;; - a function header 18 | 19 | ; String -> 1String 20 | ; Extracts the last character from a string. 21 | ; (define (f str) "z") 22 | 23 | 24 | ;; 3. Illustrate the signature and the purpose statement 25 | ;; with some functional examples. 26 | 27 | ; "z" -> "z" 28 | ; "0-day-1" -> "1" 29 | ; "Fun FacT" -> "T" 30 | 31 | 32 | ;; 4. Write down function prototype: header and body template. 33 | 34 | ; (define (string-last s) (... s ...)) 35 | 36 | 37 | ;; 5. Write down the function. 38 | 39 | (define (string-last s) 40 | (substring s (- (string-length s) 1))) 41 | 42 | 43 | ;; 6. Test the function on the step 3 examples. 44 | 45 | (string-last "z") 46 | 47 | (string-last "0-day-1") 48 | 49 | (string-last "Fun FacT") 50 | 51 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/03-How-to-Design-Programs/Exercise-37.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-37) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 37. 6 | ;; Design the function string-rest, 7 | ;; which produces a string like the given one 8 | ;; with the first character removed. 9 | 10 | ;; 1. Express how you wish to represent information as data. 11 | 12 | ; We use String data type to represent textual information. 13 | 14 | 15 | ;; 2. Write down: 16 | ;; - a signature 17 | ;; - a statement of purpose 18 | ;; - a function header 19 | 20 | ; String -> String 21 | ; Removes first character from a string. 22 | ; (define (f str) "abc") 23 | 24 | 25 | ;; 3. Illustrate the signature and the purpose statement 26 | ;; with some functional examples. 27 | 28 | ; "Fun Fact" -> "un Fact" 29 | ; "0-day" -> "-day" 30 | 31 | 32 | ;; 4. Write down function prototype: header and body template. 33 | 34 | ; (define (string-rest str) (... str ...)) 35 | 36 | 37 | ;; 5. Write down the function. 38 | 39 | (define (string-rest str) 40 | (substring str 1)) 41 | 42 | 43 | ;; 6. Test the function on the step 3 examples. 44 | 45 | (string-rest "Fun Fact") 46 | 47 | (string-rest "0-day") 48 | 49 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/03-How-to-Design-Programs/Exercise-38.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-38) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 38. 6 | ;; Design the function string-remove-last, 7 | ;; which produces a string like the given one 8 | ;; with the last character removed. 9 | 10 | ;; 1. Express how you wish to represent information as data. 11 | 12 | ; We use String data type to represent textual information. 13 | 14 | 15 | ;; 2. Write down: 16 | ;; - a signature 17 | ;; - a statement of purpose 18 | ;; - a function header 19 | 20 | ; String -> String 21 | ; Removes last character from a string. 22 | ; (define (f str) "abc") 23 | 24 | 25 | ;; 3. Illustrate the signature and the purpose statement 26 | ;; with some functional examples. 27 | 28 | ; "0-day" -> "0-da" 29 | ; "FYI" -> "FY" 30 | 31 | ;; 4. Write down function prototype: header and body template. 32 | 33 | ; (define (string-remove-last str) (... str ...)) 34 | 35 | 36 | ;; 5. Write down the function. 37 | 38 | (define (string-remove-last str) 39 | (substring str 0 (- (string-length str) 1))) 40 | 41 | 42 | ;; 6. Test the function on the step 3 examples. 43 | 44 | (string-remove-last "0-day") 45 | 46 | (string-remove-last "FYI") 47 | 48 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/03-How-to-Design-Programs/Exercise-40.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-40) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 40. 6 | ;; Formulate the examples as BSL tests, that is, using the check-expect form. 7 | ;; Introduce a mistake. Re-run the tests. 8 | 9 | (require 2htdp/universe) 10 | 11 | ;; Unit tests 12 | (check-expect (tock 20) 23) 13 | (check-expect (tock 78) 81) 14 | 15 | ;; Definitions 16 | ;; WorldState -> WorldState 17 | ;; moves the car by 3 pixels for every clock tick 18 | (define (tock ws) 19 | (+ ws 3)) 20 | 21 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/03-How-to-Design-Programs/images/cat1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs-studies/htdp/225e7287a6bba1a60eb3bc0e96aeedb2f2d60bdc/01-Fixed-Size-Data/03-How-to-Design-Programs/images/cat1.png -------------------------------------------------------------------------------- /01-Fixed-Size-Data/03-How-to-Design-Programs/images/cat2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs-studies/htdp/225e7287a6bba1a60eb3bc0e96aeedb2f2d60bdc/01-Fixed-Size-Data/03-How-to-Design-Programs/images/cat2.png -------------------------------------------------------------------------------- /01-Fixed-Size-Data/03-How-to-Design-Programs/images/cat3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs-studies/htdp/225e7287a6bba1a60eb3bc0e96aeedb2f2d60bdc/01-Fixed-Size-Data/03-How-to-Design-Programs/images/cat3.png -------------------------------------------------------------------------------- /01-Fixed-Size-Data/04-Intervals-Enumerations-Itemizations/Exercise-48.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-48) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 48. 6 | ;; Enter the definition of reward followed by (reward 18) 7 | ;; into the definitions area of DrRacket 8 | ;; and use the stepper to find out 9 | ;; how DrRacket evaluates applications of the function. 10 | 11 | ;; A PositiveNumber is a Number greater than/equal to 0. 12 | 13 | ;; Unit Tests 14 | (check-expect (reward 3) "bronze") 15 | (check-expect (reward 18) "silver") 16 | (check-expect (reward 21) "gold") 17 | 18 | ;; PositiveNumber -> String 19 | ;; Computes the reward level from the given score s. 20 | (define (reward s) 21 | (cond 22 | [(<= 0 s 10) "bronze"] 23 | [(and (< 10 s) (<= s 20)) "silver"] 24 | [else "gold"])) 25 | 26 | ;; Application 27 | (reward 18) 28 | 29 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/04-Intervals-Enumerations-Itemizations/Exercise-49-1.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-49-1) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 49. 6 | ;; Use the stepper to evaluate the expression for y as 100 and 210. 7 | 8 | (define y 100) 9 | ;;(define y 210) 10 | 11 | (- 200 (cond [(> y 200) 0] [else y])) 12 | 13 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/04-Intervals-Enumerations-Itemizations/Exercise-49-2.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-49-2) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 49. 6 | ;; Reformulate create-rocket-scene.v5 to use a nested expression; 7 | ;; the resulting function mentions place-image only once. 8 | 9 | (require 2htdp/image) 10 | 11 | ;; Definitions 12 | (define WIDTH 100) 13 | (define HEIGHT 60) 14 | (define MTSCN (empty-scene WIDTH HEIGHT)) 15 | (define ROCKET (bitmap "./images/rocket.png")) 16 | (define ROCKET-CENTER-TO-TOP 17 | (- HEIGHT (/ (image-height ROCKET) 2))) 18 | 19 | ;; Number -> Image 20 | (define (create-rocket-scene.v5 h) 21 | (place-image ROCKET 22 | 50 23 | (cond 24 | [(<= h ROCKET-CENTER-TO-TOP) h] 25 | [else ROCKET-CENTER-TO-TOP]) 26 | MTSCN)) 27 | 28 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/04-Intervals-Enumerations-Itemizations/Exercise-50.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-50) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 50. 6 | ;; Copy and paste the traffic-light-next function definition. 7 | ;; Add enough tests to make DrRacket happy. 8 | 9 | ;; TrafficLight -> TrafficLight 10 | ;; Yields the next state given current state s. 11 | (check-expect (traffic-light-next "red") "green") 12 | (check-expect (traffic-light-next "green") "yellow") 13 | (check-expect (traffic-light-next "yellow") "red") 14 | (define (traffic-light-next s) 15 | (cond 16 | [(string=? "red" s) "green"] 17 | [(string=? "green" s) "yellow"] 18 | [(string=? "yellow" s) "red"])) 19 | 20 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/04-Intervals-Enumerations-Itemizations/Exercise-52.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-52) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 52. 6 | ;; Which integers are contained in the four intervals above? 7 | 8 | ;; Closed interval. 9 | ;; [3, 5] = {3, 4, 5} 10 | 11 | ;; Left-open interval. 12 | ;; (3, 5] = {4, 5} 13 | 14 | ;; Right-open interval. 15 | ;; [3, 5) = {3, 4} 16 | 17 | ;; Open interval. 18 | ;; (3, 5) = {4} 19 | 20 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/04-Intervals-Enumerations-Itemizations/Exercise-54.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-54) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 54. 6 | ;; Why would it be incorrect to use (string=? "resting" x) 7 | ;; as the first condition in show? 8 | ;; Conversely, formulate a completely accurate condition, 9 | ;; that is, a Boolean expression that evaluates to #true 10 | ;; precisely when x belongs to the first sub-class of LRCD. 11 | 12 | 13 | ;; Answer. 14 | ;; string=? expects String parameters, 15 | ;; but x may be a Number in this function. 16 | ;; So, it is required to check if x is a String. 17 | ;; Without such a check, if x in a Number, 18 | ;; the program quits with an error. 19 | ;; For example: 20 | ;; (define x 4) 21 | ;; (string=? "str" x) 22 | ;; outputs "string=?: expects a string as 2nd argument, given 4". 23 | 24 | 25 | ;; Accurate Condition. 26 | ;; (define x 4) ; #false 27 | ;; (define x "rest") ; #false 28 | (define x "resting") ; #true 29 | (and (string? x) (string=? "resting" x)) 30 | 31 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/04-Intervals-Enumerations-Itemizations/Exercise-55.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-55) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 55. 6 | ;; Define an auxiliary function that draws a rocket 7 | ;; and thus shortens function show. 8 | 9 | (require 2htdp/image) 10 | 11 | 12 | ;; Constants Definitions 13 | 14 | (define WIDTH 100) 15 | (define HEIGHT 200) 16 | (define SCENE (empty-scene WIDTH HEIGHT)) 17 | 18 | (define ROCKET (bitmap "./images/rocket.png")) 19 | (define ROCKET-CENTER (/ (image-height ROCKET) 2)) 20 | 21 | 22 | ;; Functions Definitions 23 | 24 | (define (show x) 25 | (cond 26 | [(string? x) 27 | (draw-rocket (- HEIGHT ROCKET-CENTER))] 28 | [(<= -3 x -1) 29 | (place-image (text (number->string x) 20 "red") 30 | 10 (* 3/4 WIDTH) 31 | (draw-rocket (- HEIGHT ROCKET-CENTER)))] 32 | [(>= x 0) 33 | (draw-rocket (- x ROCKET-CENTER))])) 34 | 35 | (define (draw-rocket y) 36 | (place-image ROCKET 10 y SCENE)) 37 | 38 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/04-Intervals-Enumerations-Itemizations/images/rocket.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs-studies/htdp/225e7287a6bba1a60eb3bc0e96aeedb2f2d60bdc/01-Fixed-Size-Data/04-Intervals-Enumerations-Itemizations/images/rocket.png -------------------------------------------------------------------------------- /01-Fixed-Size-Data/05-Adding-Structure/Exercise-64.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-64) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 64. 6 | ;; Design the function manhattan-distance, 7 | ;; which measures the Manhattan distance of the given posn to the origin. 8 | 9 | 10 | ;; Posn -> distance 11 | (check-expect (manhattan-distance (make-posn 3 4)) 7) 12 | (check-expect (manhattan-distance (make-posn 3 0)) 3) 13 | (check-expect (manhattan-distance (make-posn 0 4)) 4) 14 | (define (manhattan-distance p) 15 | (+ (posn-x p) (posn-y p))) 16 | 17 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/05-Adding-Structure/Exercise-67.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-67) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 67. 6 | ;; Here is another way to represent bouncing balls: 7 | (define SPEED 3) 8 | (define-struct balld [location direction]) 9 | (make-balld 10 "up") 10 | ;; Interpret this code fragment and create other instances of balld. 11 | 12 | 13 | ;; Answer. 14 | ;; 1. Constant SPEED is defined but not used in the given code fragment. 15 | ;; 2. Struct balld is defined with two parameters: location and direction. 16 | ;; 3. A new istance of the struct balld is created 17 | ;; by calling a constructor make-balld 18 | ;; with parameters 10 (location) and "up" (direction). 19 | 20 | 21 | ;; Instance 1. 22 | (check-expect (balld-location new-balld1) 5) 23 | (define new-balld1 (make-balld 5 "down")) 24 | 25 | ;; Instance 2. 26 | (check-expect (balld-direction new-balld2) "down") 27 | (define new-balld2 (make-balld 20 "down")) 28 | 29 | ;; Instance 3. 30 | (check-expect (balld? new-balld3) #true) 31 | (define new-balld3 (make-balld 100 "up")) 32 | 33 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/05-Adding-Structure/Exercise-68.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-68) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 68. 6 | ;; Create an instance of ballf that has the same interpretation as ball1. 7 | 8 | ;(define ball1 9 | ; (make-ball (make-posn 30 40) (make-vel -10 5))) 10 | 11 | (define-struct ballf [x y deltax deltay]) 12 | 13 | (make-ballf 30 40 -10 5) 14 | 15 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/05-Adding-Structure/Exercise-69.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-69) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 69. 6 | ;; Draw box representations for the solution of exercise 65. 7 | 8 | (require 2htdp/image) 9 | 10 | (bitmap "./images/diagrams.png") 11 | 12 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/05-Adding-Structure/Exercise-70.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-70) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 70. 6 | ;; Spell out the laws for these structure type definitions: 7 | 8 | (define-struct centry [name home office cell]) 9 | (define-struct phone [area number]) 10 | 11 | ;; centry Laws 12 | ; (centry-name (make-centry n0 h0 o0 c0)) == n0 13 | ; (centry-home (make-centry n0 h0 o0 c0)) == h0 14 | ; (centry-office (make-centry n0 h0 o0 c0)) == o0 15 | ; (centry-cell (make-centry n0 h0 o0 c0)) == c0 16 | 17 | 18 | ;; phone Laws 19 | ; (phone-area (make-phone a0 n0)) == a0 20 | ; (phone-number (make-phone a0 n0)) == n0 21 | 22 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/05-Adding-Structure/Exercise-72.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-72) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 72. 6 | 7 | 8 | ;; Task 1. 9 | ;; Formulate a data definition 10 | ;; for the above phone structure type definition 11 | ;; that accommodates the given examples. 12 | 13 | (define-struct phone [area number]) 14 | ;; A Phone is a structure: 15 | ;; (make-phone Number String) 16 | ;; Interpretation: 17 | ;; area is an area code, 18 | ;; number is a local phone number. 19 | 20 | 21 | ;; Task 2. 22 | ;; Formulate a data definition for phone numbers 23 | ;; using the following structure type definition. 24 | ;; Describe the content of the three fields 25 | ;; as precisely as possible with intervals. 26 | 27 | (define-struct phone# [area switch num]) 28 | ;; A Phone# is a structure: 29 | ;; (make-phone# Number Number Number) 30 | ;; Interpretation: 31 | ;; area is an area code [000, 999], 32 | ;; switch is a switch code [000, 999], 33 | ;; num is a local number [0000, 9999]. 34 | 35 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/05-Adding-Structure/Exercise-77.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-77) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 77. 6 | ;; Provide a structure type definition and a data definition 7 | ;; for representing points in time since midnight. 8 | ;; A point in time consists of three numbers: 9 | ;; hours, minutes, and seconds. 10 | 11 | 12 | (define-struct time [hour minute second]) 13 | 14 | ;; A Time is a structure: 15 | ;; (make-time Number Number Number) 16 | ;; (make-time h m s) represents 17 | ;; a point in time since midnight 18 | ;; at h hours, 19 | ;; m minutes, 20 | ;; s seconds. 21 | 22 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/05-Adding-Structure/Exercise-78.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-78) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 78. 6 | ;; Provide a structure type and a data definition 7 | ;; for representing three-letter words. 8 | ;; A word consists of lowercase letters, 9 | ;; represented with the 1Strings "a" through "z" plus #false. 10 | 11 | 12 | ;; A Letter is one of: 13 | ;; - 1String "a" through "z", 14 | ;; - #false. 15 | ;; Represents a lowercase letter of a Latin alphabet. 16 | ;; #false denotes exceptional cases. 17 | 18 | (define-struct word [letter1 letter2 letter3]) 19 | 20 | ;; A Word is a stucture: 21 | ;; (make-word Letter Letter Letter). 22 | ;; (make-word l1 l2 l3) represents 23 | ;; an English language three-letter word 24 | ;; that consists of: 25 | ;; - the first Letter l1, 26 | ;; - the second Letter l2, and 27 | ;; - the third [final] Letter l3. 28 | 29 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/05-Adding-Structure/Exercise-80.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-80) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 80. 6 | ;; Create templates for functions 7 | ;; that consume instances of the following structure types: 8 | 9 | 10 | (define-struct movie [title director year]) 11 | 12 | (define (watch m) 13 | (... (movie-title m) ... (movie-director m) ... (movie-year m) ...)) 14 | 15 | 16 | (define-struct pet [name number]) 17 | 18 | (define (pat p) 19 | (... (pet-name p) ... (pet-number p) ...)) 20 | 21 | 22 | (define-struct CD [artist title price]) 23 | 24 | (define (play cd) 25 | (... (CD-artist cd) ... (CD-title cd) ... (CD-price cd) ...)) 26 | 27 | 28 | (define-struct sweater [material size color]) 29 | 30 | (define (buy s) 31 | (... (sweater-material s) ... (sweater-size s) ... (sweater-color s) ...)) 32 | 33 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/05-Adding-Structure/Exercise-88.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-88) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 88. 6 | ;; Define a structure type 7 | ;; that keeps track of the cat’s x-coordinate and its happiness. 8 | ;; Then formulate a data definition for cats, dubbed VCat, 9 | ;; including an interpretation. 10 | 11 | 12 | ;; A Score is a Number 13 | ;; in an interval [0, 100] 14 | ;; Represents a happiness level. 15 | 16 | (define-struct vCat [x score]) 17 | ;; A VCat is a structure: 18 | ;; (make-vCat Number Score) 19 | ;; (make-vCat x s) represents a walking cat 20 | ;; which is located on an x-coordinate x and 21 | ;; has a happiness level s. 22 | 23 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/05-Adding-Structure/images/cat1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs-studies/htdp/225e7287a6bba1a60eb3bc0e96aeedb2f2d60bdc/01-Fixed-Size-Data/05-Adding-Structure/images/cat1.png -------------------------------------------------------------------------------- /01-Fixed-Size-Data/05-Adding-Structure/images/cat2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs-studies/htdp/225e7287a6bba1a60eb3bc0e96aeedb2f2d60bdc/01-Fixed-Size-Data/05-Adding-Structure/images/cat2.png -------------------------------------------------------------------------------- /01-Fixed-Size-Data/05-Adding-Structure/images/cat3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs-studies/htdp/225e7287a6bba1a60eb3bc0e96aeedb2f2d60bdc/01-Fixed-Size-Data/05-Adding-Structure/images/cat3.png -------------------------------------------------------------------------------- /01-Fixed-Size-Data/05-Adding-Structure/images/cham-v2-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs-studies/htdp/225e7287a6bba1a60eb3bc0e96aeedb2f2d60bdc/01-Fixed-Size-Data/05-Adding-Structure/images/cham-v2-1.png -------------------------------------------------------------------------------- /01-Fixed-Size-Data/05-Adding-Structure/images/cham-v2-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs-studies/htdp/225e7287a6bba1a60eb3bc0e96aeedb2f2d60bdc/01-Fixed-Size-Data/05-Adding-Structure/images/cham-v2-2.png -------------------------------------------------------------------------------- /01-Fixed-Size-Data/05-Adding-Structure/images/cham-v3-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs-studies/htdp/225e7287a6bba1a60eb3bc0e96aeedb2f2d60bdc/01-Fixed-Size-Data/05-Adding-Structure/images/cham-v3-1.png -------------------------------------------------------------------------------- /01-Fixed-Size-Data/05-Adding-Structure/images/cham-v3-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs-studies/htdp/225e7287a6bba1a60eb3bc0e96aeedb2f2d60bdc/01-Fixed-Size-Data/05-Adding-Structure/images/cham-v3-2.png -------------------------------------------------------------------------------- /01-Fixed-Size-Data/05-Adding-Structure/images/diagrams.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs-studies/htdp/225e7287a6bba1a60eb3bc0e96aeedb2f2d60bdc/01-Fixed-Size-Data/05-Adding-Structure/images/diagrams.png -------------------------------------------------------------------------------- /01-Fixed-Size-Data/06-Itemizations-and-Structures/Exercise-105.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-105) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 105. 6 | ;; Some program contains the following data definition. 7 | ;; Make up at least two data examples per clause in the data definition. 8 | 9 | 10 | ;;; Data Definitions 11 | 12 | ;; A Coordinate is one of: 13 | ;; – a NegativeNumber 14 | ;; interpretation on the y axis, distance from top 15 | ;; – a PositiveNumber 16 | ;; interpretation on the x axis, distance from left 17 | ;; – a Posn 18 | ;; interpretation an ordinary Cartesian point 19 | 20 | 21 | ;;; Application 22 | 23 | (define coord-y1 -30) 24 | (define coord-y2 -10) 25 | 26 | (define coord-x1 15) 27 | (define coord-x2 40) 28 | 29 | (define coord-p1 (make-posn 20 25)) 30 | (define coord-p2 (make-posn 45 17)) 31 | 32 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/06-Itemizations-and-Structures/Exercise-110.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-110) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 110. 6 | ;; A checked version of area-of-disk can also enforce 7 | ;; that the arguments to the function are positive numbers, 8 | ;; not just arbitrary numbers. 9 | ;; Modify checked-area-of-disk in this way. 10 | 11 | 12 | (define MESSAGE "area-of-disk: positive number expected") 13 | 14 | ;; Number -> Number 15 | ;; Computes the area of a disk with radius r. 16 | (define (area-of-disk r) 17 | (* 3.14 (* r r))) 18 | 19 | ;; Any -> Number 20 | ;; Checks that v is a proper input for function area-of-disk. 21 | (check-expect (checked-area-of-disk 1) 3.14) 22 | (check-error (checked-area-of-disk 0) MESSAGE) 23 | (check-error (checked-area-of-disk -1) MESSAGE) 24 | (check-error (checked-area-of-disk "a") MESSAGE) 25 | (define (checked-area-of-disk v) 26 | (cond 27 | [(and (number? v) (> v 0)) 28 | (area-of-disk v)] 29 | [else 30 | (error MESSAGE)])) 31 | 32 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/06-Itemizations-and-Structures/Exercise-112.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-112) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 112. 6 | ;; Reformulate the predicate missile-or-not? using an *or* expression. 7 | 8 | 9 | ;; Any -> Boolean 10 | ;; Check that v is a an element of the MissileOrNot collection. 11 | (check-expect (missile-or-not? #false) #true) 12 | (check-expect (missile-or-not? (make-posn 9 2)) #true) 13 | (check-expect (missile-or-not? "yellow") #false) 14 | (check-expect (missile-or-not? #true) #false) 15 | (check-expect (missile-or-not? 10) #false) 16 | (define (missile-or-not? v) 17 | (or (false? v) (posn? v))) 18 | 19 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/06-Itemizations-and-Structures/Exercise-114.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-114) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 114. 6 | ;; Use the predicates from exercise 113 to check 7 | ;; - the space invader world program, 8 | ;; - the virtual pet program (exercise 106), 9 | ;; - and the editor program (A Graphical Editor). 10 | 11 | 12 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/06-Itemizations-and-Structures/Exercise-94.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-94) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 94. 6 | ;; Create your initial scene 7 | ;; from the constants for the tank, the UFO, and the background. 8 | 9 | 10 | (require 2htdp/image) 11 | 12 | ;;; Constants 13 | 14 | (define WIDTH 300) 15 | (define HEIGHT 200) 16 | (define SCENE (empty-scene WIDTH HEIGHT "deepskyblue")) 17 | 18 | (define UFO-HEIGHT 20) 19 | (define UFO-WIDTH (* 2 UFO-HEIGHT)) 20 | (define UFO (overlay 21 | (circle (/ UFO-HEIGHT 2) "solid" "palegreen") 22 | (ellipse UFO-WIDTH (/ UFO-HEIGHT 2) "solid" "green"))) 23 | (define UFO-X-START (/ WIDTH 2)) 24 | (define UFO-Y-START (/ UFO-HEIGHT 2)) 25 | 26 | (define TANK-HEIGHT 10) 27 | (define TANK-WIDTH (* 2 TANK-HEIGHT)) 28 | (define TANK-X-START (/ TANK-WIDTH 2)) 29 | (define TANK-Y (- HEIGHT (/ TANK-HEIGHT 2))) 30 | (define TANK (rectangle TANK-WIDTH TANK-HEIGHT "solid" "midnightblue")) 31 | 32 | 33 | ;;; Application 34 | 35 | (place-image UFO 36 | UFO-X-START UFO-Y-START 37 | (place-image TANK 38 | TANK-X-START TANK-Y 39 | SCENE)) 40 | 41 | -------------------------------------------------------------------------------- /01-Fixed-Size-Data/06-Itemizations-and-Structures/images/cat1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs-studies/htdp/225e7287a6bba1a60eb3bc0e96aeedb2f2d60bdc/01-Fixed-Size-Data/06-Itemizations-and-Structures/images/cat1.png -------------------------------------------------------------------------------- /01-Fixed-Size-Data/06-Itemizations-and-Structures/images/cat2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs-studies/htdp/225e7287a6bba1a60eb3bc0e96aeedb2f2d60bdc/01-Fixed-Size-Data/06-Itemizations-and-Structures/images/cat2.png -------------------------------------------------------------------------------- /01-Fixed-Size-Data/06-Itemizations-and-Structures/images/cat3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs-studies/htdp/225e7287a6bba1a60eb3bc0e96aeedb2f2d60bdc/01-Fixed-Size-Data/06-Itemizations-and-Structures/images/cat3.png -------------------------------------------------------------------------------- /01-Fixed-Size-Data/06-Itemizations-and-Structures/images/cham-v3-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs-studies/htdp/225e7287a6bba1a60eb3bc0e96aeedb2f2d60bdc/01-Fixed-Size-Data/06-Itemizations-and-Structures/images/cham-v3-1.png -------------------------------------------------------------------------------- /01-Fixed-Size-Data/06-Itemizations-and-Structures/images/cham-v3-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs-studies/htdp/225e7287a6bba1a60eb3bc0e96aeedb2f2d60bdc/01-Fixed-Size-Data/06-Itemizations-and-Structures/images/cham-v3-2.png -------------------------------------------------------------------------------- /01-Fixed-Size-Data/06-Itemizations-and-Structures/images/stop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs-studies/htdp/225e7287a6bba1a60eb3bc0e96aeedb2f2d60bdc/01-Fixed-Size-Data/06-Itemizations-and-Structures/images/stop.png -------------------------------------------------------------------------------- /01-Fixed-Size-Data/06-Itemizations-and-Structures/images/walk.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs-studies/htdp/225e7287a6bba1a60eb3bc0e96aeedb2f2d60bdc/01-Fixed-Size-Data/06-Itemizations-and-Structures/images/walk.png -------------------------------------------------------------------------------- /01-Intermezzo/Exercise-116.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-116) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 116. 6 | ;; Explain why the following sentences are syntactically legal expressions. 7 | 8 | 9 | x 10 | ;; - x is a variable 11 | ;; - any variable is a valid expression 12 | 13 | 14 | (= y z) 15 | ;; - y and z are variables 16 | ;; - variables are expressions 17 | ;; - = is a primitive 18 | ;; - (= y z) is a (primitive expr expr) 19 | ;; - (primitive expr expr) is an expression 20 | 21 | 22 | (= (= y z) 0) 23 | ;; - (= y z) is an expression, as shown in the previous example 24 | ;; - 0 is a value 25 | ;; - value is an expression 26 | ;; - = is a primitive 27 | ;; - (primitive expr expr) is an expression 28 | 29 | -------------------------------------------------------------------------------- /01-Intermezzo/Exercise-117.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-117) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 117. 6 | ;; Explain why the following sentences are syntactically illegal. 7 | 8 | 9 | (3 + 4) 10 | ;; - 3 and 4 are values 11 | ;; - values are expressions 12 | ;; - + is a privitive 13 | ;; - (expr primitive expr) is illegal 14 | 15 | 16 | number? 17 | ;; - number? is primitive 18 | ;; - primitive is illegal 19 | 20 | 21 | (x) 22 | ;; - x is a variable 23 | ;; - (variable) is illegal 24 | 25 | -------------------------------------------------------------------------------- /01-Intermezzo/Exercise-118.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-118) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 118. 6 | ;; Explain why the following sentences are syntactically legal definitions. 7 | 8 | 9 | (define (f x) x) 10 | ;; - f and x are variables 11 | ;; - variable is an expression 12 | ;; - (define (variable variable) expr) is a definition 13 | 14 | 15 | (define (f x) y) 16 | ;; - f, x, y are variables 17 | ;; - y is an expression 18 | ;; - (define (variable variable) expr) is a definition 19 | 20 | 21 | (define (f x y) 3) 22 | ;; - f, x, y are variables 23 | ;; - 3 is a value 24 | ;; - value is an expression 25 | ;; - (define (variable variable variable) expr) is a definition 26 | 27 | -------------------------------------------------------------------------------- /01-Intermezzo/Exercise-119.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-119) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 119. 6 | ;; Explain why the following sentences are syntactically illegal. 7 | 8 | 9 | (define (f "x") x) 10 | ;; - f and x are variables 11 | ;; - variable is an expression 12 | ;; - "x" is a string value 13 | ;; - (define (variable value) expr) is illegal 14 | 15 | 16 | (define (f x y z) (x)) 17 | ;; - f, x, y, z are variables 18 | ;; - x is an expression 19 | ;; - (define (variable variable variable variable) (expr)) is illegal 20 | 21 | -------------------------------------------------------------------------------- /01-Intermezzo/Exercise-120.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-120) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 120. 6 | ;; Discriminate the legal from the illegal sentences. 7 | ;; Explain why the sentences are legal or illegal. 8 | ;; Determine whether the legal ones belong to the category expr or def. 9 | 10 | 11 | (x) 12 | ;; - x is a variable 13 | ;; - variable is an expression 14 | ;; - (expr) is illegal 15 | 16 | 17 | (+ 1 (not x)) 18 | ;; - 1 is a value 19 | ;; - value is an expression 20 | ;; - x is a variable 21 | ;; - variable is an expression 22 | ;; - not is a primitive 23 | ;; - (not x) is a (primitive expr) 24 | ;; - (primitive expr) is an expression 25 | ;; - (+ 1 (not x) is a (primitive expr expr) 26 | ;; - (primitive expr expr) is an expression, 27 | ;; though it will not run 28 | ;; because + accepts only number arguments 29 | ;; but (not x) is always a boolean value 30 | 31 | 32 | (+ 1 2 3) 33 | ;; - 1, 2, 3 are values 34 | ;; - values are expressions 35 | ;; - + is a primitive 36 | ;; - (primitive expr expr expr) is an expression 37 | 38 | -------------------------------------------------------------------------------- /01-Intermezzo/Exercise-121.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-121) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 121. 6 | ;; Evaluate the following expressions step-by-step. 7 | 8 | 9 | (+ (* (/ 12 8) 2/3) 10 | (- 20 (sqrt 4))) 11 | ;; == 12 | (+ (* 3/2 2/3) 13 | (- 20 (sqrt 4))) 14 | ;; == 15 | (+ 1 (- 20 (sqrt 4))) 16 | ;; == 17 | (+ 1 (- 20 2)) 18 | ;; == 19 | (+ 1 18) 20 | ;; == 21 | ;; 19 22 | 23 | 24 | (cond 25 | [(= 0 0) #false] 26 | [(> 0 1) (string=? "a" "a")] 27 | [else (= (/ 1 0) 9)]) 28 | ;; == 29 | (cond 30 | [#true #false] 31 | [(> 0 1) (string=? "a" "a")] 32 | [else (= (/ 1 0) 9)]) 33 | ;; == 34 | #false 35 | 36 | 37 | (cond 38 | [(= 2 0) #false] 39 | [(> 2 1) (string=? "a" "a")] 40 | [else (= (/ 1 2) 9)]) 41 | ;; == 42 | (cond 43 | [#false #false] 44 | [(> 2 1) (string=? "a" "a")] 45 | [else (= (/ 1 2) 9)]) 46 | ;; == 47 | (cond 48 | [(> 2 1) (string=? "a" "a")] 49 | [else (= (/ 1 2) 9)]) 50 | ;; == 51 | (cond 52 | [#true (string=? "a" "a")] 53 | [else (= (/ 1 2) 9)]) 54 | ;; == 55 | (string=? "a" "a") 56 | ;; == 57 | #true 58 | 59 | -------------------------------------------------------------------------------- /01-Intermezzo/Exercise-122.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-122) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 122. 6 | ;; Suppose the program contains the definition. 7 | ;; Show how DrRacket evaluates the following expressions, step-by-step. 8 | 9 | 10 | (define (f x y) 11 | (+ (* 3 x) (* y y))) 12 | 13 | 14 | (+ (f 1 2) (f 2 1)) 15 | ;; == 16 | (+ 17 | (+ (* 3 1) (* 2 2)) 18 | (f 2 1)) 19 | ;; == 20 | (+ 21 | (+ 3 (* 2 2)) 22 | (f 2 1)) 23 | ;; == 24 | (+ 25 | (+ 3 4) 26 | (f 2 1)) 27 | ;; == 28 | (+ 29 | 7 30 | (f 2 1)) 31 | ;; == 32 | (+ 33 | 7 34 | (+ (* 3 2) (* 1 1))) 35 | ;; == 36 | (+ 7 (+ 6 (* 1 1))) 37 | ;; == 38 | (+ 7 (+ 6 1)) 39 | ;; == 40 | (+ 7 7) 41 | ;; == 42 | ;; 14 43 | 44 | 45 | (f 1 (* 2 3)) 46 | ;; == 47 | (f 1 6) 48 | ;; == 49 | (+ (* 3 1) (* 6 6)) 50 | ;; == 51 | (+ 3 (* 6 6)) 52 | ;; == 53 | (+ 3 36) 54 | ;; == 55 | ;; 39 56 | 57 | 58 | (f (f 1 (* 2 3)) 19) 59 | ;; == 60 | (f (f 1 6) 19) 61 | ;; == 62 | (f 63 | (+ (* 3 1) (* 6 6)) 64 | 19) 65 | ;; == 66 | (f 67 | (+ 3 (* 6 6)) 68 | 19) 69 | ;; == 70 | (f 71 | (+ 3 36) 72 | 19) 73 | ;; == 74 | (f 39 19) 75 | ;; == 76 | (+ (* 3 39) (* 19 19)) 77 | ;; == 78 | (+ 117 (* 19 19)) 79 | ;; == 80 | (+ 117 361) 81 | ;; == 82 | ;; 478 83 | 84 | -------------------------------------------------------------------------------- /01-Intermezzo/Exercise-123.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-123) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 123. 6 | ;; Write down a rule that shows how to reformulate 7 | ;; (if exp-test exp-then exp-else) 8 | ;; as a cond expression. 9 | 10 | 11 | (cond 12 | [exp-test exp-then] 13 | [else exp-else]) 14 | 15 | -------------------------------------------------------------------------------- /01-Intermezzo/Exercise-125.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-125) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 125. 6 | ;; Discriminate the legal from the illegal sentences. 7 | ;; Explain why the sentences are legal or illegal. 8 | 9 | 10 | (define-struct oops []) 11 | ;; legal 12 | ;; matches (define-struct name) 13 | 14 | (define-struct child [parents dob date]) 15 | ;; legal 16 | ;; matches (define-struct name [name name name]) 17 | 18 | (define-struct (child person) [dob date]) 19 | ;; illegal 20 | ;; (child person) part must be a name 21 | 22 | -------------------------------------------------------------------------------- /01-Intermezzo/Exercise-126.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-126) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 126. 6 | ;; Identify the values among the following expressions, 7 | ;; assuming the definitions area contains these structure type definitions: 8 | 9 | (define-struct point [x y z]) 10 | (define-struct none []) 11 | 12 | 13 | (make-point 1 2 3) 14 | ;; value 15 | ;; structure point 16 | 17 | 18 | (make-point (make-point 1 2 3) 4 5) 19 | ;; value 20 | ;; structure point 21 | 22 | 23 | (make-point (+ 1 2) 3 4) 24 | ;; value 25 | ;; structure point 26 | ;; == (make-point 3 3 4) 27 | 28 | 29 | (make-none) 30 | ;; value 31 | ;; structure none 32 | 33 | 34 | (make-point (point-x (make-point 1 2 3)) 4 5) 35 | ;; value 36 | ;; structure point 37 | ;; == (make-point 1 4 5) 38 | 39 | -------------------------------------------------------------------------------- /01-Intermezzo/Exercise-127.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-127) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 127. 6 | ;; Predict the results of evaluating the following expressions. 7 | 8 | 9 | (define-struct ball [x y speed-x speed-y]) 10 | 11 | 12 | (number? (make-ball 1 2 3 4)) 13 | ;; #false 14 | 15 | 16 | (ball-speed-y (make-ball (+ 1 2) (+ 3 3) 2 3)) 17 | ;; 3 18 | 19 | 20 | (ball-y (make-ball (+ 1 2) (+ 3 3) 2 3)) 21 | ;; 6 22 | 23 | 24 | ;; (ball-x (make-posn 1 2)) 25 | ;; error 26 | 27 | 28 | ;; (ball-speed-y 5) 29 | ;; error 30 | 31 | -------------------------------------------------------------------------------- /01-Intermezzo/Exercise-128.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-128) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 128. 6 | ;; Validate that the following tests fail and explain why. 7 | 8 | 9 | (check-member-of "green" "red" "yellow" "grey") 10 | ;; "green" does not equal to any of "red", "yellow" "grey" 11 | 12 | 13 | (check-within (make-posn #i1.0 #i1.1) 14 | (make-posn #i0.9 #i1.2) 0.01) 15 | ;; the epsilon is too small for this test to pass 16 | 17 | 18 | (check-range #i0.9 #i0.6 #i0.8) 19 | ;; #i0.9 is not between #i0.6 and #i0.8 20 | 21 | 22 | (check-random (make-posn (random 3) (random 9)) 23 | (make-posn (random 9) (random 3))) 24 | ;; the arguments are in the wrong order 25 | 26 | 27 | (check-satisfied 4 odd?) 28 | ;; 4 is not odd 29 | 30 | -------------------------------------------------------------------------------- /02-Arbitrarily-Large-Data/08-Lists/Exercise-130.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-130) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 130. 6 | ;; Create an element of List-of-names that contains five Strings. 7 | 8 | 9 | ;; A List-of-names is one of: 10 | ;; – '() 11 | ;; – (cons String List-of-names) 12 | ;; Represents a list of invitees, by last name. 13 | 14 | 15 | (cons "Liszt" 16 | (cons "Mozart" 17 | (cons "Bach" 18 | (cons "Prokofiev" 19 | (cons "Glass" 20 | '()))))) 21 | 22 | ;;; Question 23 | ;; Explain why 24 | ;; (cons "1" (cons "2" '())) 25 | ;; is an element of List-of-names and why (cons 2 '()) isn’t. 26 | 27 | ;;; Answer 28 | ;; Because "2" is a String, which is a valid first field data type. 29 | ;; 2 is a Number, not a String. So 2 is not a valid first field data type. 30 | 31 | -------------------------------------------------------------------------------- /02-Arbitrarily-Large-Data/08-Lists/Exercise-131.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-131) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 131. 6 | ;; Provide a data definition for representing lists of Boolean values. 7 | ;; The class contains all arbitrarily long lists of Booleans. 8 | 9 | 10 | ;; A List-of-bools is one of: 11 | ;; – '() 12 | ;; – (cons Boolean List-of-bools) 13 | 14 | 15 | ;; Example 16 | (cons #false 17 | (cons #true 18 | (cons #true '()))) 19 | 20 | -------------------------------------------------------------------------------- /02-Arbitrarily-Large-Data/08-Lists/Exercise-134.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-134) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 134. 6 | ;; Develop the contains? function, 7 | ;; which determines whether some given string occurs on a given list of strings. 8 | 9 | 10 | ;; A List-of-strings is one of: 11 | ;; – '() 12 | ;; – (cons String List-of-strings) 13 | 14 | 15 | ;; List-of-strings -> Boolean 16 | ;; Determines whether str is on strings-list. 17 | (check-expect (contains? '() "a") #false) 18 | (check-expect (contains? (cons "a" '()) "a") #true) 19 | (check-expect (contains? (cons "b" '()) "a") #false) 20 | (check-expect (contains? (cons "a" (cons "b" '())) "a") #true) 21 | (check-expect (contains? (cons "b" (cons "a" '())) "a") #true) 22 | (check-expect (contains? (cons "b" (cons "c" '())) "a") #false) 23 | (define (contains? string-list str) 24 | (cond 25 | [(empty? string-list) #false] 26 | [else 27 | (if (string=? str (first string-list)) 28 | #true 29 | (contains? (rest string-list) str))])) 30 | 31 | -------------------------------------------------------------------------------- /02-Arbitrarily-Large-Data/08-Lists/Exercise-135.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-135) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 135. 6 | ;; Use DrRacket’s stepper to check the calculation for 7 | ;; (contains-flatt? (cons "Flatt" (cons "C" '()))) 8 | ;; Also use the stepper to determine the value of 9 | ;; (contains-flatt? 10 | ;; (cons "A" (cons "Flatt" (cons "C" '())))) 11 | 12 | 13 | ;; A List-of-names is one of: 14 | ;; – '() 15 | ;; – (cons String List-of-names) 16 | ;; Represents a contact list for a cell phone. 17 | 18 | ;; List-of-names -> Boolean 19 | ;; Determines whether "Flatt" is on a-list-of-names. 20 | (define (contains-flatt? names) 21 | (cond 22 | [(empty? names) #false] 23 | [(cons? names) 24 | (or (string=? (first names) "Flatt") 25 | (contains-flatt? (rest names)))])) 26 | 27 | 28 | (contains-flatt? (cons "Flatt" (cons "C" '()))) 29 | 30 | 31 | (contains-flatt? 32 | (cons "A" (cons "Flatt" (cons "C" '())))) 33 | 34 | 35 | ;;; Question 36 | ;; What happens when "Flatt" is replaced with "B"? 37 | (contains-flatt? 38 | (cons "A" (cons "B" (cons "C" '())))) 39 | 40 | ;;; Answer 41 | ;; contains-flatt? checks each cons - one by one - and 42 | ;; returns #false as the result. 43 | 44 | -------------------------------------------------------------------------------- /02-Arbitrarily-Large-Data/08-Lists/Exercise-136.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-136) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 136. 6 | ;; Validate with DrRacket’s stepper: 7 | ;; (our-first (our-cons "a" '())) == "a" 8 | ;; (our-rest (our-cons "a" '())) == '() 9 | 10 | 11 | (define-struct pair [left right]) 12 | ; A ConsPair is a structure: 13 | ; (make-pair Any Any). 14 | 15 | ; A ConsOrEmpty is one of: 16 | ; – '() 17 | ; – (make-pair Any ConsOrEmpty) 18 | ; interpretation ConsOrEmpty is the class of all lists 19 | 20 | ; Any Any -> ConsOrEmpty 21 | (define (our-cons a-value a-list) 22 | (cond 23 | [(or (empty? a-list) (pair? a-list)) (make-pair a-value a-list)] 24 | [else (error "cons: second argument ...")])) 25 | 26 | ; ConsOrEmpty -> Any 27 | ; extracts the left part of the given pair 28 | (define (our-first a-list) 29 | (if (empty? a-list) 30 | (error 'our-first "...") 31 | (pair-left a-list))) 32 | 33 | 34 | ; ConsOrEmpty -> Any 35 | ; extracts the right part of the given pair 36 | (define (our-rest a-list) 37 | (if (empty? a-list) 38 | a-list 39 | (pair-right a-list))) 40 | 41 | 42 | ;(our-first (our-cons "a" '())) 43 | 44 | ;(our-rest (our-cons "a" '())) 45 | 46 | -------------------------------------------------------------------------------- /02-Arbitrarily-Large-Data/09-Designing-with-Self-Referential-Data-Definitions/Exercise-137.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-137) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 137. 6 | ;; Compare the template for contains-flatt? with the one for how-many. 7 | ;; Ignoring the function name, they are the same. Explain the similarity. 8 | 9 | 10 | ;; List-of-names -> Boolean 11 | ;; Determines whether "Flatt" is on a-list-of-names. 12 | (define (contains-flatt? alon) 13 | (cond 14 | [(empty? alon) ...] 15 | [else 16 | (... (first alon) ... 17 | ... (contains-flatt? (rest alon)) ...)])) 18 | 19 | 20 | ;; List-of-strings -> Number 21 | ;; Determines how many strings are on alos. 22 | (define (how-many alos) 23 | (cond 24 | [(empty? alos) ...] 25 | [else 26 | (... (first alos) ... 27 | ... (how-many (rest alos)) ...)])) 28 | 29 | 30 | ;;; Answer 31 | ;; Templates are built based on a data definition. 32 | ;; Both of these templates deal with the self-referential List data definition. 33 | ;; Hence structures of the templates are similar. 34 | 35 | -------------------------------------------------------------------------------- /02-Arbitrarily-Large-Data/09-Designing-with-Self-Referential-Data-Definitions/Exercise-138.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-138) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 138. 6 | ;; The following data definition represents sequences of amounts of money. 7 | ;; Design the sum function. 8 | 9 | 10 | ;; A List-of-amounts is one of: 11 | ;; – '() 12 | ;; – (cons PositiveNumber List-of-amounts) 13 | ;; Represents sequences of amounts of money. 14 | 15 | ;; Examples: 16 | ;; '() 17 | ;; (cons 20 '()) 18 | ;; (cons 20 (cons 30 '())) 19 | 20 | 21 | ;; List-of-amounts -> Number 22 | ;; Computes the sum of the amounts. 23 | (check-expect (sum '()) 0) 24 | (check-expect (sum (cons 20 '())) 20) 25 | (check-expect (sum (cons 20 (cons 30 '()))) 50) 26 | #| 27 | ;; Template 28 | (define (sum amounts-list) 29 | (cond 30 | [(empty? amounts-list) ...] 31 | [else 32 | (... (first amounts-list) ... 33 | ... (sum (rest amounts-list)) ...)])) 34 | |# 35 | (define (sum amounts-list) 36 | (cond 37 | [(empty? amounts-list) 0] 38 | [else 39 | (+ 40 | (first amounts-list) 41 | (sum (rest amounts-list)))])) 42 | 43 | 44 | ;;; Application 45 | 46 | ;(sum (cons 10 (cons 20 '()))) 47 | 48 | -------------------------------------------------------------------------------- /02-Arbitrarily-Large-Data/09-Designing-with-Self-Referential-Data-Definitions/Exercise-141.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-141) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 141. 6 | ;; Guess a function that can create the desired result 7 | ;; from the values computed by the sub-expressions. 8 | 9 | 10 | ;; List-of-string -> String 11 | ;; Concatenates all strings in l into one long string. 12 | (check-expect (cat '()) "") 13 | (check-expect (cat (cons "a" (cons "b" '()))) "ab") 14 | (check-expect 15 | (cat (cons "ab" (cons "cd" (cons "ef" '())))) 16 | "abcdef") 17 | #| 18 | ;; Template 19 | (define (cat l) 20 | (cond 21 | [(empty? l) ""] 22 | [else (... (first l) ... (cat (rest l)) ...)])) 23 | |# 24 | (define (cat l) 25 | (cond 26 | [(empty? l) ""] 27 | [else 28 | (string-append 29 | (first l) 30 | (cat (rest l)))])) 31 | 32 | 33 | ;;; Application 34 | 35 | (cat (cons "a" '())) 36 | 37 | -------------------------------------------------------------------------------- /02-Arbitrarily-Large-Data/09-Designing-with-Self-Referential-Data-Definitions/Exercise-145.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-145) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 145. 6 | ;; Design the sorted>? predicate, 7 | ;; which consumes a NEList-of-temperatures 8 | ;; and produces #true if the temperatures are sorted in descending order. 9 | 10 | 11 | ;; A CTemperature is a Number greater than -272. 12 | 13 | ;; An NEList-of-temperatures is one of: 14 | ;; – (cons CTemperature '()) 15 | ;; – (cons CTemperature NEList-of-temperatures) 16 | ;; Represents non-empty lists of Celsius temperatures. 17 | 18 | ;; NEList-of-temperatures -> Boolean 19 | ;; Determines if the temperatures are sorted in descending order. 20 | (check-expect (sorted>? (cons 1 '())) #true) 21 | (check-expect (sorted>? (cons 2 (cons 1 '()))) #true) 22 | (check-expect (sorted>? (cons 1 (cons 2 '()))) #false) 23 | #| 24 | ;; Template 25 | (define (sorted>? ne-l) 26 | (cond 27 | [(empty? (rest ne-l)) ...] 28 | [else (... (first ne-l) ... (sorted>? (rest ne-l)) ...)])) 29 | |# 30 | (define (sorted>? ne-l) 31 | (cond 32 | [(empty? (rest ne-l)) #true] 33 | [else (and 34 | (> (first ne-l) (first (rest ne-l))) 35 | (sorted>? (rest ne-l)))])) 36 | 37 | -------------------------------------------------------------------------------- /02-Arbitrarily-Large-Data/09-Designing-with-Self-Referential-Data-Definitions/Exercise-148.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-148) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 148. 6 | ;; Compare the function definitions from this section 7 | ;; (sum, how-many, all-true, one-true) 8 | ;; with the corresponding function definitions from the preceding sections. 9 | ;; Is it better to work with data definitions 10 | ;; that accommodate empty lists 11 | ;; as opposed to definitions for non-empty lists? 12 | ;; Why? Why not? 13 | 14 | 15 | ;; Answer 16 | ;; Data definitions that accommodate empty lists 17 | ;; cover more use cases with a bit simpler definitions and functions implementation. 18 | ;; Getting more at a lower cost usually is a preferred option. 19 | 20 | -------------------------------------------------------------------------------- /02-Arbitrarily-Large-Data/09-Designing-with-Self-Referential-Data-Definitions/Exercise-151.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-151) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 151. 6 | ;; Design the function multiply. 7 | ;; How does multiply relate to what you know from grade school? 8 | 9 | ;; An N is one of: 10 | ;; – 0 11 | ;; – (add1 N) 12 | ;; Represents the counting numbers. 13 | 14 | 15 | ;; N Number -> Number 16 | (check-expect (multiply 2 3) 6) 17 | (check-expect (multiply 1 3) 3) 18 | (check-expect (multiply 0 3) 0) 19 | (check-expect (multiply 0 pi) 0) 20 | (check-within (multiply 3 pi) (* 3 pi) 0.001) 21 | #| 22 | ;; Template 23 | (define (multiply n x) 24 | (cond 25 | [(zero? n) ...] 26 | [else (... (multiply (sub1 n) x) ...)])) 27 | |# 28 | (define (multiply n x) 29 | (cond 30 | [(zero? n) 0] 31 | [else (+ x (multiply (sub1 n) x))])) 32 | 33 | 34 | ;;; Answer 35 | ;; The flow of this function exhibits what multiplication is basically: 36 | ;; summing up some value with itself n times. 37 | 38 | -------------------------------------------------------------------------------- /02-Arbitrarily-Large-Data/09-Designing-with-Self-Referential-Data-Definitions/Exercise-154.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-154) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 154. 6 | ;; Design the function colors. 7 | ;; It consumes a Russian doll and produces a string of all colors, 8 | ;; separated by a comma and a space. 9 | 10 | 11 | (define-struct layer [color doll]) 12 | 13 | ;; A Doll is one of: 14 | ;; – String 15 | ;; – (make-layer String Doll) 16 | 17 | 18 | ;; Doll -> String 19 | ;; Produces a string of all colors of the d. 20 | (check-expect (colors "red") "red") 21 | (check-expect (colors (make-layer "yellow" (make-layer "green" "red"))) "yellow, green, red") 22 | #| 23 | ;; Template 24 | (define (colors d) 25 | (cond 26 | [(string? d) ...] 27 | [(layer? d) (... (layer-color d) ... (colors (layer-doll d) ...))])) 28 | |# 29 | (define (colors d) 30 | (cond 31 | [(string? d) d] 32 | [(layer? d) (string-append (layer-color d) ", " (colors (layer-doll d)))])) 33 | 34 | -------------------------------------------------------------------------------- /02-Arbitrarily-Large-Data/09-Designing-with-Self-Referential-Data-Definitions/Exercise-155.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-155) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 155. 6 | ;; Design the function inner, 7 | ;; which consumes an RD and produces the (color of the) innermost doll. 8 | 9 | 10 | (define-struct layer [color doll]) 11 | 12 | ;; A Doll is one of: 13 | ;; – String 14 | ;; – (make-layer String Doll) 15 | 16 | 17 | ;; Doll -> String 18 | ;; Produces the color of the innermost doll. 19 | (check-expect (inner "red") "red") 20 | (check-expect (inner (make-layer "yellow" (make-layer "red" "green"))) "green") 21 | #| 22 | ;; Template 23 | (define (inner d) 24 | (cond 25 | [(string? d) ...] 26 | [(layer? d) (... (layer-color d) ... (inner (layer-doll d) ...))])) 27 | |# 28 | (define (inner d) 29 | (cond 30 | [(string? d) d] 31 | [(layer? d) (inner (layer-doll d))])) 32 | 33 | -------------------------------------------------------------------------------- /02-Arbitrarily-Large-Data/09-Designing-with-Self-Referential-Data-Definitions/Exercise-160.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-160) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 160. 6 | ;; Design the functions set+.L and set+.R, 7 | ;; which create a set by adding a number x to some given set s 8 | ;; for the left-hand and right-hand data definition, respectively. 9 | 10 | 11 | ;; A Set-of-numbers-L is one of: 12 | ;; – empty 13 | ;; – (cons Number Set-of-numbers-L) 14 | ;; May contain not unique numbers. 15 | 16 | ;; A Set-of-numbers-R is one of: 17 | ;; – empty 18 | ;; – (cons Number Set-of-numbers-R) 19 | ;; Contains only unique numbers. 20 | 21 | 22 | ;; Set-of-numbers-L -> Set-of-numbers-L 23 | ;; Adds a number x to the given set s. 24 | (check-expect (set+.L '() 1) (cons 1 '())) 25 | (check-expect (set+.L (cons 1 '()) 1) (cons 1 (cons 1 '()))) 26 | (define (set+.L s x) 27 | (cons x s)) 28 | 29 | ;; Set-of-numbers-R -> Set-of-numbers-R 30 | ;; Adds a number x to the given set s. 31 | (check-expect (set+.R '() 1) (cons 1 '())) 32 | (check-expect (set+.R (cons 1 '()) 1) (cons 1 '())) 33 | (define (set+.R s x) 34 | (if (member? x s) 35 | s 36 | (cons x s))) 37 | 38 | -------------------------------------------------------------------------------- /02-Arbitrarily-Large-Data/10-More-on-Lists/Exercise-161.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-161) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 161. 6 | ;; Translate the examples into tests and make sure they all succeed. 7 | ;; Revise the entire program so that changing the wage for everyone 8 | ;; is a single change to the entire program and not several. 9 | 10 | 11 | ;; A List-of-numbers is one of: 12 | ;; - '() 13 | ;; - (cons Number List-of-numbers) 14 | 15 | (define RATE 14) 16 | 17 | ;; List-of-numbers -> List-of-numbers 18 | ;; Computes the weekly wages for all given weekly hours. 19 | (check-expect (wage* '()) '()) 20 | (check-expect (wage* (cons 28 '())) (cons (wage 28) '())) 21 | (check-expect (wage* (cons 4 (cons 2 '()))) (cons (wage 4) (cons (wage 2) '()))) 22 | (define (wage* whrs) 23 | (cond 24 | [(empty? whrs) '()] 25 | [else (cons (wage (first whrs)) (wage* (rest whrs)))])) 26 | 27 | ;; Number -> Number 28 | ;; Computes the wage for h hours of work. 29 | (check-expect (wage 0) 0) 30 | (check-expect (wage 1) (* RATE 1)) 31 | (check-expect (wage 4) (* RATE 4)) 32 | (define (wage h) 33 | (* RATE h)) 34 | 35 | -------------------------------------------------------------------------------- /02-Arbitrarily-Large-Data/10-More-on-Lists/Exercise-163.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-163) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 163. 6 | ;; Design convertFC. 7 | ;; The function converts a list of measurements in Fahrenheit to a list of Celsius measurements. 8 | 9 | 10 | ;; A List-of-numbers is one of: 11 | ;; - '() 12 | ;; - (cons Number List-of-numbers) 13 | 14 | ;; List-of-numbers -> List-of-numbers 15 | ;; Converts a list of measurements in Fahrenheit 16 | ;; to a list of Celsius measurements. 17 | (check-expect (convertFC '()) '()) 18 | (check-within (convertFC (cons 0 (cons 32 (cons 212 '())))) 19 | (cons -17.78 (cons 0 (cons 100 '()))) 0.01) 20 | #| 21 | ;; Template 22 | (define (convertFC fl) 23 | (cond 24 | [(empty? fl) ...] 25 | [else (... (first fl) ... (convertFC (rest fl)) ...)])) 26 | |# 27 | (define (convertFC fl) 28 | (cond 29 | [(empty? fl) '()] 30 | [else (cons (* 5/9 (- (first fl) 32)) (convertFC (rest fl)))])) 31 | 32 | -------------------------------------------------------------------------------- /02-Arbitrarily-Large-Data/10-More-on-Lists/Exercise-167.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-167) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 167. 6 | ;; Design the function sum, 7 | ;; which consumes a list of Posns 8 | ;; and produces the sum of all of its x-coordinates. 9 | 10 | 11 | ;; A List-of-posns is one of: 12 | ;; - '() 13 | ;; - (cons Posn List-of-posns) 14 | 15 | ;; List-of-posns -> Number 16 | ;; Produces the sum of the x-coordinates of Posns on the list lp. 17 | (check-expect (sum '()) 0) 18 | (check-expect (sum (cons (make-posn 10 30) (cons (make-posn 77 88) '()))) 87) 19 | #| 20 | ;; Template 21 | (define (sum lp) 22 | (cond 23 | [(empty? lp) ...] 24 | [(cons? lp) (... (first lp) ... 25 | ... ... (posn-x (first lp)) ... 26 | ... ... (posn-y (first lp)) ... 27 | (sum (rest lp)) ...)])) 28 | |# 29 | (define (sum lp) 30 | (cond 31 | [(empty? lp) 0] 32 | [(cons? lp) (+ (posn-x (first lp)) (sum (rest lp)))])) 33 | 34 | 35 | ;;; Application 36 | 37 | ;(sum (cons (make-posn 10 30) (cons (make-posn 77 88) '()))) 38 | 39 | -------------------------------------------------------------------------------- /02-Arbitrarily-Large-Data/10-More-on-Lists/Exercise-177.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-177) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 177. 6 | ;; Design the function create-editor. 7 | ;; The function consumes two strings and produces an Editor. 8 | ;; The first string is the text to the left of the cursor 9 | ;; and the second string is the text to the right of the cursor. 10 | 11 | 12 | ;; An Lo1S is one of: 13 | ;; - '() 14 | ;; - (cons 1String Lo1S) 15 | 16 | (define-struct editor [pre post]) 17 | ;; An Editor is a structure: 18 | ;; (make-editor Lo1S Lo1S) 19 | 20 | 21 | ;; String String -> Editor 22 | ;; Produces an Editor. 23 | (check-expect (create-editor "" "") (make-editor '() '())) 24 | (check-expect (create-editor "a" "") (make-editor (cons "a" '()) '())) 25 | (check-expect (create-editor "ab" "c") (make-editor (cons "a" (cons "b" '())) (cons "c" '()))) 26 | (define (create-editor s-pre s-post) 27 | (make-editor (explode s-pre) (explode s-post))) 28 | 29 | -------------------------------------------------------------------------------- /02-Arbitrarily-Large-Data/10-More-on-Lists/Exercise-178.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Exercise-178) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 178. 6 | ;; Explain why the template for editor-kh deals with "\t" and "\r" 7 | ;; before it checks for strings of length 1. 8 | 9 | 10 | ;;; Answer 11 | ;; Because the length of "\t" and "\b" equals to 1. 12 | ;; But at the same time, these keys presses have to be handled differently 13 | ;; from other keys presses that also identified by the strings of length 1. 14 | ;; https://docs.racket-lang.org/teachpack/2htdpuniverse.html?q=universe#%28tech._world._keyevent%29 15 | 16 | (string-length "\t") 17 | 18 | (string-length "\b") 19 | 20 | -------------------------------------------------------------------------------- /02-Arbitrarily-Large-Data/10-More-on-Lists/files/ttt.txt: -------------------------------------------------------------------------------- 1 | TTT 2 | 3 | Put up in a place 4 | where it's easy to see 5 | the cryptic admonishment 6 | T.T.T. 7 | 8 | When you feel how depressingly 9 | slowly you climb, 10 | it's well to remember that 11 | Things Take Time. 12 | 13 | Piet Hein 14 | -------------------------------------------------------------------------------- /02-Arbitrarily-Large-Data/11-Design-by-Composition/Exercise-181.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-abbr-reader.ss" "lang")((modname Exercise-181) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 181. 6 | ;; Use list to construct the equivalent of the following lists. 7 | 8 | 9 | (define list-1-1 (cons "a" (cons "b" (cons "c" (cons "d" '()))))) 10 | (define list-2-1 (list "a" "b" "c" "d")) 11 | (check-expect list-1-1 list-2-1) 12 | 13 | (define list-1-2 (cons (cons 1 (cons 2 '())) '())) 14 | (define list-2-2 (list (list 1 2))) 15 | (check-expect list-1-2 list-2-2) 16 | 17 | (define list-1-3 (cons "a" (cons (cons 1 '()) (cons #false '())))) 18 | (define list-2-3 (list "a" (list 1) #false)) 19 | (check-expect list-1-3 list-2-3) 20 | 21 | (define list-1-4 (cons (cons "a" (cons 2 '())) (cons "hello" '()))) 22 | (define list-2-4 (list (list "a" 2) "hello")) 23 | (check-expect list-1-4 list-2-4) 24 | 25 | (define list-1-5 (cons (cons 1 (cons 2 '())) 26 | (cons (cons 2 '()) 27 | '()))) 28 | (define list-2-5 (list (list 1 2) (list 2))) 29 | (check-expect list-1-5 list-2-5) 30 | 31 | -------------------------------------------------------------------------------- /02-Arbitrarily-Large-Data/11-Design-by-Composition/Exercise-182.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-abbr-reader.ss" "lang")((modname Exercise-182) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 182. 6 | ;; Use cons and '() to form the equivalent of the following lists. 7 | 8 | 9 | (define list-1-1 (list 0 1 2 3 4 5)) 10 | (define list-2-1 (cons 0 (cons 1 (cons 2 (cons 3 (cons 4 (cons 5 '()))))))) 11 | (check-expect list-1-1 list-2-1) 12 | 13 | (define list-1-2 (list (list "he" 0) (list "it" 1) (list "lui" 14))) 14 | (define list-2-2 (cons 15 | (cons "he" (cons 0 '())) 16 | (cons 17 | (cons "it" (cons 1 '())) 18 | (cons (cons "lui" (cons 14 '())) '())))) 19 | (check-expect list-1-2 list-2-2) 20 | 21 | (define list-1-3 (list 1 (list 1 2) (list 1 2 3))) 22 | (define list-2-3 (cons 1 (cons 23 | (cons 1 (cons 2 '())) 24 | (cons (cons 1 (cons 2 (cons 3 '()))) '())))) 25 | (check-expect list-1-3 list-2-3) 26 | 27 | -------------------------------------------------------------------------------- /02-Arbitrarily-Large-Data/11-Design-by-Composition/Exercise-183.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-abbr-reader.ss" "lang")((modname Exercise-183) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 183. 6 | ;; On some occasions lists are formed with cons and list. 7 | ;; Reformulate each of the following expressions using only cons or only list. 8 | ;; Use check-expect to check your answers. 9 | 10 | 11 | (define list-1-1 (cons "a" (list 0 #false))) 12 | (define list-2-1 (list "a" 0 #false)) 13 | (check-expect list-1-1 list-2-1) 14 | 15 | (define list-1-2 (list (cons 1 (cons 13 '())))) 16 | (define list-2-2 (list (list 1 13))) 17 | (check-expect list-1-2 list-2-2) 18 | 19 | (define list-1-3 (cons (list 1 (list 13 '())) '())) 20 | (define list-2-3 (list (list 1 (list 13 '())))) 21 | (check-expect list-1-3 list-2-3) 22 | 23 | (define list-1-4 (list '() '() (cons 1 '()))) 24 | (define list-2-4 (list '() '() (list 1))) 25 | (check-expect list-1-4 list-2-4) 26 | 27 | (define list-1-5 (cons "a" (cons (list 1) (list #false '())))) 28 | (define list-2-5 (list "a" (list 1) #false '())) 29 | (check-expect list-1-5 list-2-5) 30 | 31 | -------------------------------------------------------------------------------- /02-Arbitrarily-Large-Data/11-Design-by-Composition/Exercise-184.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-abbr-reader.ss" "lang")((modname Exercise-184) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 184. 6 | ;; Determine the values of the following expressions. 7 | ;; Use check-expect to express your answers. 8 | 9 | 10 | (define list-1 11 | (list (string=? "a" "b") #false)) 12 | 13 | (check-expect list-1 (list #false #false)) 14 | 15 | 16 | (define list-2 17 | (list (+ 10 20) (* 10 20) (/ 10 20))) 18 | 19 | (check-expect list-2 (list 30 200 10/20)) 20 | 21 | 22 | (define list-3 23 | (list "dana" "jane" "mary" "laura")) 24 | 25 | (check-expect list-3 (list "dana" "jane" "mary" "laura")) 26 | 27 | -------------------------------------------------------------------------------- /02-Arbitrarily-Large-Data/11-Design-by-Composition/Exercise-185.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-abbr-reader.ss" "lang")((modname Exercise-185) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 185. 6 | ;; Determine the values of the following expressions. 7 | 8 | 9 | (define expr1 10 | (first (list 1 2 3))) 11 | 12 | (check-expect expr1 1) 13 | 14 | 15 | (define expr2 16 | (rest (list 1 2 3))) 17 | 18 | (check-expect expr2 (list 2 3)) 19 | 20 | 21 | (define expr3 22 | (second (list 1 2 3))) 23 | 24 | (check-expect expr3 2) 25 | 26 | -------------------------------------------------------------------------------- /02-Arbitrarily-Large-Data/12-Projects-Lists/Exercise-199.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-abbr-reader.ss" "lang")((modname Exercise-199) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 199. 6 | ;; Make up examples of Dates, Tracks, and LTracks. 7 | 8 | 9 | (require 2htdp/itunes) 10 | 11 | 12 | ;; year month day hour minute second 13 | (define DATE1 14 | (create-date 2020 2 20 12 0 0)) 15 | (define DATE2 16 | (create-date 2020 5 15 23 15 30)) 17 | (define DATE3 18 | (create-date 2019 12 5 1 20 44)) 19 | (define DATE4 20 | (create-date 2020 6 2 5 15 4)) 21 | 22 | ;; name artist album time track# added play# played 23 | (define TRACK1 24 | (create-track "Joyful Sonata" "T. Nikolayeva" "The Best of Piano" 301641 44 DATE1 77 DATE2)) 25 | (define TRACK2 26 | (create-track "Believer" "Imagine Dragons" "Evolve" 204000 102 DATE3 82 DATE4)) 27 | 28 | (define LTRACKS1 '()) 29 | (define LTRACKS2 (list TRACK1 TRACK2)) 30 | 31 | -------------------------------------------------------------------------------- /02-Arbitrarily-Large-Data/12-Projects-Lists/Exercise-209.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-abbr-reader.ss" "lang")((modname Exercise-209) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 209. 6 | ;; Complete the definitions of string->word and word->string. 7 | 8 | 9 | ;; A Word is one of: 10 | ;; – '() 11 | ;; – (cons 1String Word) 12 | ;; Represents a list of 1Strings (letters). 13 | 14 | 15 | ;; String -> Word 16 | ;; Converts s to the chosen word representation. 17 | (check-expect (string->word "") '()) 18 | (check-expect (string->word "a") (cons "a" '())) 19 | (check-expect (string->word "ab") (list "a" "b")) 20 | (check-expect (string->word "abc") (list "a" "b" "c")) 21 | (define (string->word s) 22 | (explode s)) 23 | 24 | ;; Word -> String 25 | ;; Converts w to a string. 26 | (check-expect (word->string '()) "") 27 | (check-expect (word->string (cons "a" '())) "a") 28 | (check-expect (word->string (list "a" "b")) "ab") 29 | (check-expect (word->string (list "a" "b" "c")) "abc") 30 | (define (word->string w) 31 | (implode w)) 32 | 33 | -------------------------------------------------------------------------------- /02-Arbitrarily-Large-Data/12-Projects-Lists/Exercise-211.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-abbr-reader.ss" "lang")((modname Exercise-211) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 211. 6 | ;; Complete the design of in-dictionary. 7 | 8 | 9 | (require 2htdp/batch-io) 10 | 11 | 12 | ;; On OS X: 13 | (define LOCATION "/usr/share/dict/words") 14 | ;; On LINUX: /usr/share/dict/words or /var/lib/dict/words 15 | ;; On WINDOWS: borrow the word file from your Linux friend 16 | 17 | (define DICTIONARY (read-lines LOCATION)) 18 | 19 | 20 | ;; List-of-strings -> List-of-strings 21 | ;; Picks out all those Strings that occur in the dictionary. 22 | (check-expect (in-dictionary '()) '()) 23 | (check-expect (in-dictionary (list "cat")) (list "cat")) 24 | (check-expect (in-dictionary (list "cat" "ttc" "dog")) (list "cat" "dog")) 25 | (define (in-dictionary los) 26 | (cond 27 | [(empty? los) '()] 28 | [else (if (member? (first los) DICTIONARY) 29 | (cons (first los) (in-dictionary (rest los))) 30 | (in-dictionary (rest los)))])) 31 | 32 | -------------------------------------------------------------------------------- /02-Arbitrarily-Large-Data/12-Projects-Lists/Exercise-226.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-abbr-reader.ss" "lang")((modname Exercise-226) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 226. 6 | ;; Design state=?, an equality predicate for states. 7 | 8 | 9 | ;; FSM-State is a String that specifies a color. 10 | 11 | 12 | ;; FSM-State FSM-State -> Boolean 13 | ;; Verifies equality of two FSM-States. 14 | (check-expect (state=? "red" "green") #false) 15 | (check-expect (state=? "red" "red") #true) 16 | (define (state=? s1 s2) 17 | (string=? s1 s2)) 18 | 19 | -------------------------------------------------------------------------------- /02-Arbitrarily-Large-Data/12-Projects-Lists/Exercise-227.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-abbr-reader.ss" "lang")((modname Exercise-227) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 227. 6 | ;; The BW Machine is an FSM that flips 7 | ;; from black to white and back to black for every key event. 8 | ;; Formulate a data representation for the BW Machine. 9 | 10 | 11 | ;; FSM-State is a String that specifies a color. 12 | 13 | (define-struct transition [current next]) 14 | ;; A Transition is a structure: 15 | ;; (make-transition FSM-State FSM-State) 16 | 17 | ;; An FSM is one of: 18 | ;; - '() 19 | ;; - (cons Transition FSM) 20 | ;; Represents the transitions that a finite state machine 21 | ;; can take from one state to another in reaction to keystrokes. 22 | 23 | 24 | (define BW-Machine 25 | (list (make-transition "black" "white") 26 | (make-transition "white" "black"))) 27 | 28 | -------------------------------------------------------------------------------- /02-Intermezzo/Exercise-231.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-abbr-reader.ss" "lang")((modname Exercise-231) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 231. 6 | ;; Eliminate quote in favor of list from the following expressions. 7 | 8 | 9 | '(1 "a" 2 #false 3 "c") 10 | ; == 11 | (list '1 '"a" '2 '#false '3 '"c") 12 | ; == 13 | (list 1 "a" 2 #false 3 "c") 14 | 15 | 16 | '() 17 | ; == 18 | empty 19 | 20 | 21 | '(("alan" 1000) 22 | ("barb" 2000) 23 | ("carl" 1500)) 24 | ; == 25 | (list '("alan" 1000) '("barb" 2000) '("carl" 1500)) 26 | ; == 27 | (list (list '"alan" '1000) (list '"barb" '2000) (list '"carl" '1500)) 28 | ; == 29 | (list (list "alan" 1000) (list "barb" 2000) (list "carl" 1500)) 30 | 31 | -------------------------------------------------------------------------------- /03-Abstraction/14-Similarities-Everywhere/Exercise-237.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-reader.ss" "lang")((modname Exercise-237) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 237. 6 | ;; Evaluate (squared>? 3 10) and (squared>? 4 10) in DrRacket. 7 | ;; How about (squared>? 5 10)? 8 | 9 | 10 | ;; Number Number -> Boolean 11 | ;; Determines whether the area of a square with side x larger than c. 12 | (define (squared>? x c) 13 | (> (* x x) c)) 14 | 15 | 16 | (squared>? 4 10) 17 | 18 | (squared>? 5 10) 19 | 20 | (squared>? 6 10) 21 | 22 | -------------------------------------------------------------------------------- /03-Abstraction/14-Similarities-Everywhere/Exercise-239.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-reader.ss" "lang")((modname Exercise-239) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 239. 6 | ;; Instantiate the given definition to describe the following classes of data. 7 | ;; Also make one concrete example for each of these three data definitions. 8 | 9 | 10 | ;; A [List X Y] is a structure: 11 | ;; (cons X (cons Y '())) 12 | 13 | ;; pairs of Numbers 14 | ;; [List Number Number] 15 | (define pair-1 (cons 10 (cons 20 '()))) 16 | 17 | ;; pairs of Numbers and 1Strings 18 | ;; [List Number 1String] 19 | (define pair-2 (cons 10 (cons "a" '()))) 20 | 21 | ;; pairs of Strings and Booleans 22 | ;; [List String Boolean] 23 | (define pair-3 (cons "hello" (cons #true '()))) 24 | 25 | -------------------------------------------------------------------------------- /03-Abstraction/14-Similarities-Everywhere/Exercise-240.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-reader.ss" "lang")((modname Exercise-240) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 240. 6 | ;; Make examples for the LStr and LNum. 7 | ;; Abstract over the two. 8 | ;; Then instantiate the abstract definition to get back the originals. 9 | 10 | 11 | (define-struct layer [stuff]) 12 | 13 | ;; An LStr is one of: 14 | ;; – String 15 | ;; – (make-layer LStr) 16 | 17 | 18 | ;; An LNum is one of: 19 | ;; – Number 20 | ;; – (make-layer LNum) 21 | 22 | (define lstr-1 "hello") 23 | (define lstr-2 (make-layer lstr-1)) 24 | (define lstr-3 (make-layer lstr-2)) 25 | 26 | (define lnum-1 42) 27 | (define lnum-2 (make-layer lnum-1)) 28 | (define lnum-3 (make-layer lnum-2)) 29 | 30 | ;; A [Layer-of ITEM] is one of: 31 | ;; - ITEM 32 | ;; - (make-layer [Layer-of ITEM]) 33 | 34 | ;; An LStr is a [Layer-of String] 35 | 36 | ;; An LNum is a [Layer-of Number] 37 | 38 | -------------------------------------------------------------------------------- /03-Abstraction/14-Similarities-Everywhere/Exercise-241.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-reader.ss" "lang")((modname Exercise-241) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 241. 6 | ;; Compare the definitions for NEList-of-temperatures and NEList-of-Booleans. 7 | ;; Then formulate an abstract data definition NEList-of. 8 | 9 | 10 | ;; An [NEList-of ITEM] is one of: 11 | ;; – (cons ITEM '()) 12 | ;; – (cons ITEM [NEList-of ITEM]) 13 | 14 | -------------------------------------------------------------------------------- /03-Abstraction/14-Similarities-Everywhere/Exercise-242.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-reader.ss" "lang")((modname Exercise-242) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 242. 6 | ;; Interpret the following data definitions. 7 | ;; Finish implementing occurs. 8 | 9 | 10 | ;; A [Maybe X] is one of: 11 | ;; – #false 12 | ;; – X 13 | 14 | ;; A [Maybe String] is one of: 15 | ;; - #false 16 | ;; - String 17 | 18 | ;; A [Maybe [List-of String]] is one of: 19 | ;; - #false 20 | ;; - [List-of String] 21 | 22 | ;; A [List-of [Maybe String]] is one of: 23 | ;; - '() 24 | ;; - (cons [Maybe String] [List-of [Maybe String]]) 25 | 26 | 27 | ;; String [List-of String] -> [Maybe [List-of String]] 28 | ;; Returns the remainder of los starting with s 29 | ;; #false otherwise. 30 | (check-expect (occurs "a" '()) #false) 31 | (check-expect (occurs "a" (list "b" "a" "d" "e")) (list "d" "e")) 32 | (check-expect (occurs "a" (list "b" "c" "d")) #false) 33 | (define (occurs s los) 34 | (cond 35 | [(empty? los) #false] 36 | [(string=? s (first los)) (rest los)] 37 | [else (occurs s (rest los))])) 38 | 39 | -------------------------------------------------------------------------------- /03-Abstraction/14-Similarities-Everywhere/Exercise-243.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-reader.ss" "lang")((modname Exercise-243) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 243. 6 | ;; Identify the values among the following expressions. 7 | 8 | 9 | (define (f x) x) 10 | 11 | ;; a list 12 | (cons f '()) 13 | ; == 14 | (list f) 15 | 16 | ;; a value, because f is a function 17 | (f f) 18 | ; == 19 | f 20 | 21 | ;; a list 22 | (cons f (cons 10 (cons (f 10) '()))) 23 | ; == 24 | (list f 10 10) 25 | 26 | -------------------------------------------------------------------------------- /03-Abstraction/14-Similarities-Everywhere/Exercise-244.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-reader.ss" "lang")((modname Exercise-244) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 244. 6 | ;; Argue why the following sentences are now legal. 7 | 8 | 9 | (define (f x) (x 10)) 10 | 11 | (define (f1 x1) (x1 f1)) 12 | 13 | (define (f2 x y) (x 'a y 'b)) 14 | 15 | 16 | ;;; Answer. 17 | ;; Function arguments can be functions themselves. 18 | 19 | 20 | ;;; Application 21 | 22 | ;(f add1) 23 | 24 | -------------------------------------------------------------------------------- /03-Abstraction/14-Similarities-Everywhere/Exercise-245.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-reader.ss" "lang")((modname Exercise-245) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 245. 6 | ;; Develop the function=at-1.2-3-and-5.775? function. 7 | ;; Can we hope to define function=?, 8 | ;; which determines whether two functions from numbers to numbers are equal? 9 | 10 | 11 | ;; Function Function -> Boolean 12 | ;; Determines whether x and y produce the same results for 1.2, 3, and -5.775. 13 | (check-expect (function=at-1.2-3-and-5.775? add1 add1) #true) 14 | (check-expect (function=at-1.2-3-and-5.775? add1 sub1) #false) 15 | (define (function=at-1.2-3-and-5.775? x y) 16 | (and (= (x 1.2) (y 1.2)) 17 | (= (x 3) (y 3)) 18 | (= (x 5.775) (y 5.775)))) 19 | 20 | 21 | ;;; Answer. 22 | ;; No, it is not possible. 23 | ;; First, we cannot test two functions results on all possible numbers. 24 | ;; Second, the halting problem arises considering such a comparing function function=?. 25 | 26 | -------------------------------------------------------------------------------- /03-Abstraction/14-Similarities-Everywhere/Exercise-246.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-reader.ss" "lang")((modname Exercise-246) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 246. 6 | ;; Check step 1 of the last calculation using DrRacket's stepper. 7 | 8 | 9 | (define (extract R l t) 10 | (cond 11 | [(empty? l) '()] 12 | [else (cond 13 | [(R (first l) t) 14 | (cons (first l) 15 | (extract R (rest l) t))] 16 | [else 17 | (extract R (rest l) t)])])) 18 | 19 | 20 | (extract < (cons 6 (cons 4 '())) 5) 21 | 22 | -------------------------------------------------------------------------------- /03-Abstraction/14-Similarities-Everywhere/Exercise-247.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-reader.ss" "lang")((modname Exercise-247) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 247. 6 | ;; Evaluate with DrRacket's stepper. 7 | 8 | 9 | (define (extract R l t) 10 | (cond 11 | [(empty? l) '()] 12 | [else (cond 13 | [(R (first l) t) 14 | (cons (first l) 15 | (extract R (rest l) t))] 16 | [else 17 | (extract R (rest l) t)])])) 18 | 19 | (extract < (cons 8 (cons 4 '())) 5) 20 | 21 | -------------------------------------------------------------------------------- /03-Abstraction/14-Similarities-Everywhere/Exercise-248.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-reader.ss" "lang")((modname Exercise-248) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 248. 6 | ;; Evaluate with DrRacket's stepper. 7 | 8 | 9 | ;; Number Number -> Boolean 10 | ;; Determines whether the area of a square with side x larger than c. 11 | (define (squared>? x c) 12 | (> (* x x) c)) 13 | 14 | (squared>? 3 10) 15 | 16 | (squared>? 4 10) 17 | 18 | -------------------------------------------------------------------------------- /03-Abstraction/14-Similarities-Everywhere/Exercise-249.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-reader.ss" "lang")((modname Exercise-249) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 249. 6 | ;; Evaluate with DrRacket's stepper. 7 | 8 | 9 | (define (f x) x) 10 | 11 | (cons f '()) 12 | 13 | (f f) 14 | 15 | (cons f (cons 10 (cons (f 10) '()))) 16 | 17 | -------------------------------------------------------------------------------- /03-Abstraction/15-Designing-Abstractions/Exercise-251.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-reader.ss" "lang")((modname Exercise-251) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 251. 6 | ;; Design fold1, which is the abstraction of the two functions in figure 93. 7 | 8 | 9 | ;; [List-of Number] Function Number -> Number 10 | ;; Computes g of the numbers on l, 11 | ;; using on-empty value. 12 | (check-expect (fold1 '() + 0) 0) 13 | (check-expect (fold1 '(1) + 0) 1) 14 | (check-expect (fold1 '(1 2) + 0) 3) 15 | (check-expect (fold1 '() * 1) 1) 16 | (check-expect (fold1 '(1) * 1) 1) 17 | (check-expect (fold1 '(1 2) * 1) 2) 18 | (define (fold1 l g on-empty) 19 | (cond 20 | [(empty? l) on-empty] 21 | [else 22 | (g (first l) 23 | (fold1 (rest l) g on-empty))])) 24 | 25 | -------------------------------------------------------------------------------- /03-Abstraction/15-Designing-Abstractions/Exercise-254.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-reader.ss" "lang")((modname Exercise-254) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 254. 6 | ;; Formulate signatures for the following functions: 7 | ;; - sort-n 8 | ;; - sort-s 9 | ;; Then abstract over the two signatures. 10 | ;; Also show that the generalized signature can be instantiated 11 | ;; to describe the signature of a sort function for lists of IRs. 12 | 13 | 14 | ;; sort-n signature 15 | ;; [List-of Number] [Number Number -> Boolean] -> [List-of Number] 16 | 17 | ;; sort-s signature 18 | ;; [List-of String] [String String -> Boolean] -> [List-of String] 19 | 20 | ;; abstract signature 21 | ;; [List-of X] [X X -> Boolean] -> [List-of X] 22 | 23 | ;; sort-IR signature 24 | ;; [List-of IR] [IR IR -> Boolean] -> [List-of IR] 25 | 26 | -------------------------------------------------------------------------------- /03-Abstraction/15-Designing-Abstractions/Exercise-255.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-reader.ss" "lang")((modname Exercise-255) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 255. 6 | ;; Formulate signatures for the following functions: 7 | ;; - map-n 8 | ;; - map-s 9 | ;; Then abstract over the two signatures. 10 | ;; Also show that the generalized signature can be instantiated 11 | ;; to describe the signature of the map1 function above. 12 | 13 | 14 | ;; map-n signature 15 | ;; [List-of Number] [Number -> Number] -> [List-of Number] 16 | 17 | ;; map-s signature 18 | ;; [List-of String] [String -> String] -> [List-of String] 19 | 20 | ;; abstract signature 21 | ;; [List-of X] [X -> X] -> [List-of X] 22 | 23 | ;; map1 signature 24 | ;; [List-of X] [X -> Y] -> [List-of Y] 25 | ;; also covers 26 | ;; [List-of IR] [IR -> String] -> [List-of String] 27 | 28 | -------------------------------------------------------------------------------- /03-Abstraction/16-Using-Abstractions/Exercise-257.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-reader.ss" "lang")((modname Exercise-257) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 257. 6 | ;; Design build-l*st, which works just like build-list. 7 | 8 | 9 | ;; [X] N [N -> X] -> [List-of X] 10 | ;; Constructs a list by applying f to 0, 1, ..., (sub1 n) 11 | ;; (build-l*st n f) == (list (f 0) ... (f (- n 1))) 12 | (check-expect (build-l*st 0 add1) '()) 13 | (check-expect (build-l*st 1 add1) '(1)) 14 | (check-expect (build-l*st 2 add1) '(1 2)) 15 | (check-expect (build-l*st 3 add1) '(1 2 3)) 16 | (define (build-l*st n f) 17 | (cond 18 | [(= 0 n) '()] 19 | [else (add-at-end (f (sub1 n)) (build-l*st (sub1 n) f))])) 20 | 21 | ;; [X] X [List-of -> X] -> [List-of X] 22 | ;; Creates a new list by adding p to the end of l. 23 | (check-expect (add-at-end 10 '()) '(10)) 24 | (check-expect (add-at-end 10 '(1)) '(1 10)) 25 | (check-expect (add-at-end 1 '(10 5)) '(10 5 1)) 26 | (define (add-at-end x l) 27 | (cond 28 | [(empty? l) (cons x '())] 29 | [else (cons (first l) (add-at-end x (rest l)))])) 30 | 31 | -------------------------------------------------------------------------------- /03-Abstraction/16-Using-Abstractions/Exercise-273.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-273) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 273. 6 | ;; Use fold to define map. 7 | 8 | 9 | ;; [X Y] [X -> Y] [List-of X] -> [List-of Y] 10 | (check-expect (map-from-fold add1 '(1 2 3)) '(2 3 4)) 11 | (check-expect (map-from-fold sub1 '(1 2 3)) '(0 1 2)) 12 | (check-expect (map-from-fold tag-with-a '(3 -4 2/5)) '(("a" 4) ("a" -3) ("a" 1.4))) 13 | (define (map-from-fold f lx) 14 | (local ((define (traverse x y) 15 | (cons (f x) y))) 16 | (foldr traverse '() lx))) 17 | 18 | (define (tag-with-a x) 19 | (list "a" (+ x 1))) 20 | 21 | -------------------------------------------------------------------------------- /03-Abstraction/17-Nameless-Functions/Exercise-279.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-279) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 279. 6 | ;; Decide which of the following phrases are legal lambda expressions. 7 | 8 | 9 | ;(lambda (x y) (x y y)) 10 | ;; Invalid. 11 | ;; (x y y) is not a valid expression (see Intermezzo 1). 12 | ;; Using a primitive would make (+ x y y) 13 | ;; a valid expression and fix lambda expression. 14 | 15 | 16 | ;(lambda () 10) 17 | ;; Invalid. 18 | ;; The keyword lambda must be followed 19 | ;; by a sequence of variables. 20 | 21 | 22 | ;(lambda (x) x) 23 | ;; Valid. 24 | ;; The expression consists of the keyword lambda, 25 | ;; a variable enclosed in a pair of parentheses, 26 | ;; followed by a valid expression (variable in this case). 27 | 28 | 29 | ;(lambda (x y) x) 30 | ;; Valid. 31 | ;; Consists of keyword, variables, and a valid expression. 32 | 33 | 34 | ;(lambda x 10) 35 | ;; Invalid. 36 | ;; A variable must be enclosed in a pair of parentheses. 37 | 38 | -------------------------------------------------------------------------------- /03-Abstraction/17-Nameless-Functions/Exercise-280.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-280) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 280. 6 | ;; Calculate the result of the following expressions. 7 | 8 | 9 | ((lambda (x y) (+ x (* x y))) 10 | 1 2) 11 | ; == 12 | (+ 1 (* 1 2)) 13 | ; == 14 | 3 15 | 16 | 17 | ((lambda (x y) 18 | (+ x 19 | (local ((define z (* y y))) 20 | (+ (* 3 z) (/ 1 x))))) 21 | 1 2) 22 | ; == 23 | (+ 1 (+ (* 3 (* 2 2)) (/ 1 1))) 24 | ; == 25 | (+ 1 (+ (* 3 4) 1)) 26 | ; == 27 | (+ 1 (+ 12 1)) 28 | ; == 29 | (+ 1 13) 30 | ; == 31 | 14 32 | 33 | 34 | ((lambda (x y) 35 | (+ x 36 | ((lambda (z) 37 | (+ (* 3 z) (/ 1 z))) 38 | (* y y)))) 39 | 1 2) 40 | ; == 41 | (+ 1 (+ (* 3 4) (/ 1 4))) 42 | ; == 43 | (+ 1 (+ 12 1/4)) 44 | ; == 45 | 13.25 46 | 47 | -------------------------------------------------------------------------------- /03-Abstraction/17-Nameless-Functions/Exercise-282.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-282) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 282. 6 | ;; Experiment with the above definitions in DrRacket. 7 | 8 | 9 | (define (f-plain x) 10 | (* 10 x)) 11 | 12 | (define f-lambda 13 | (lambda (x) 14 | (* 10 x))) 15 | 16 | (check-expect (f-plain 2) (f-lambda 2)) 17 | 18 | ;; Number -> Boolean 19 | (define (compare x) 20 | (= (f-plain x) (f-lambda x))) 21 | 22 | (compare (random 100000)) 23 | 24 | (compare (random 100000)) 25 | 26 | (compare (random 100000)) 27 | 28 | -------------------------------------------------------------------------------- /03-Abstraction/17-Nameless-Functions/Exercise-283.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-283) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 283. 6 | ;; Confirm that DrRacket’s stepper can deal with lambda. 7 | 8 | 9 | (map (lambda (x) (* 10 x)) 10 | '(1 2 3)) 11 | ; == 12 | ; '(10 20 30) 13 | 14 | 15 | (foldl (lambda (name rst) 16 | (string-append name ", " rst)) 17 | "etc." 18 | '("Matthew" "Robby")) 19 | ; == 20 | ; "Robby, Matthew, etc." 21 | 22 | 23 | (define th 10) 24 | (define-struct ir [name price]) 25 | (filter (lambda (ir) (<= (ir-price ir) th)) 26 | (list (make-ir "bear" 10) 27 | (make-ir "doll" 33))) 28 | ; == 29 | ; (list (make-ir "bear" 10)) 30 | 31 | -------------------------------------------------------------------------------- /03-Abstraction/17-Nameless-Functions/Exercise-284.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-284) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 284. 6 | ;; Step through the evaluation of these expressions. 7 | 8 | 9 | ((lambda (x) x) (lambda (x) x)) 10 | ; == 11 | ;(lambda (x) x) 12 | 13 | 14 | ((lambda (x) (x x)) (lambda (x) x)) 15 | ; == 16 | ;((lambda (x) x) (lambda (x) x)) 17 | ; == 18 | ;(lambda (x) x) 19 | 20 | 21 | ;; Be ready to hit STOP. 22 | ;((lambda (x) (x x)) (lambda (x) (x x))) 23 | 24 | -------------------------------------------------------------------------------- /03-Abstraction/17-Nameless-Functions/Exercise-291.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-291) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 291. 6 | ;; Use fold to define map-via-ford, which simulates map. 7 | 8 | 9 | ;; [X Y] [X -> Y] [List-of X] -> [List-of Y] 10 | (check-expect (map-via-fold add1 '(1 2 3)) '(2 3 4)) 11 | (check-expect (map-via-fold sub1 '(1 2 3)) '(0 1 2)) 12 | (check-expect (map-via-fold (lambda (i) (list "a" (+ i 1))) '(3 -4 2/5)) 13 | '(("a" 4) ("a" -3) ("a" 1.4))) 14 | (define (map-via-fold f lx) 15 | (foldr (lambda (x l) (cons (f x) l)) '() lx)) 16 | 17 | -------------------------------------------------------------------------------- /03-Abstraction/17-Nameless-Functions/Exercise-292.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-292) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 292. 6 | ;; Design the function sorted?, which comes with the following signature and purpose statement. 7 | 8 | 9 | ;; [X X -> Boolean] [NEList-of X] -> Boolean 10 | ;; Determines whether l is sorted according to cmp. 11 | (check-expect (sorted? < '()) #true) 12 | (check-expect (sorted? < '(1)) #true) 13 | (check-expect (sorted? < '(1 2 3)) #true) 14 | (check-expect (sorted? < '(2 1 3)) #false) 15 | (define (sorted? cmp l) 16 | (cond 17 | [(or (empty? l) (empty? (rest l))) #true] 18 | [else (and 19 | (cmp (first l) (second l)) 20 | (sorted? cmp (rest l)))])) 21 | 22 | -------------------------------------------------------------------------------- /03-Abstraction/17-Nameless-Functions/Exercise-297.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-297) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 297. 6 | ;; Design the function distance-between. 7 | 8 | 9 | ;; Number Number Posn -> Number 10 | ;; Computes the distance between the points (x, y) and p. 11 | (check-expect (distance-between 10 10 (make-posn 10 10)) 0) 12 | (check-expect (distance-between 3 4 (make-posn 0 0)) 5) 13 | (define (distance-between x y p) 14 | (sqrt (+ (expt (- x (posn-x p)) 2) 15 | (expt (- y (posn-y p)) 2)))) 16 | 17 | -------------------------------------------------------------------------------- /03-Abstraction/17-Nameless-Functions/Exercise-298.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-298) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 298. 6 | ;; Design my-animate. 7 | 8 | 9 | (require 2htdp/image) 10 | (require 2htdp/universe) 11 | 12 | 13 | (define rocket (bitmap "./images/rocket.png")) 14 | 15 | ;; An ImageStream is a function: 16 | ;; [N -> Image] 17 | ;; A stream denotes a series of images. 18 | 19 | ;; ImageStream 20 | ;; Data Example 21 | ;; [N -> Image] 22 | ;(define (create-rocket-scene height) 23 | ; (place-image ROCKET 70 height (empty-image 60 60))) 24 | 25 | 26 | ;; ImageStream N -> Number 27 | ;; Shows the images (s 0), (s 1) and so on 28 | ;; at a rate of 30 images per second up to n images total. 29 | (define (my-animate s n0) 30 | (big-bang 0 31 | [to-draw s] 32 | [on-tick add1 1/30] 33 | [stop-when (lambda (n) (= n n0))])) 34 | 35 | 36 | ;;; Application 37 | 38 | ;; (my-animate create-rocket-scene 45) 39 | (my-animate 40 | (lambda (n) 41 | (place-image rocket 30 n (empty-scene 60 60))) 42 | 45) 43 | 44 | -------------------------------------------------------------------------------- /03-Abstraction/17-Nameless-Functions/images/rocket.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs-studies/htdp/225e7287a6bba1a60eb3bc0e96aeedb2f2d60bdc/03-Abstraction/17-Nameless-Functions/images/rocket.png -------------------------------------------------------------------------------- /03-Intermezzo/Exercise-300.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-300) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 300. 6 | 7 | 8 | (define (P1 X y) 9 | (+ (* X y) 10 | (+ (* 2 X) 11 | (+ (* 2 y) 22)))) 12 | 13 | (define (p2 x) 14 | (+ (* 55 x) (+ x 11))) 15 | 16 | (define (p3 x) 17 | (+ (P1 x 0) 18 | (+ (P1 x 1) (p2 x)))) 19 | 20 | -------------------------------------------------------------------------------- /03-Intermezzo/Exercise-302.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-302) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 302. 6 | 7 | 8 | ;; error: x is used here before its definition 9 | ;(define x (cons 1 x)) 10 | 11 | ;; The right-hand side variable must be defined 12 | ;; before its usage in a constant definition, 13 | ;; so the constant definition could be evaluated immediately. 14 | 15 | ;; Keep (cons 1 x) evaluation and avoid error. 16 | (define x (lambda (x) (cons 1 x))) 17 | 18 | ;; Usage 19 | (x '(2 3)) ; outputs '(1 2 3) 20 | 21 | -------------------------------------------------------------------------------- /03-Intermezzo/Exercise-303.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-303) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 303. 6 | 7 | 8 | (lambda (x y) ; binding occurence 9 | ;---------------- 10 | (+ x 11 | (* x y)) 12 | ;---------------- 13 | ) 14 | 15 | 16 | (lambda (x y) ; binding occurence 17 | ;----------------------------------------- 18 | (+ x 19 | ;;------------------------- and the hole in the outer scope 20 | (local ((define x (* y y))) ; binding occurence 21 | (+ (* 3 x) 22 | (/ 1 x)) 23 | ) 24 | ;;-------------------------- 25 | ) 26 | ;----------------------------------------- 27 | ) 28 | 29 | 30 | (lambda (x y) ; binding occurence 31 | ;------------------------ 32 | (+ x 33 | ( 34 | ;;----------------- the hole in the outer scope 35 | (lambda (x) ; binding occurrence 36 | ;;;---------- 37 | (+ (* 3 x) 38 | (/ 1 x)) 39 | ;;;---------- 40 | ) 41 | ;;----------------- 42 | (* y y))) 43 | ;------------------------ 44 | ) 45 | 46 | -------------------------------------------------------------------------------- /03-Intermezzo/Exercise-304.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-304) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 304. 6 | 7 | 8 | (require 2htdp/abstraction) 9 | 10 | 11 | ;; [X Y] [List-of X] -> [List-of Y] 12 | ;; Produces a list of the same items as on the list l 13 | ;; but paired with their relative index. 14 | (check-expect (enumerate '()) '()) 15 | (check-expect (enumerate (list 2 4 10)) 16 | '((1 2) (2 4) (3 10))) 17 | (check-expect (enumerate '(a b c)) '((1 a) (2 b) (3 c))) 18 | (define (enumerate l) 19 | (build-list 20 | (length l) 21 | (lambda (i) (list (add1 i) (list-ref l i))))) 22 | 23 | 24 | (for/list ([i 2] [j '(a b)]) (list i j)) 25 | 26 | (for*/list ([i 2] [j '(a b)]) (list i j)) 27 | 28 | -------------------------------------------------------------------------------- /03-Intermezzo/Exercise-305.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-305) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 305. 6 | ;; Use loops to define convert-euro. See exercise 267. 7 | 8 | 9 | (require 2htdp/abstraction) 10 | 11 | 12 | (define USD-EUR-RATE 1.06) 13 | 14 | ;; [List-of Number] -> [List-of Number] 15 | ;; Converts a list of US$ amounts into a list of € amounts. 16 | (check-expect (convert-euro '()) '()) 17 | (check-expect (convert-euro (list 1.06 21.2 106)) (list 1 20 100)) 18 | (define (convert-euro lon) 19 | (for/list ([n lon]) 20 | (/ n USD-EUR-RATE))) 21 | 22 | -------------------------------------------------------------------------------- /03-Intermezzo/Exercise-308.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-308) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 308. 6 | ;; Design the function replace, 7 | ;; which substitutes the area code 713 with 281 in a list of phone records. 8 | 9 | 10 | (require 2htdp/abstraction) 11 | 12 | 13 | (define-struct phone [area switch four]) 14 | 15 | (define phone1 (make-phone 713 111 1111)) 16 | (define phone2 (make-phone 555 222 2222)) 17 | (define phone3 (make-phone 713 333 3333)) 18 | 19 | ;; [List-of Phone] -> [List-of Phone] 20 | ;; Substitutes the area code 713 with 281. 21 | (check-expect (replace (list phone1 phone2 phone3)) 22 | (list (make-phone 281 111 1111) 23 | (make-phone 555 222 2222) 24 | (make-phone 281 333 3333))) 25 | (define (replace l) 26 | (for/list ([p l]) 27 | (match p 28 | [(phone a s f) 29 | (make-phone (if (= a 713) 281 a) s f)]))) 30 | 31 | -------------------------------------------------------------------------------- /03-Intermezzo/Exercise-309.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-309) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 309. 6 | ;; Design the function words-on-line, 7 | ;; which determines the number of Strings 8 | ;; per item in a list of list of strings. 9 | 10 | 11 | (require 2htdp/abstraction) 12 | 13 | 14 | ;; [List-of [List-of String]] -> [List-of Number] 15 | ;; Determines the number of Strings per item 16 | ;; in a list of list of strings. 17 | (check-expect (words-on-line '()) '()) 18 | (check-expect (words-on-line '(("cat" "dog") ("am" "fish" "done"))) '(2 3)) 19 | (define (words-on-line lls) 20 | (match lls 21 | ['() '()] 22 | [(cons fst rst) (cons (length fst) (words-on-line rst))])) 23 | 24 | ;; More compact version without matching 25 | (check-expect (words-on-line2 '()) '()) 26 | (check-expect (words-on-line2 '(("cat" "dog") ("am" "fish" "done"))) '(2 3)) 27 | (define (words-on-line2 lls) 28 | (for/list ([los lls]) (length los))) 29 | 30 | -------------------------------------------------------------------------------- /04-Intermezzo/Exercise-415.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-415) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 415. 6 | ;; ISL+ uses +inf.0 to deal with overflow. 7 | ;; Determine the integer n such that (expt #i10.0 n) 8 | ;; is an inexact number while (expt #i10. (+ n 1)) is approximated with +inf.0. 9 | ;; Hint Design a function to compute n. 10 | 11 | 12 | ;; N is one of: 13 | ;; - 0 14 | ;; - (add1 N) 15 | 16 | 17 | ;; N -> N 18 | ;; Determines the integer n such that: 19 | ;; - (expt #i10.0 n) is an inexact number 20 | ;; - (expt #i10.0 (+ n 1)) is approximated with +inf.0. 21 | (define (compute n) 22 | (cond 23 | [(not (integer? (expt #i10.0 n))) (- n 1)] 24 | [else (compute (add1 n))])) 25 | 26 | 27 | ;;; Application 28 | 29 | (compute 0) ; 308 30 | 31 | (expt #i10. 308) 32 | ;; #i1e+308 33 | 34 | (expt #i10. 309) 35 | ;; #i+inf.0 36 | 37 | -------------------------------------------------------------------------------- /04-Intermezzo/Exercise-416.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-416) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 416. 6 | ;; ISL+ uses #i0.0 to approximate underflow. 7 | ;; Determine the smallest integer n such that 8 | ;; (expt #i10.0 n) is still an inexact ISL+ number 9 | ;; and (expt #i10. (- n 1)) is approximated with 0. 10 | 11 | 12 | ;; N is one of: 13 | ;; - 0 14 | ;; - (add1 N) 15 | 16 | 17 | ;; N -> N 18 | ;; Determines the integer n such that: 19 | ;; - (expt #i10.0 n) is an inexact number 20 | ;; - (expt #i10.0 (- n 1)) is approximated with #i0.0. 21 | (define (compute n) 22 | (cond 23 | [(equal? #i0.0 (expt #i10.0 n)) (+ n 1)] 24 | [else (compute (sub1 n))])) 25 | 26 | 27 | ;;; Application 28 | 29 | (compute 0) ; -323 30 | 31 | (expt #i10. -323) 32 | ;; #i9.8813129168249e-324 33 | 34 | (expt #i10. -324) 35 | ;; #i0.0 36 | 37 | -------------------------------------------------------------------------------- /04-Intermezzo/Exercise-417.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-417) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 417. 6 | 7 | 8 | (expt 1.001 1e-12) 9 | ; == 10 | (expt 1001/1000 1/1000000000000) 11 | ; == 12 | #i1.000000000000001 13 | 14 | -------------------------------------------------------------------------------- /04-Intermezzo/Exercise-418.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-418) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 418. 6 | ;; Design my-expt without using expt. 7 | ;; The function raises the first given number 8 | ;; to the power of the second one, a natural number. 9 | 10 | 11 | ;; N is one of: 12 | ;; - 0 13 | ;; - (add1 N) 14 | 15 | ;; Number N -> Number 16 | ;; Raises n to the power of p. 17 | (check-expect (my-expt 2 0) 1) 18 | (check-expect (my-expt 2 1) 2) 19 | (check-expect (my-expt 2 2) 4) 20 | (check-expect (my-expt 2 3) 8) 21 | (check-expect (my-expt 5 3) 125) 22 | (define (my-expt n p) 23 | (cond 24 | [(= 0 p) 1] 25 | [else (* n (my-expt n (sub1 p)))])) 26 | 27 | 28 | ;;; Application 29 | 30 | (define inex (+ 1 #i1e-12)) 31 | (define exac (+ 1 1e-12)) 32 | 33 | (my-expt inex 30) 34 | ;; #i1.0000000000300027 35 | (my-expt exac 30) 36 | ;; 1.0000000000300000000004350... 37 | 38 | -------------------------------------------------------------------------------- /04-Intermezzo/Exercise-419.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-419) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 419. 6 | 7 | 8 | (define JANUS 9 | (list 31.0 10 | #i2e+34 11 | #i-1.2345678901235e+80 12 | 2749.0 13 | -2939234.0 14 | #i-2e+33 15 | #i3.2e+270 16 | 17.0 17 | #i-2.4e+270 18 | #i4.2344294738446e+170 19 | 1.0 20 | #i-8e+269 21 | 0.0 22 | 99.0)) 23 | 24 | 25 | (define (sum l) 26 | (foldl + 0 l)) 27 | 28 | (sum JANUS) 29 | ;; #i99.0 30 | (sum (reverse JANUS)) 31 | ;; #i-1.2345678901234999e+80 32 | (sum (sort JANUS <)) 33 | ;; #i0.0 34 | 35 | (expt 2 #i53.0) 36 | ;; #i9007199254740992.0 37 | (sum (list #i1.0 (expt 2 #i53.0))) 38 | ;; #i9007199254740992.0 39 | (sum (list #i1.0 #i1.0 (expt 2 #i53.0))) 40 | ;; #i9007199254740994.0 41 | 42 | (exact->inexact (sum (map inexact->exact JANUS))) 43 | ;; #i4.2344294738446e+170 44 | 45 | -------------------------------------------------------------------------------- /04-Intermezzo/Exercise-420.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-420) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 420. 6 | 7 | 8 | ;; N is one of: 9 | ;; - 0 10 | ;; - (add1 N) 11 | 12 | 13 | ;; N -> [List-of Number] 14 | ;; Produces the first n elements of a matematical series. 15 | (define (oscillate n) 16 | (local ((define (O i) 17 | (cond 18 | [(> i n) '()] 19 | [else 20 | (cons (expt #i-0.99 i) (O (+ i 1)))]))) 21 | (O 1))) 22 | 23 | 24 | ;;; Application 25 | 26 | (oscillate 15) 27 | 28 | 29 | (define osc (oscillate #i1000.0)) 30 | 31 | (define sum1 (foldl + 0 osc)) 32 | 33 | (define sum2 (foldl + 0 (reverse osc))) 34 | 35 | (- (* 1e+16 sum1) (* 1e+16 sum2)) 36 | ;; #i14.0 37 | 38 | -------------------------------------------------------------------------------- /04-Intertwined-Data/19-The-Poetry-of-S-expressions/Exercise-316.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-316) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 316. 6 | ;; Define atom? function. 7 | 8 | 9 | ;; An Atom is one of: 10 | ;; – Number 11 | ;; – String 12 | ;; – Symbol 13 | 14 | ;; Any -> Boolean 15 | ;; Determines whether any is Atom. 16 | (check-expect (atom? '()) #false) 17 | (check-expect (atom? #true) #false) 18 | (check-expect (atom? 10) #true) 19 | (check-expect (atom? "a") #true) 20 | (check-expect (atom? 'x) #true) 21 | (define (atom? any) 22 | (or 23 | (number? any) 24 | (string? any) 25 | (symbol? any))) 26 | 27 | -------------------------------------------------------------------------------- /04-Intertwined-Data/19-The-Poetry-of-S-expressions/Exercise-321.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-321) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 321. 6 | ;; Abstract the data definitions for S-expr and SL 7 | ;; so that they abstract over the kinds of Atoms that may appear. 8 | 9 | 10 | ;; An X is any atomic data type. 11 | 12 | ;; An SL is a [List-of S-expr] 13 | 14 | ;; An S-expr is one of: 15 | ;; – X 16 | ;; – SL 17 | 18 | -------------------------------------------------------------------------------- /04-Intertwined-Data/19-The-Poetry-of-S-expressions/Exercise-328.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-328) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 328. 6 | 7 | 8 | ;; An Atom is one of: 9 | ;; – Number 10 | ;; – String 11 | ;; – Symbol 12 | 13 | ;; An SL is one of: 14 | ;; – '() 15 | ;; – (cons S-expr SL) 16 | 17 | ;; An S-expr is one of: 18 | ;; – Atom 19 | ;; – SL 20 | 21 | 22 | ;; S-expr Symbol Atom -> S-expr 23 | ;; Replaces all occurrences of old in sexp with new. 24 | (check-expect (substitute '(((world) bye) bye) 'bye '42) 25 | '(((world) 42) 42)) 26 | (check-expect (substitute '(30 "a") 'cat 'fish) '(30 "a")) 27 | (check-expect (substitute '(cat dog) 'cat 'fish) '(fish dog)) 28 | (check-expect (substitute '(((cat) dog) cat) 'cat 'fish) '(((fish) dog) fish)) 29 | (define (substitute sexp old new) 30 | (local ((define (atom? sexp) 31 | (or (number? sexp) (string? sexp) (symbol? sexp)))) 32 | (cond 33 | [(atom? sexp) (if (equal? sexp old) new sexp)] 34 | [else (map (lambda (s) (substitute s old new)) sexp)]))) 35 | 36 | -------------------------------------------------------------------------------- /04-Intertwined-Data/20-Iterative-Refinement/Exercise-329.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-329) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 329. 6 | 7 | ;;; Question 8 | ;; How many times does a file name read! occur in the directory tree TS? 9 | ;;; Answer 10 | ;; 2 times. 11 | 12 | ;;; Question 13 | ;; Can you describe the path from the root directory to the occurrences? 14 | ;;; Answer 15 | ;; Yes: 16 | ;; - TS/read! 17 | ;; - TS/Libs/Docs/read! 18 | 19 | ;;; Question 20 | ;; What is the total size of all the files in the tree? 21 | ;;; Answer 22 | (+ 10 99 52 17 8 2 19) 23 | ;= 207 24 | 25 | ;;; Question 26 | ;; What is the total size of the directory if each directory node has size 1? 27 | ;;; Answer 28 | (+ 207 5) 29 | ;= 212 30 | 31 | ;;; Question 32 | ;; How many levels of directories does it contain? 33 | ;;; Answer 34 | ;; 3 levels. 35 | 36 | -------------------------------------------------------------------------------- /04-Intertwined-Data/20-Iterative-Refinement/Exercise-330.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-330) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 330. 6 | ;; Translate the directory tree in figure 123 7 | ;; into a data representation according to model 1. 8 | 9 | 10 | ;; A File is a String. 11 | 12 | ;; A Dir (short for directory) is one of: 13 | ;; – '() 14 | ;; – (cons File Dir) 15 | ;; – (cons Dir Dir) 16 | 17 | 18 | (define Dir-Text (cons "part1" (cons "part2" (cons "part3" '())))) 19 | 20 | 21 | (define Dir-Code (cons "hang" (cons "draw" '()))) 22 | 23 | (define Dir-Docs (cons "read!" '())) 24 | 25 | (define Dir-Libs (cons Dir-Code (cons Dir-Docs '()))) 26 | 27 | 28 | (define Dir-TS (cons Dir-Text (cons "read!" (cons Dir-Libs '())))) 29 | 30 | (check-expect Dir-TS '(("part1" "part2" "part3") 31 | "read!" 32 | (("hang" "draw") 33 | ("read!")))) 34 | 35 | -------------------------------------------------------------------------------- /04-Intertwined-Data/20-Iterative-Refinement/Exercise-331.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-331) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 331. 6 | ;; Design the function how-many, 7 | ;; which determines how many files a given Dir contains. 8 | 9 | 10 | ;; A File is a String. 11 | 12 | ;; A Dir (short for directory) is one of: 13 | ;; – '() 14 | ;; – (cons File Dir) 15 | ;; – (cons Dir Dir) 16 | 17 | 18 | (define Dir-Text (cons "part1" (cons "part2" (cons "part3" '())))) 19 | 20 | (define Dir-Code (cons "hang" (cons "draw" '()))) 21 | 22 | (define Dir-Docs (cons "read!" '())) 23 | 24 | (define Dir-Libs (cons Dir-Code (cons Dir-Docs '()))) 25 | 26 | (define Dir-TS (cons "read!" (cons Dir-Text (cons Dir-Libs '())))) 27 | 28 | 29 | ;; Dir -> Number 30 | ;; Determines how many files a given Dir contains. 31 | (check-expect (how-many '()) 0) 32 | (check-expect (how-many Dir-Docs) 1) 33 | (check-expect (how-many Dir-TS) 7) 34 | (define (how-many d) 35 | (cond 36 | [(empty? d) 0] 37 | [else (+ (if (string? (first d)) 38 | 1 39 | (how-many (first d))) 40 | (how-many (rest d)))])) 41 | 42 | -------------------------------------------------------------------------------- /04-Intertwined-Data/20-Iterative-Refinement/Exercise-334.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-334) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 334. 6 | ;; Show how to equip a directory with two more attributes: size and readability. 7 | ;; The former measures how much space the directory itself 8 | ;; (as opposed to its content) consumes; 9 | ;; the latter specifies whether anyone else besides the user 10 | ;; may browse the content of the directory. 11 | 12 | 13 | ;; A File is a String. 14 | 15 | ;; An LOFD (short for list of files and directories) is one of: 16 | ;; – '() 17 | ;; – (cons File LOFD) 18 | ;; – (cons Dir LOFD) 19 | 20 | (define-struct dir [name content size r]) 21 | ;; A Dir is a structure: 22 | ;; (make-dir String LOFD Number Boolean) 23 | ;; (make-dir n l s r) represents a directory 24 | ;; with the name n, content l, size s, 25 | ;; and readability status r. 26 | 27 | -------------------------------------------------------------------------------- /04-Intertwined-Data/20-Iterative-Refinement/Exercise-338.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-338) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 338. 6 | ;; Use create-dir to turn some of your directories 7 | ;; into ISL+ data representations. 8 | ;; Then use how-many from exercise 336 9 | ;; to count how many files they contain. 10 | ;; Why are you confident that how-many 11 | ;; produces correct results for these directories? 12 | 13 | 14 | (require htdp/dir) 15 | 16 | 17 | (check-expect (how-many (create-dir "../../03-Intermezzo/")) 10) 18 | (check-expect (how-many (create-dir "../../01-Fixed-Size-Data/01-Arithmetic/")) 11) 19 | (define (how-many d) 20 | (foldr 21 | (lambda (d sum) (+ (how-many d) sum)) 22 | (length (dir-files d)) 23 | (dir-dirs d))) 24 | 25 | 26 | ;;; Answer. 27 | ;; This number of files is easy to verify by counting them in the file system. 28 | ;; Moreover, the function was tested before. 29 | 30 | -------------------------------------------------------------------------------- /04-Intertwined-Data/21-Refining-Interpreters/Exercise-346.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-346) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 346. 6 | ;; Formulate a data definition for the class of values 7 | ;; to which a representation of a BSL expression can evaluate. 8 | 9 | 10 | ;;; Definition according to the chapter examples. 11 | ;; A BSL-value is a Number. 12 | 13 | ;;; A broader definition according to the Intermezzo 1. 14 | ;; A BSL-value is one of: 15 | ;; - Number 16 | ;; - Boolean 17 | ;; - String 18 | ;; - Image 19 | 20 | -------------------------------------------------------------------------------- /04-Intertwined-Data/21-Refining-Interpreters/Exercise-350.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-350) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 350. 6 | ;; What is unusual about the definition of this program 7 | ;; with respect to the design recipe? 8 | 9 | 10 | ;;; Answer. 11 | ;; 12 | ;; 1. parse-sl does not follow a common design recipe for lists. 13 | ;; Instead, parse-sl uses more cond branches, 14 | ;; handling only particular lists with three elements 15 | ;; and returning errors for anything beyond the limits. 16 | ;; 17 | ;; 2. Atom data type is declared to be accepted by parse function 18 | ;; but causes an error if a String or Symbol Atom data is given. 19 | ;; This behavior is a sign of the need for data definitions refinements. 20 | 21 | -------------------------------------------------------------------------------- /04-Intertwined-Data/21-Refining-Interpreters/Exercise-356.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-356) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 356. 6 | ;; Extend the data representation of Interpreting Variables 7 | ;; to include the application of a programmer-defined function. 8 | ;; Represent the following expressions. 9 | 10 | 11 | (define-struct add [left right]) 12 | 13 | (define-struct mul [left right]) 14 | 15 | (define-struct fun [name arg]) 16 | 17 | ;; A BSL-fun-expr is one of: 18 | ;; – Number 19 | ;; – Symbol 20 | ;; – (make-add BSL-fun-expr BSL-fun-expr) 21 | ;; – (make-mul BSL-fun-expr BSL-fun-expr) 22 | ;; - (make-fun Symbol BSL-fun-expr) 23 | 24 | 25 | ;(k (+ 1 1)) 26 | ; == 27 | (make-fun 'k (make-add 1 1)) 28 | 29 | ;(* 5 (k (+ 1 1))) 30 | ; == 31 | (make-mul 5 (make-fun 'k (make-add 1 1))) 32 | 33 | ;(* (i 5) (k (+ 1 1))) 34 | ; == 35 | (make-mul (make-fun 'i 5) (make-fun 'k (make-add 1 1))) 36 | 37 | -------------------------------------------------------------------------------- /04-Intertwined-Data/22-Project-The-Commerce-of-XML/Exercise-363.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-363) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 363. 6 | ;; Reformulate the definition of Xexpr 7 | ;; to isolate the common beginning 8 | ;; and highlight the different kinds of endings. 9 | ;; Eliminate the use of List-of. 10 | 11 | 12 | ;; An Attribute is a list of two items: 13 | ;; (cons Symbol (cons String '())) 14 | 15 | ;; A Body is one of: 16 | ;; - '() 17 | ;; - (cons Xexpr Body) 18 | ;; - (cons [List-of Attribute] Body) 19 | 20 | ;; An Xexpr is a list (cons Symbol Body) 21 | 22 | -------------------------------------------------------------------------------- /04-Intertwined-Data/22-Project-The-Commerce-of-XML/Exercise-364.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-364) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 364. 6 | ;; Represent this XML data as elements of Xexpr. 7 | ;; Which one could be represented in Xexpr.v0 or Xexpr.v1? 8 | 9 | 10 | ;; An Xexpr.v0 (short for X-expression) is a one-item list: 11 | ;; (cons Symbol '()) 12 | 13 | ;; An Xexpr.v1 is a list: 14 | ;; (cons Symbol [List-of Xexpr.v1]) 15 | 16 | ;; An Xexpr.v2 is a list: 17 | ;; – (cons Symbol Body) 18 | ;; – (cons Symbol (cons [List-of Attribute] Body)) 19 | ;; where Body is short for [List-of Xexpr.v2] 20 | ;; An Attribute is a list of two items: 21 | ;; (cons Symbol (cons String '())) 22 | 23 | 24 | ;; 25 | '(transaction ((from "seen-e") (to "seen-f"))) 26 | 27 | ;;
28 | '(ul (li (word) (word)) (li (word))) 29 | 30 | 31 | ;;; Answer. 32 | ;; 'ul could be represented in Xexpr.v1. 33 | 34 | -------------------------------------------------------------------------------- /04-Intertwined-Data/22-Project-The-Commerce-of-XML/Exercise-365.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-365) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 365. 6 | ;; Interpret the following elements of Xexpr.v2 as XML data. 7 | ;; Which ones are elements of Xexpr.v0 or Xexpr.v1? 8 | 9 | 10 | ;; An Xexpr.v0 (short for X-expression) is a one-item list: 11 | ;; (cons Symbol '()) 12 | 13 | ;; An Xexpr.v1 is a list: 14 | ;; (cons Symbol [List-of Xexpr.v1]) 15 | 16 | ;; An Xexpr.v2 is a list: 17 | ;; – (cons Symbol Body) 18 | ;; – (cons Symbol (cons [List-of Attribute] Body)) 19 | ;; where Body is short for [List-of Xexpr.v2] 20 | ;; An Attribute is a list of two items: 21 | ;; (cons Symbol (cons String '())) 22 | 23 | 24 | '(server ((name "example.org"))) 25 | ;; 26 | 27 | '(carcas (board (grass)) (player ((name "sam")))) 28 | ;; 29 | ;; 30 | ;; 31 | ;; 32 | 33 | '(start) 34 | ;; 35 | 36 | 37 | ;;; Answer. 38 | ;; 'start is an element of Xexpr.v0 and Xexpr.v1. 39 | 40 | -------------------------------------------------------------------------------- /04-Intertwined-Data/22-Project-The-Commerce-of-XML/Exercise-368.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-368) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 368. 6 | ;; Formulate a data definition 7 | ;; that replaces the informal “or” signature 8 | ;; for the definition of the list-of-attributes? function. 9 | 10 | 11 | ;; An Xexpr is a list: 12 | ;; – (cons Symbol Body) 13 | ;; – (cons Symbol (cons [List-of Attribute] Body)) 14 | ;; where Body is short for [List-of Xexpr] 15 | ;; An Attribute is a list of two items: 16 | ;; (cons Symbol (cons String '())) 17 | 18 | 19 | ;; An Attrs-or-Xexpr is one of: 20 | ;; - [List-of Attribute] 21 | ;; - Xexpr 22 | 23 | -------------------------------------------------------------------------------- /04-Intertwined-Data/22-Project-The-Commerce-of-XML/Exercise-369.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-369) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 369. 6 | ;; Design find-attr. 7 | ;; The function consumes a list of attributes and a symbol. 8 | ;; If the attributes list associates the symbol with a string, 9 | ;; the function retrieves this string; 10 | ;; otherwise it returns #false. 11 | 12 | 13 | ;; An Xexpr is a list: 14 | ;; – (cons Symbol Body) 15 | ;; – (cons Symbol (cons [List-of Attribute] Body)) 16 | ;; where Body is short for [List-of Xexpr] 17 | ;; An Attribute is a list of two items: 18 | ;; (cons Symbol (cons String '())) 19 | 20 | 21 | (define a0 '((initial "X"))) 22 | (define a1 '((name "observer"))) 23 | (define a2 '((status "free"))) 24 | 25 | (define loa (append a0 a1 a2)) 26 | 27 | 28 | ;; [List-of Attribute] Symbol -> [Maybe String] 29 | ;; Retrieves a string associated with s from loa. 30 | (check-expect (find-attr '() 'x) #false) 31 | (check-expect (find-attr loa 'x) #false) 32 | (check-expect (find-attr loa 'name) "observer") 33 | (define (find-attr loa s) 34 | (local ((define found (assq s loa))) 35 | (if (false? found) 36 | #false 37 | (second found)))) 38 | 39 | -------------------------------------------------------------------------------- /04-Intertwined-Data/22-Project-The-Commerce-of-XML/Exercise-371.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-371) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 371. 6 | ;; Refine the definition of Xexpr 7 | ;; so that you can represent XML elements, 8 | ;; including items in enumerations, that are plain strings. 9 | 10 | 11 | ;; An XWord is '(word ((text String))). 12 | 13 | ;; An Attribute is a list of two items: 14 | ;; (cons Symbol (cons String '())) 15 | 16 | ;; A Body is one of: 17 | ;; - [List-of Xexpr] 18 | ;; - XWord 19 | 20 | ;; An Xexpr is a list: 21 | ;; – (cons Symbol Body) 22 | ;; – (cons Symbol (cons [List-of Attribute] Body)) 23 | 24 | 25 | ;;
    26 | ;;
  • Hey hey
  • 27 | ;;
  • Hiya
  • 28 | ;;
29 | 30 | -------------------------------------------------------------------------------- /04-Intertwined-Data/22-Project-The-Commerce-of-XML/Exercise-374.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-374) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 374. 6 | ;; The data definitions in figure 127 use list. 7 | ;; Rewrite them so they use cons. 8 | ;; Then use the recipe to design 9 | ;; the rendering functions for XEnum and XItem from scratch. 10 | ;; You should come up with the same definitions as in figure 128. 11 | 12 | 13 | ;;; Data Definitions 14 | 15 | ;; An Attribute is a list of two items: 16 | ;; (cons Symbol (cons String '())) 17 | 18 | ;; An XWord is '(word ((text String))). 19 | 20 | ;; An XItem is one of: 21 | ;; – (cons 'li (cons XWord '())) 22 | ;; – (cons 'li (cons [List-of Attribute] (cons XWord '()))) 23 | ;; – (cons 'li (cons XEnum '())) 24 | ;; – (cons 'li (cons [List-of Attribute] (cons XEnum '()))) 25 | 26 | ;; An XEnum is one of: 27 | ;; – (cons 'ul [List-of XItem]) 28 | ;; – (cons 'ul (cons [List-of Attribute] [List-of XItem])) 29 | 30 | -------------------------------------------------------------------------------- /04-Intertwined-Data/22-Project-The-Commerce-of-XML/Exercise-381.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-381) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 381. 6 | ;; The definitions of XMachine and X1T use quote, 7 | ;; which is highly inappropriate for novice program designers. 8 | ;; Rewrite them first to use list and then cons. 9 | 10 | 11 | ;; An FSM-State is a String that specifies a color. 12 | 13 | 14 | ;; An XT is a nested list of this shape: 15 | ;; `(action ((state ,FSM-State) (next ,FSM-State))) 16 | ;; == 17 | ;; (list 'action (list (list 'state FSM-State)) (list 'next FSM-State)) 18 | ;; == 19 | ;; (cons 'action 20 | ;; (cons 21 | ;; (cons (cons 'state (cons FSM-State '())) '()) 22 | ;; (cons (cons 'next (cons FSM-State '())) '()))) 23 | 24 | 25 | ;; An XMachine is a nested list of this shape: 26 | ;; `(machine ((initial ,FSM-State)) [List-of XT]) 27 | ;; == 28 | ;; (list 'machine (list (list 'initial FSM-State)) [List-of XT]) 29 | ;; == 30 | ;; (cons 'machine 31 | ;; (cons 32 | ;; (cons (cons 'initial (cons FSM-State '())) '()) 33 | ;; (cons [List-of XT] '()))) 34 | 35 | -------------------------------------------------------------------------------- /04-Intertwined-Data/22-Project-The-Commerce-of-XML/Exercise-382.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-382) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 382. 6 | ;; Formulate an XML configuration for the BW machine, 7 | ;; which switches from white to black and back for every key event. 8 | ;; Translate the XML configuration into an XMachine representation. 9 | ;; See exercise 227 for an implementation of the machine as a program. 10 | 11 | 12 | #| 13 | 14 | 15 | 16 | 17 | |# 18 | 19 | '(machine ((initial "white")) 20 | (action ((state "white") (next "black"))) 21 | (action ((state "black") (next "white")))) 22 | 23 | -------------------------------------------------------------------------------- /04-Intertwined-Data/22-Project-The-Commerce-of-XML/Exercise-385.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-385) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 385. 6 | ;; Use read-xexpr in DrRacket to view the source code 7 | ;; of the page with the current stock price as an Xexpr. 8 | 9 | 10 | (require 2htdp/batch-io) 11 | 12 | 13 | ;; An Attribute is a list of two items: 14 | ;; (list Symbol String) 15 | 16 | ;; An Attribute* is a [List-of Attribute] 17 | 18 | ;; An Xexpr is one of: 19 | ;; - Symbol 20 | ;; - String 21 | ;; - Number 22 | ;; - (cons Symbol (cons Attribute* [List-of Xexpr])) 23 | ;; - (cons Symbol [List-of Xexpr]) 24 | 25 | 26 | (read-xexpr "./files/Ford.html") 27 | 28 | -------------------------------------------------------------------------------- /04-Intertwined-Data/23-Simultaneous-Processing/Exercise-398.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-398) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 398. 6 | ;; Design value. 7 | ;; The function consumes two equally long lists: 8 | ;; a linear combination and a list of variable values. 9 | ;; It produces the value of the combination for these values. 10 | 11 | 12 | ;; [List-of Number] [List-of Number] -> Number 13 | ;; Produces the value of the linear combination 14 | ;; for the coefficients and values on the lists l1 and l2. 15 | (check-expect (value '() '()) 0) 16 | (check-expect (value '(5) '(2)) 10) 17 | (check-expect (value '(5 17) '(2 10)) (+ 10 170)) 18 | (check-expect (value '(5 17 8) '(3 5 4)) (+ (* 5 3) (* 17 5) (* 8 4))) 19 | (define (value l1 l2) 20 | (cond 21 | [(empty? l1) 0] 22 | [else (+ 23 | (* (first l1) (first l2)) 24 | (value (rest l1) (rest l2)))])) 25 | 26 | -------------------------------------------------------------------------------- /04-Intertwined-Data/23-Simultaneous-Processing/Exercise-402.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-402) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 402. 6 | ;; Reread exercise 354. 7 | ;; Explain the reasoning behind our hint 8 | ;; to think of the given expression as an atomic value at first. 9 | 10 | 11 | ;;; Answer. 12 | ;; Given two complex inputs, only one of which is a list, 13 | ;; it's often more convenient to consider another input as an atomic value 14 | ;; and traverse the list, 15 | ;; performing all required manipulations with the data available at each step. 16 | ;; This approach was described as a "situation 1" in the chapter 17 | ;; "23.5 Designing Functions that Consume Two Complex Inputs". 18 | ;; It was applicable for the exercise 354 case. 19 | 20 | -------------------------------------------------------------------------------- /05-Generative-Recursion/25-Non-standard-Recursion/Exercise-423.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-423) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 423. 6 | ;; Define partition. 7 | ;; It consumes a String s and a natural number n. 8 | ;; The function produces a list of string chunks of size n. 9 | 10 | 11 | ;; N is one of: 12 | ;; - 0 13 | ;; - (add1 N) 14 | 15 | 16 | ;; String N ->[List-of String] 17 | ;; Produces a list of string chunks of size n. 18 | (check-expect (partition "" 0) '()) 19 | (check-expect (partition "" 1) '()) 20 | (check-expect (partition "a" 1) '(("a"))) 21 | (check-expect (partition "a" 2) '(("a"))) 22 | (check-expect (partition "ab" 1) '(("a") ("b"))) 23 | (check-expect (partition "ab" 2) '(("ab"))) 24 | (check-expect (partition "abc" 2) '(("ab") ("c"))) 25 | (check-expect (partition "abcdefgh" 3) '(("abc") ("def") ("gh"))) 26 | (define (partition s n) 27 | (local ((define str-length (string-length s))) 28 | (cond 29 | [(or (zero? str-length) (zero? n)) '()] 30 | [else (if (>= n str-length) 31 | (cons (list s) '()) 32 | (cons (list (substring s 0 n)) 33 | (partition (substring s n str-length) n)))]))) 34 | 35 | -------------------------------------------------------------------------------- /05-Generative-Recursion/25-Non-standard-Recursion/Exercise-424.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-424) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 424. 6 | ;; Draw a quick-sort diagram like the one in figure 148 7 | ;; for (list 11 9 2 18 12 14 4 1). 8 | 9 | 10 | (require 2htdp/image) 11 | 12 | 13 | (bitmap "./images/quick-sort.png") 14 | 15 | -------------------------------------------------------------------------------- /05-Generative-Recursion/25-Non-standard-Recursion/Exercise-429.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-429) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 429. 6 | ;; Use filter to define smallers and largers. 7 | 8 | 9 | ;; See exercise 428. 10 | ;; This example uses filter without defining additional functions. 11 | 12 | 13 | ;; [List-of Number] -> [List-of Number] 14 | ;; Produces a sorted version of alon. 15 | (check-expect (quick-sort< '()) '()) 16 | (check-expect (quick-sort< '(1)) '(1)) 17 | (check-expect (quick-sort< (list 11 5 9 2 4)) (list 2 4 5 9 11)) 18 | (check-expect (quick-sort< (list 3 2 1 1 2 3)) (list 1 1 2 2 3 3)) 19 | (define (quick-sort< alon) 20 | (cond 21 | [(or (empty? alon) (empty? (rest alon))) alon] 22 | [else (local ((define pivot (first alon))) 23 | (append (quick-sort< (filter (lambda (x) (< x pivot)) alon)) 24 | (filter (lambda (x) (= x pivot)) alon) 25 | (quick-sort< (filter (lambda (x) (> x pivot)) alon))))])) 26 | 27 | -------------------------------------------------------------------------------- /05-Generative-Recursion/25-Non-standard-Recursion/images/quick-sort.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs-studies/htdp/225e7287a6bba1a60eb3bc0e96aeedb2f2d60bdc/05-Generative-Recursion/25-Non-standard-Recursion/images/quick-sort.png -------------------------------------------------------------------------------- /05-Generative-Recursion/26-Designing-Algorithms/Exercise-438.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-438) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 438. 6 | ;; In your words: how does greatest-divisor-<= work? 7 | ;; Use the design recipe to find the right words. 8 | ;; Why does the locally defined greatest-divisor-<= recur on (min n m)? 9 | 10 | 11 | ;; N[>= 1] N[>= 1] -> N 12 | ;; Finds the greatest common divisor of n and m. 13 | (check-expect (gcd 6 25) 1) 14 | (check-expect (gcd 18 24) 6) 15 | (define (gcd-structural n m) 16 | (local (;; N -> N 17 | ;; Determines the gcd of n and m less than i. 18 | (define (greatest-divisor-<= i) 19 | (cond 20 | [(= i 1) 1] 21 | [else 22 | (if (= (remainder n i) (remainder m i) 0) 23 | i 24 | (greatest-divisor-<= (- i 1)))]))) 25 | (greatest-divisor-<= (min n m)))) 26 | 27 | 28 | ;;; Answer. 29 | ;; The function tests all natural numbers - 30 | ;; starting with the smaller of the given numbers - 31 | ;; to 1 (in descending order). 32 | ;; It a tested number divides evenly both numbers or equals to 1, 33 | ;; this is the gcd. 34 | 35 | -------------------------------------------------------------------------------- /05-Generative-Recursion/26-Designing-Algorithms/Exercise-439.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-439) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 439. 6 | ;; Copy gcd-structural into DrRacket and evaluate 7 | ;; (time (gcd-structural 101135853 45014640)) 8 | ;; in the interactions area. 9 | 10 | 11 | ;; N[>= 1] N[>= 1] -> N 12 | ;; Finds the greatest common divisor of n and m. 13 | (check-expect (gcd 6 25) 1) 14 | (check-expect (gcd 18 24) 6) 15 | (define (gcd-structural n m) 16 | (local (;; N -> N 17 | ;; Determines the gcd of n and m less than i. 18 | (define (greatest-divisor-<= i) 19 | (cond 20 | [(= i 1) 1] 21 | [else 22 | (if (= (remainder n i) (remainder m i) 0) 23 | i 24 | (greatest-divisor-<= (- i 1)))]))) 25 | (greatest-divisor-<= (min n m)))) 26 | 27 | 28 | ;;; Application 29 | 30 | ;(time (gcd-structural 101135853 45014640)) 31 | 32 | -------------------------------------------------------------------------------- /05-Generative-Recursion/26-Designing-Algorithms/Exercise-440.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-440) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 440. 6 | ;; Copy gcd-generative into the definitions area of DrRacket and evaluate 7 | ;; (time (gcd-generative 101135853 45014640)) 8 | ;; in the interactions area. 9 | 10 | 11 | ;; N[>= 1] N[>= 1] -> N 12 | ;; Finds the greatest common divisor of n and m. 13 | (check-expect (gcd 6 25) 1) 14 | (check-expect (gcd 18 24) 6) 15 | (define (gcd-generative n m) 16 | (local (;; N[>= 1] N[>=1] -> N 17 | ;; Generative recursion. 18 | ;; (gcd L S) == (gcd S (remainder L S)) 19 | (define (clever-gcd L S) 20 | (cond 21 | [(= S 0) L] 22 | [else (clever-gcd S (remainder L S))]))) 23 | (clever-gcd (max m n) (min m n)))) 24 | 25 | 26 | ;;; Application 27 | 28 | (time (gcd-generative 101135853 45014640)) 29 | 30 | -------------------------------------------------------------------------------- /05-Generative-Recursion/27-Variations-on-the-Theme/Exercise-448.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-448) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 448. 6 | ;; The find-root algorithm terminates for all (continuous) 7 | ;; f, left, and right for which the assumption holds. 8 | ;; Formulate a termination argument. 9 | 10 | 11 | ;;; Termination argument. 12 | ;; Note that the distance between left and right 13 | ;; becomes smaller on each recursive step. 14 | ;; Then, after some number of steps, 15 | ;; the distance becomes smaller or equal to EPSILON, 16 | ;; leading to the termination of the function. 17 | 18 | -------------------------------------------------------------------------------- /05-Generative-Recursion/27-Variations-on-the-Theme/images/i-4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs-studies/htdp/225e7287a6bba1a60eb3bc0e96aeedb2f2d60bdc/05-Generative-Recursion/27-Variations-on-the-Theme/images/i-4.png -------------------------------------------------------------------------------- /05-Generative-Recursion/27-Variations-on-the-Theme/images/mi.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs-studies/htdp/225e7287a6bba1a60eb3bc0e96aeedb2f2d60bdc/05-Generative-Recursion/27-Variations-on-the-Theme/images/mi.png -------------------------------------------------------------------------------- /05-Generative-Recursion/27-Variations-on-the-Theme/images/poly.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs-studies/htdp/225e7287a6bba1a60eb3bc0e96aeedb2f2d60bdc/05-Generative-Recursion/27-Variations-on-the-Theme/images/poly.png -------------------------------------------------------------------------------- /05-Generative-Recursion/28-Mathematical-Examples/Exercise-455.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-455) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 455. 6 | ;; Translate the mathematical formula for slope into the function slope, 7 | ;; which maps function f and a number r1 to the slope of f at r1. 8 | 9 | 10 | (require 2htdp/image) 11 | 12 | 13 | (bitmap "./images/xx+x-2.png") 14 | 15 | (define f1 (lambda (i) (+ (* i i) i -2))) 16 | 17 | (define EPSILON 0.001) 18 | 19 | 20 | ;; [Number -> Number] Number -> Number 21 | ;; Produces a slope of f at r1. 22 | (check-expect (slope (lambda (i) i) 0) 1) 23 | (check-expect (slope (lambda (i) i) 10) 1) 24 | (check-expect (slope f1 10) 21) 25 | (check-expect (slope f1 2) 5) 26 | (check-expect (slope f1 -0.5) 0) 27 | (check-expect (slope f1 -2) -3) 28 | (check-expect (slope f1 -2.1) -3.2) 29 | (check-expect (slope f1 -3) -5) 30 | (define (slope f r1) 31 | (local ((define r0 (- r1 EPSILON)) 32 | (define r2 (+ r1 EPSILON)) 33 | (define f@r0 (f r0)) 34 | (define f@r2 (f r2))) 35 | (/ (- f@r2 f@r0) (- r2 r0)))) 36 | ;(/ (- f@r2 f@r0) (* 2 EPSILON)))) 37 | 38 | -------------------------------------------------------------------------------- /05-Generative-Recursion/28-Mathematical-Examples/Exercise-459.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-459) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 459. 6 | ;; Turn the description of the process into an ISL+ function. 7 | ;; Adapt the test cases from figure 165 to this case. 8 | 9 | 10 | (define EPS 0.01) 11 | 12 | (define R 159) 13 | 14 | 15 | ;; [Number -> Number] Number Number -> Number 16 | ;; Computes the area under the graph of f between a and b. 17 | ;; Assumes that (< a b) holds. 18 | (check-within (integrate-rectangles (lambda (x) 20) 12 22) 200 EPS) ; 200 19 | (check-within (integrate-rectangles (lambda (x) (* 2 x)) 0 10) 100 EPS) ; 100 20 | (check-within (integrate-rectangles (lambda (x) (* 3 (sqr x))) 0 10) 1000 EPS) ; 999.9901... 21 | (define (integrate-rectangles f a b) 22 | (local ((define width (/ (- b a) R)) 23 | (define mid (+ a (/ width 2))) 24 | (define (integrate-rectangle i) 25 | (cond 26 | [(= i R) 0] 27 | [else (+ (* width (f (+ mid (* i width)))) 28 | (integrate-rectangle (add1 i)))]))) 29 | (integrate-rectangle 0))) 30 | 31 | -------------------------------------------------------------------------------- /05-Generative-Recursion/28-Mathematical-Examples/images/xx+x-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs-studies/htdp/225e7287a6bba1a60eb3bc0e96aeedb2f2d60bdc/05-Generative-Recursion/28-Mathematical-Examples/images/xx+x-2.png -------------------------------------------------------------------------------- /05-Generative-Recursion/29-Algorithms-that-Backtrack/Exercise-478.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-478) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 478. 6 | ;; Explain why all of these solutions 7 | ;; are just like the three scenarios depicted in figure 173. 8 | ;; This leaves the central square. 9 | ;; Is it possible to place even a second queen 10 | ;; after you place one on the central square of a 3 by 3 board? 11 | 12 | 13 | ;; The boards are symmetric n x n spaces with no rules over positioning directions. 14 | ;; The "threatening rules" are bound to the board's axis only. 15 | ;; When a board is rotated strictly 90 degrees, the axis and rules match the previous position. 16 | ;; Hence a situation on a board repeats itself with such a rotation. 17 | ;; Thus the scenarios are the same having the queen positioned 18 | ;; in all the squares of the marginal rows and columns. 19 | 20 | ;; A queen positioned on the central square threatens all remaining squares of the 3 x 3 board. 21 | ;; So it is not possible no place the second queen in this case. 22 | 23 | -------------------------------------------------------------------------------- /05-Generative-Recursion/29-Algorithms-that-Backtrack/images/graph-168.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs-studies/htdp/225e7287a6bba1a60eb3bc0e96aeedb2f2d60bdc/05-Generative-Recursion/29-Algorithms-that-Backtrack/images/graph-168.png -------------------------------------------------------------------------------- /05-Generative-Recursion/29-Algorithms-that-Backtrack/images/graph-170.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cs-studies/htdp/225e7287a6bba1a60eb3bc0e96aeedb2f2d60bdc/05-Generative-Recursion/29-Algorithms-that-Backtrack/images/graph-170.png -------------------------------------------------------------------------------- /05-Intermezzo/Exercise-486.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-486) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 486. 6 | ;; We stated that the function f(n) = n2 + n belongs to the class O(n2). 7 | ;; Determine the pair of numbers c and bigEnough that verify this claim. 8 | 9 | 10 | #| 11 | 12 | f(n) = n2 + n 13 | g(n) = n2 14 | 15 | 16 | n 1 10 100 1000 2000 17 | 18 | f(n) 2 110 10100 1001000 2002000 19 | 20 | c=1 21 | g(n) 1 100 10000 1000000 2000000 22 | 23 | c=2 24 | g(n) 2 200 20000 2000000 4000000 25 | 26 | 27 | For bigEnough = 1 and c = 2, 28 | 29 | f(n) <= 2 * g(n) for every n >= 1. 30 | 31 | Hence f(n) belongs to the class O(n2). 32 | 33 | |# 34 | 35 | -------------------------------------------------------------------------------- /05-Intermezzo/Exercise-487.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-487) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 487. 6 | ;; Consider the functions f(n) = 2n and g(n) = 1000 n. 7 | ;; Show that g belongs to O(f), which means that f is, abstractly speaking, 8 | ;; more (or at least equally) expensive than g. 9 | ;; If the input size is guaranteed to be between 3 and 12, which function is better? 10 | 11 | 12 | #| 13 | 14 | f(n) = 2n (expt) 15 | g(n) = 1000*n 16 | 17 | 18 | n 1 3 5 10 12 20 30 19 | 20 | g(n) 1000 3000 5000 10000 12000 20000 30000 21 | 22 | c=1 23 | f(n) 2 8 32 1024 4096 1048576 1073741824 24 | 25 | c=10 26 | f(n) 20 80 320 10240 40960 10485760 10737418240 27 | 28 | 29 | g(n) belongs to O(f). 30 | 31 | But for small inputs and small constant c, f(n) is more efficient, 32 | so for input size between 3 and 12 it's better to use f(n). 33 | 34 | |# 35 | 36 | -------------------------------------------------------------------------------- /05-Intermezzo/Exercise-488.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-488) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 488. 6 | ;; Compare f(n) = n * log(n) and g(n) = n2 (expt). 7 | ;; Does f belong to O(g) or g to O(f)? 8 | 9 | 10 | #| 11 | 12 | f(n) = n * log(n) 13 | g(n) = n2 (expt) 14 | 15 | 16 | n 1 3 5 10 12 20 30 17 | 18 | f(n) 0 ~5 ~12 ~33 ~43 ~86 ~147 19 | 20 | g(n) 2 8 32 1024 4096 1048576 1073741824 21 | 22 | 23 | f belong to O(g). 24 | 25 | |# 26 | 27 | -------------------------------------------------------------------------------- /06-Accumulators/31-The-Loss-of-Knowledge/Exercise-489.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-489) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 489. 6 | ;; Reformulate add-to-each using map and lambda. 7 | 8 | 9 | ;; Number [List-of Number] -> [List-of Number] 10 | ;; Adds n to each number on l. 11 | (check-expect (add-to-each 30 '(40 20)) '(70 50)) 12 | (check-expect (cons 50 (add-to-each 50 '(40 110 140 170)))'(50 90 160 190 220)) 13 | (define (add-to-each n l) 14 | (map (lambda (i) (+ n i)) l)) 15 | 16 | ;; [List-of Number] -> [List-of Number] 17 | ;; Converts a list of relative to absolute distances. 18 | ;; the first number represents the distance to the origin. 19 | (check-expect (relative->absolute '(30)) '(30)) 20 | (check-expect (relative->absolute '(40 30)) '(40 70)) 21 | (check-expect (relative->absolute '(50 40 70 30 30)) '(50 90 160 190 220)) 22 | (define (relative->absolute l) 23 | (cond 24 | [(empty? l) '()] 25 | [else (local ((define n (first l))) 26 | (cons n 27 | (map (lambda (i) (+ n i)) 28 | (relative->absolute (rest l)))))])) 29 | 30 | -------------------------------------------------------------------------------- /06-Accumulators/32-Designing-Accumulator-Style-Functions/Exercise-496.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-496) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 496. 6 | ;; What should the value of a be when n0 is 3 and n is 1? 7 | ;; How about when n0 is 10 and n is 8? 8 | 9 | 10 | ;; N is one of: 11 | ;; - 0 12 | ;; - (add1 N) 13 | 14 | ;; N -> N 15 | ;; Computes factorial of n0. 16 | (check-expect (! 3) 6) 17 | (define (! n0) 18 | (local (;; N N -> N 19 | ;; Computes (* n (- n 1) (- n 2) ... 1). 20 | ;; Accumulator a is the product 21 | ;; of the natural numbers in the interval [n0,n). 22 | (define (!/a n a) 23 | (cond 24 | [(zero? n) a] 25 | [else (!/a (sub1 n) (* n a))]))) 26 | (!/a n0 1))) 27 | 28 | ;;; Answer 29 | ;; When n0 is 3 and n is 1, 30 | ;; a is the product of numbers in [3,1), 31 | ;; which is 3 * 2 = 6. 32 | ;; 33 | ;; When n0 is 10 and n is 8, 34 | ;; a is the product of numbers in [10,8), 35 | ;; which is 10 * 9 = 90. 36 | 37 | #| 38 | (! 10) 39 | ;= 40 | (!/a 10 1) 41 | ;= 42 | (!/a 9 (* 10 1)) 43 | ;= 44 | (!/a 8 (* 9 10 1)) 45 | |# 46 | 47 | -------------------------------------------------------------------------------- /06-Accumulators/32-Designing-Accumulator-Style-Functions/Exercise-501.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-501) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 501. 6 | ;; Design an accumulator-style version of add-to-pi. 7 | 8 | 9 | ;; N is one of: 10 | ;; - 0 11 | ;; - (add1 N) 12 | 13 | 14 | ;; N -> Number 15 | ;; Adds n to pi without using +. 16 | (check-within (add-to-pi 2) (+ 2 pi) 0.001) 17 | (define (add-to-pi n) 18 | (cond 19 | [(zero? n) pi] 20 | [else (add1 (add-to-pi (sub1 n)))])) 21 | 22 | 23 | ;; N -> Number 24 | ;; Adds n to pi without using +. 25 | (check-within (add-to-pi.v2 2) (+ 2 pi) 0.001) 26 | (define (add-to-pi.v2 n0) 27 | (local (;; N Number -> Number 28 | ;; Adds n to a, which is initially equals to pi. 29 | ;; Accumulator a is a sum of pi 30 | ;; and the difference between n0 and n. 31 | (define (add-to-pi/acc n a) 32 | (cond 33 | [(zero? n) a] 34 | [else (add-to-pi/acc (sub1 n) (add1 a))]))) 35 | (add-to-pi/acc n0 pi))) 36 | 37 | -------------------------------------------------------------------------------- /06-Accumulators/32-Designing-Accumulator-Style-Functions/Exercise-505.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-505) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 505. 6 | ;; Design the function is-prime?, 7 | ;; which consumes a natural number 8 | ;; and returns #true if it is prime and #false otherwise. 9 | 10 | 11 | ;; N is one of: 12 | ;; - 0 13 | ;; - (add1 N) 14 | 15 | ;; N -> Boolean 16 | ;; Determines whether n is a prime number. 17 | (check-expect (is-prime? 17) #true) 18 | (check-expect (is-prime? 22) #false) 19 | (define (is-prime? n0) 20 | (local (;; N -> Boolean 21 | ;; n is a natural number in the interval [1, n0) 22 | (define (is-prime n) 23 | (cond 24 | [(= n 1) #true] 25 | [else (and (not (= 0 (modulo n0 n))) 26 | (is-prime (sub1 n)))]))) 27 | (is-prime (sub1 n0)))) 28 | 29 | -------------------------------------------------------------------------------- /06-Accumulators/32-Designing-Accumulator-Style-Functions/files/510.txt: -------------------------------------------------------------------------------- 1 | All the world's a stage, And all the men and women merely players; 2 | They have their exits and their entrances; And one man in his time plays many parts, His acts being seven ages. At first the infant, Mewling and puking in the nurse's arms; 3 | And then the whining school-boy, with his satchel And shining morning face, creeping like snail Unwillingly to school. And then the lover, Sighing like furnace, with a woeful ballad Made to his mistress' eyebrow. 4 | Then a soldier, Full of strange oaths, and bearded like the pard, Jealous in honour, sudden and quick in quarrel, Seeking the bubble reputation Even in the cannon's mouth. 5 | And then the justice, In fair round belly with good capon lin'd, With eyes severe and beard of formal cut, Full of wise saws and modern instances; And so he plays his part. The sixth age shifts Into the lean and slipper'd pantaloon, With spectacles on nose and pouch on side; His youthful hose, well sav'd, a world too wide For his shrunk shank; and his big manly voice, Turning again toward childish treble, pipes And whistles in his sound. 6 | Last scene of all, That ends this strange eventful history, Is second childishness and mere oblivion; Sans teeth, sans eyes, sans taste, sans everything. 7 | -------------------------------------------------------------------------------- /06-Accumulators/33-Accumulators-and-Trees/Exercise-511.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-511) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 511. 6 | ;; Explain the scope of each binding occurrence in the above examples. 7 | ;; Draw arrows from the bound to the binding occurrences. 8 | 9 | 10 | #| 11 | (λ (x) x) 12 | (λ (x) <- x) 13 | 14 | (λ (x) y) 15 | y is a free variable 16 | 17 | (λ (y) (λ (x) y)) 18 | ^ | 19 | | | 20 | *---------* 21 | 22 | ((λ (x) x) (λ (x) x)) 23 | ((λ (x) <- x) (λ (x) <- x)) 24 | 25 | ((λ (x) (x x)) (λ (x) (x x))) 26 | 27 | (((λ (y) (λ (x) y)) (λ (z) <- z)) (λ (w) <- w)) 28 | ^ | 29 | | | 30 | *---------* 31 | |# 32 | 33 | -------------------------------------------------------------------------------- /06-Accumulators/33-Accumulators-and-Trees/Exercise-513.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-513) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 513. 6 | ;; Develop a data representation for the same subset of ISL+ 7 | ;; that uses structures instead of lists. 8 | ;; Also provide data representations for ex1, ex2, and ex3 9 | ;; following your data definition. 10 | 11 | 12 | ;; A Lam is one of: 13 | ;; – Symbol 14 | ;; - LExpr 15 | ;; - LApp 16 | 17 | (define-struct lexpr [param body]) 18 | ;; A LExpr is a structure: 19 | ;; (make-struct (list Symbol) Lam) 20 | 21 | (define-struct lapp [fun arg]) 22 | ;; A LApp is a structure: 23 | ;; (make-struct Lam Lam) 24 | 25 | (define ex1 (make-lexpr '(x) 'x)) 26 | (define ex2 (make-lexpr '(x) 'y)) 27 | (define ex3 (make-lexpr '(y) '(λ (x) y))) 28 | 29 | -------------------------------------------------------------------------------- /06-Accumulators/33-Accumulators-and-Trees/Exercise-518.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-518) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 518. 6 | ;; Argue that our-cons takes a constant amount of time 7 | ;; to compute its result, regardless of the size of its input. 8 | 9 | 10 | (define-struct cpair [count left right]) 11 | ;; A [MyList X] is one of: 12 | ;; – '() 13 | ;; – (make-cpair (tech "N") X [MyList X]) 14 | ;; Accumulator: the count field is the number of cpairs. 15 | 16 | ;; Data definitions, via a constructor-function. 17 | (define (our-cons f r) 18 | (cond 19 | [(empty? r) (make-cpair 1 f r)] 20 | [(cpair? r) (make-cpair (+ (cpair-count r) 1) f r)] 21 | [else (error "our-cons: ...")])) 22 | 23 | 24 | ;;; Answer 25 | ;; our-cons performs these actions: 26 | ;; - checks if cpair-right is an empty list 27 | ;; - or checks if cpair-right is an instance of cpair 28 | ;; - and then makes one sum operation. 29 | ;; All of these actions are not dependent on any changing factors, 30 | ;; including the size of the input. 31 | ;; Hence our-cons takes a constant amount of time 32 | ;; to compute its result. 33 | 34 | -------------------------------------------------------------------------------- /06-Accumulators/33-Accumulators-and-Trees/Exercise-519.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Exercise-519) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 5 | ;; Exercise 519. 6 | ;; Is it acceptable to impose the extra cost on cons 7 | ;; for all programs to turn length into a constant-time function? 8 | 9 | 10 | (define-struct cpair [count left right]) 11 | ;; A [MyList X] is one of: 12 | ;; – '() 13 | ;; – (make-cpair (tech "N") X [MyList X]) 14 | ;; Accumulator: the count field is the number of cpairs. 15 | 16 | ;; Data definitions, via a constructor-function. 17 | (define (our-cons f r) 18 | (cond 19 | [(empty? r) (make-cpair 1 f r)] 20 | [(cpair? r) (make-cpair (+ (cpair-count r) 1) f r)] 21 | [else (error "our-cons: ...")])) 22 | 23 | ;; Any -> N 24 | ;; How many items does l contain. 25 | (define (our-length l) 26 | (cond 27 | [(empty? l) 0] 28 | [(cpair? l) (cpair-count l)] 29 | [else (error "my-length: ...")])) 30 | 31 | 32 | ;;; Answer 33 | ;; It is acceptable when a program 34 | ;; makes many calls to our-length function 35 | ;; and performs a small number of writes/removals on cons. 36 | 37 | --------------------------------------------------------------------------------