Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS116 Tutorial 4 Mutation. Review set! begin equal? vs. eq? Memory diagrams.

Similar presentations


Presentation on theme: "CS116 Tutorial 4 Mutation. Review set! begin equal? vs. eq? Memory diagrams."— Presentation transcript:

1 CS116 Tutorial 4 Mutation

2 Review set! begin equal? vs. eq? Memory diagrams

3 Review: set! (set! v new_value) (set-struct-field! s new_value) Used to change the value of v/s to new_value Produces (void) Return type is Void v has to be defined (ex. constant); cannot be a parameter of a function Structure’s name Field of the structure s is a structure; can be a constant or a parameter value type

4 Review: begin ( begin ex1 ex2 … ex n ) Begin allows you to execute a series of code The expressions are executed in order from ex1 to exn Expressions or values (ex. Mutation) Cannot define anything here Expressions or values (ex. Mutation) Cannot define anything here The value = The value produced by exn produced by begin

5 Review: equal? vs. eq? (equal? x y) => true if x and y have the same fields; else false (eq? x y) => true if x and y are aliases; else false Aliases are variables that reference (point to) the same value in memory

6 Rules for drawing memory diagrams 1) For each set!, change one thing and one thing ONLY 2) Atomic values (i.e. number, string, boolean, symbol ) go into the small box beside the variable 3) Complex values (i.e. values too big to fit in the small boxes – lists, structures, functions) go into their own bigger boxes and draw an arrow from the variable’s small box to the value in the big box

7 1. What are the values of x and y after evaluating this code? (define x (make-posn 4 100)) (define y x) (set! x 1000)

8 2. What is produced by the following code? (define p (make-posn 3 7)) (define q (make-posn 3 7)) (define r q) (equal? p q) (eq? p q) (equal? r q) (eq? r q)

9 (define-struct date (day month year)) ;; A date is a structure (make-date d m y) where: ;; * d is a nat in the range [1...30] ;; * m is a nat in the range [1...12] ;; * y is any integer 3. Write a function change-the-year that consumes a date structure birthday, and an integer, new-year. The function should change the year of birthday to new-year. Do not use the function make-date in your solution.

10 (define-struct date (day month year)) ;; A date is a structure (make-date d m y) where: ;; * d is a nat in the range [1...30] ;; * m is a nat in the range [1...12] ;; * y is any integer 4. Using change-the-year, write a function change-year that consumes a list of date structures birthdays, and an integer, new-year. The function should change the year of each date structure in birthdays to new-year and output at the end: ? dates changed where ? is the number of birthday structures mutated. Do not use the function make-date in your solution.

11 5. Consider the following structure to record the wins-losses-ties record of a sports team: (define-struct team-record (wins losses ties)) ;; A team-record is a structure (make-team-record w l t), where ;; w is a nat (number of wins) ;; l is a nat (number of losses) ;; t is a nat (number of ties) Draw the memory diagram corresponding to the following Scheme code: (1) (define record1 (make-team-record 5 2 2)) (2) (define record2 (make-team-record 6 3 1)) (3) (define record3 record2) (4) (set-team-record-wins! record1 6) (5) (set-team-record-ties! record2 2) (6) (set! record2 record1) (7) (set-team-record-losses! record1 (team-record-losses record2))

12 5

13 6. Consider the following structure to record a team's name, number of points and their team-record. Note that points are calculated as: 2 points for a win, 0 for a loss, and 1 for a tie. (define-struct team (name points played)) ;; A team is structure (make-team n p r), where ;; n is a string (name of team) ;; p is a nat (number of points, based on 2 points ;; for a win, 1 for a tie, 0 for a loss). ;; r is a team-record (containing number of wins, ;; losses, ties for team) a) Draw memory diagram for the following Scheme code: (1) (define record4 (make-team-record 4 1 2)) (2) (define aces (make-team "Aces" 10 record4)) (3) (define hawks (make-team "Hawks" 8 (make-team-record 4 2 0)))

14 6

15 6. Consider the following structure to record a team's name, number of points and their team-record. Note that points are calculated as: 2 points for a win, 0 for a loss, and 1 for a tie. (define-struct team (name points played)) ;; A team is structure (make-team n p r), where ;; n is a string (name of team) ;; p is a nat (number of points, based on 2 points ;; for a win, 1 for a tie, 0 for a loss). ;; r is a team-record (containing number of wins, ;; losses, ties for team) b) Write a Scheme function add-loss that consumes a team structure, my-team. The function should increase the number of losses for that team by 1, and produce (void).

16 6. Consider the following structure to record a team's name, number of points and their team-record. Note that points are calculated as: 2 points for a win, 0 for a loss, and 1 for a tie. (define-struct team (name points played)) ;; A team is structure (make-team n p r), where ;; n is a string (name of team) ;; p is a nat (number of points, based on 2 points ;; for a win, 1 for a tie, 0 for a loss). ;; r is a team-record (containing number of wins, ;; losses, ties for team) c) Write Scheme functions add-win and add-tie, which each consume a team structure. The functions should increase the number of wins/ties for that team by 1, and update the total points for that team appropriately.

17 d) Extend the memory diagram from part (a) with the following Scheme code: (add-win aces) (add-tie aces) (add-loss hawks) (define another-aces aces) (add-win another-aces)

18 6


Download ppt "CS116 Tutorial 4 Mutation. Review set! begin equal? vs. eq? Memory diagrams."

Similar presentations


Ads by Google