Logo
  • Home
  • Classes
  • Conveying Computing
  • Exams
  • Fractal Gallery
  • Guides
  • Problem Sets
  • Syllabus

Class 24: Imperative Programming

Posted by David Evans on 19 Oct 2011 in Announcements, Book, Classes | 34 comments

Class 24: Slides [PPTX], Notes [PDF], Code [RKT]

Here is the page with instructions on using ElevenLearning: http://www.cs.virginia.edu/cs1120/on-line-version-of-course-book. Please post your comments/suggestions on it there.

If you have comments/solutions/questions on the book exercises, you can post them directly as comments to this post.

Print Friendly Print Get a PDF version of this webpage PDF

34 Responses to “Class 24: Imperative Programming”

  1. Kevin Liu says:
    19 October 2011 at 3:14 pm

    For Exercise 9.4:

    The value of (mlist-length pair) will be indeterminable. I say that the value will be indeterminable because the program will never end, or keep on going until the computer runs out of memory. The procedure will keep on evaluating pair until it gets to null. The only problem is, pair will never get to null because it is circular, with the cdr being itself. So the program will just keep on adding one for each recursive call, and it will keep on doing recursive calls because pair will never get to null.

    • David Evans says:
      20 October 2011 at 7:51 pm

      Yes, that is correct.

  2. Jordan Chandler says:
    19 October 2011 at 3:54 pm

    I was just looking over the Orders of Growth in the textbook, and in Example 7.1, the end of the first example reads “This is true, since n-7>n+12 for all values n.” I was wondering if this was a mistake or if I misunderstood something?

    • David Evans says:
      20 October 2011 at 7:58 pm

      Opps…this is a mistake. It should say, “This is true, since n-7 <= n + 12 for all values n.” Sorry for the confusion, its fixed now (in my development version).

  3. Reid Moseley says:
    19 October 2011 at 5:14 pm

    Would the answer to exercise 9.6 look something like this?
    (define (mlist-inc! p)
    (if (null? p) null
    (begin (+ mcar 1)
    (mlist-inc! mcdr))))

    • David Evans says:
      20 October 2011 at 8:05 pm

      Its on the right track, but won’t actually change anything about the input list. Instead of (+ mcar 1), you want to modify the value of the mcar of p by using set-mcar!. Here’s a full definition:

      (define (mlist-inc! p)
         (if (null? p)
            (void)
            (begin
               (set-mcar! p (+ 1 (mcar p)))
               (mlist-inc! (mcdr p)))))

      The other change is to use (void) as the result for the base case to make it so mlist-inc! does not produce any value when it is evaluated.

  4. Guy says:
    19 October 2011 at 5:18 pm

    Although this is related to Question 1 from the problem set, I also believe it pertains to Exercise 7.9 from the book.
    If we prove for functions g = (2^n) and f = (3^n) that g is not in Θf, then Θ(2^n) is not equivalent to Θ(3^n). So we must prove that g is in O(f) but not in Ω(f), which implies that g is not in Θf:

    Choose c = 1, and n_0 = 1
    (2^n) g is a member of the set O(f)

    But no matter what values are chosen for c and n_0, we can find an n > n_0 such that the inequality (2^n) >= c(3^n) does not hold.
    Given that (2^n) <= (3^n) for all n if c = 1 (stated above), then we must choose c = c(3^n). However, since c 1, such that c*(3^n) = (1/d)(3^n), which we can re-write as (1/d)(2^n)((3/2)^n). So if ((3/2)^n) = d, then the original inequality gives (2^n) >= (2^n), which is true. But, if we solve for n, then n = log_3/2 (d) = ln(d)/ln(3/2), so that any n we choose greater than ln(d)/ln(3/2) = ln(1/c)/ln(3/2) – where c and d are constants – invalidates the original inequality.
    => g is not a member of Ω(f)

    Together => g is not a member of Θ(f), so Θ(g) is not equivalent to Θ(f)

    • Guy says:
      19 October 2011 at 5:20 pm

      The second paragraph should read

      Choose c = 1, and n_0 = 1
      (2^n) g is a member of the set O(f)

      • Guy says:
        19 October 2011 at 5:21 pm

        Choose c = 1, and n_0 = 1
        “(2^n) g is a member of the set O(f)”

        • Guy says:
          19 October 2011 at 5:21 pm

          Choose c = 1, and n_0 = 1
          (2^n) is less than or equal to (3^n) for all n

          So g is a member of the set O(f)

    • David Evans says:
      20 October 2011 at 8:17 pm

      Yes, this is the right overall strategy and it is correct that Θ(2n) is not equivalent to Θ(3n).

      To prove two sets are not equal, all we have to do is find one element that is in one of the sets but not in the other set. Choosing f = 2n works. It is in Θ(2n). (We could prove this using the definition, but it follows obviously since it is clear that any function grows as fast as itself!) But, 2n is not in Θ(3n), as you proved above. I can’t quite follow the proof, though, but I think this is mostly because of the difficult in writing math in the limited HTML subset WordPress allows in comments.
      Since 2n is in O(3n), to prove 2n is not in Θ(3n), we need to show that 2n is not in Ω(3n). To prove this, we need to show that no matter that values are selected for c and n0, we can select a value of n that invalidates the inequality required by the definition of Ω: for all n ≥ n0, 2n ≥ c × 3n. This is what you need to prove for Question 1c of PS5, so I’ll hold off on providing a full proof.

  5. Odette says:
    19 October 2011 at 9:02 pm

    Possible solution to Exercise 9.1:

    > (define x 2)
    > ( * x (+ (nextx) x))
    > 10 | 12 | 15 | 18 ;; four possible solutions

    • David Evans says:
      20 October 2011 at 8:33 pm

      Yes, this works!

      The possible evaluations are:
      (nextx), xsecond, xfirst → 18
      xsecond, (nextx), xfirst → 15
      xfirst, (nextx), xsecond → 12
      xfirst, xsecond, (nextx) → 10

  6. Odette says:
    19 October 2011 at 9:11 pm

    Also, a question from today’s lecture – how does the make-list procedure we defined differ from the “insto” procedure back in chapter 5? In other words, would (make-list 5) evaluate to (list 1 2 3 4 5)?

    • David Evans says:
      20 October 2011 at 8:38 pm

      The are, indeed, very similar. The way we defined make-list:

      (define (make-list n)
        (if (= n 0) null
          (cons 0 (make-list (- n 1)))))

      it produces a list of n zeros. The only difference to make revintsto is the first input to cons:


      (define (revintsto n)
        (if (= n 0) null
          (cons n (make-list (- n 1)))))

      Making intsto (where the numbers are in order) is a bit more complicated, as explained in Example 5.8.

  7. Alex says:
    19 October 2011 at 11:32 pm

    When you post solutions, it would be helpful to have it in an alonzo-botesque format. That way I don’t have to see every solution before I input it, I can just see right or wrong on my answer with a little “give up” tab that will give me the answer to the exercise if I can’t get it after too many attempts.

    • David Evans says:
      20 October 2011 at 8:39 pm

      Yes, this would definitely be useful, but not simple to produce. Perhaps an enterprising cs1120 student will want to work on this after the course!

  8. David Crouch says:
    20 October 2011 at 12:05 am

    Some questions about the proposed extension for the problem set: Do we need to submit attempted answers for ALL of the exercises in Chapter 9 including the gold starred ones? Or should we simply submit answers for the ones we’ve been having trouble with?

    I would submit comments on the online version of the book, but I have never utilized it. I already have the downloaded pdf version and the physical copy and have assumed those two sufficient.

    • David Evans says:
      20 October 2011 at 8:27 am

      All you need to do is submit a useful comment. It can be about just one of the exercises, and can be either a proposed solution, or a question/attempt at a solution. (All of the other comments posted here appear to satisfy the intent of this.)

  9. bdp3aq says:
    20 October 2011 at 9:31 am

    Exercise 8.1

    list-sort-best-first best case input would be null, because at the beginning this would satisfy the if procedure base case, making it not need to run through the rest of the cases. This running time would be Θ(n) with n being the number of elements in the list, in this case being null so barely any time.

    • David Evans says:
      20 October 2011 at 8:43 pm

      Its true that the input for which list-sort-best-first runs fastest would be null, but this isn’t what is meant by best case input. (For nearly all procedures, the fastest-case input would always be the smallest input, so this isn’t usually an interesting question.)

      What we mean by best case input is the input of size N for which the procedure runs fastest. There are many different lists of length N, and the running time for list-sort-best-first will be different depending on properties of the input list. To answer the question, what you need to do is think about how the actual values in the input list impact the running time, and figure out what properties of the input elements will produce the fastest running time for a give input size.

  10. Anonymous says:
    20 October 2011 at 1:09 pm

    Thank you so much for allowing us to do this and bump the Problem Set! I’m extremely grateful for the extra few days~

  11. Victor says:
    20 October 2011 at 4:37 pm

    For exercise 9.5

    (define (mpair-circular? p)
    (if (null? p) false
    (if (eq? p (mcdr p))
    true
    (mpair-circular? (mcdr p))))

    • David Evans says:
      20 October 2011 at 8:53 pm

      This works for some inputs…but not for all of them. It only detects the circularity when it is a one-element loop (that is, a mcons in the list has a mcdr that points to itself). But, there are lots of other ways an mlist could be circular. For example,

      (define m1 (mlist 1 2 3))
      (set-mcdr! (mcdr (mcdr m1)) m1)

      Evaluating (mpair-circular? m1) should output true for this, but with your definition it will never finish evaluating!

      (Actually implementing mpair-circular? correctly is quite tricky, hence the gold star for this one.)

  12. Anonymous says:
    20 October 2011 at 5:34 pm

    exercise 9.6 possible solution:

    (define (mlist-inc! lst)
    (if (null? lst)
    (error “Oops! That’s not a list”)
    (set! lst (+ 1 (mcar lst))
    (mlist-inc! (mcdr lst))))

    • Alex says:
      20 October 2011 at 8:14 pm

      could you also do this using a map/list-map procedure?

      (define (mlist-inc! lst)(
      if (null? lst)
      (error “Opps! That’s not a list”)
      (map (lambda (x) (+ 1 x)) lst))

      would you run into any problems doing a recursive call doing it the other way because you are running it through with a changed list?

      • Alex says:
        20 October 2011 at 8:21 pm

        if that doesn’t actually mutate the elements of the original list, where does the new list get stored memory wise?

      • David Evans says:
        20 October 2011 at 9:01 pm

        Yes, this is a good way to do it…except that list-map (and the built-in map) procedure only work on regular lists, they don’t work on mlists. You would need to use the mlist-map! procedure (defined in Example 9.2) for this. Then, you could do:

        (define (mlist-inc! p)
          (mlist-map! (lambda (e) (+ e 1)) p))

    • David Evans says:
      20 October 2011 at 8:57 pm

      Do you mean the definition I provided in the earlier comment? That doesn’t create any new mcons cells. It is just replacing the values in the elements of the mcons cells in the input list.

  13. Emily says:
    20 October 2011 at 11:34 pm

    For exercise 9.7.
    Assuming that we have a function mlist-reverse! that reverses a given list (I don’t know if I’m allowed to assume that) you can then (maybe) define mlist-truncate! as…

    (define (mlist-truncate!)
    (if (= 0 (car (mlist-reverse! p)))
    (mlist-reverse!
    (cdr (mlist-reverse! p)))
    (error “list is circular”)))

    • Emily says:
      20 October 2011 at 11:35 pm

      it’s actually (if (null? (car (mlist-reverse p)))

    • David Evans says:
      21 October 2011 at 5:10 pm

      I don’t really understand what you are trying to do here. Reversing the list might help since it makes it easier to find the next-to-last mcons, but this is a pretty awkward way to do this. Instead, I would suggest doing something more like what we did in class today with find-last-mcons, except here we need to find the next-to-last mcons (that is, the base case is when (null? (mcdr (mcdr p))), and remove the last element by doing, (set-mcdr! p null).

  14. David Crouch says:
    20 October 2011 at 11:59 pm

    I’m not sure if I totally understand the “while” procedure, but in the interest of providing a new problem that has not been submitted before, here is my attempt at problem 9.11

    (define ( mlist-map! F p)
    (while (lambda ()(> p null))
    (begin (set-mcar! P (f (mcar p )))
    (mlist-map! F (mcdr p)))))

    • David Evans says:
      21 October 2011 at 5:15 pm

      This is on the right track, but the stopping test doesn’t quite make sense. I think you just mean (lambda () (not (null? p))) for this. Then, the body procedure would be similar to what you have, except there is no recursive call because of the while. Instead, we update the value of p using set!:

      (define (mlist-map! f p)
         (while
            (lambda ()
               (not (null? p))
            (lambda ()
               (set-mcar! p (f (mcar p)))
               (set! p (mcdr p)))))


Fall 2011

Register
Login

Help Schedule

(all in Davis Commons, except Dave's office hours in Rice 507)
Sundays, 1-6pm (Valerie/Joseph/Kristina)
Mondays, noon-1:30pm (Kristina)
Mondays, 1:15-2:00pm (Dave, Rice 507)
Tuesdays, 11am-noon (Dave, Rice 507)
Tuesdays, 5-8pm (Valerie/Jonathan)
Wednesdays, 5-6:30pm (Jiamin)
Thursdays, 9:45-11am (Dave, Rice 507)
Thursdays, 1-2:30pm (Joseph)
Thursdays, 4:30-7:30pm (Jonathan/Jiamin)
Fridays, noon-1:30pm (Peter)

Recent Posts

  • Course Wrap-Up
  • Class 41: The Cake of Computing
  • PS8 Submissions
  • Class 40: GuardRails, Big Data, and Secure Computation
  • Exam 2 Solutions

Recent Comments

  • David Evans on Problem Sets
  • jacob777 on Problem Sets
  • Prof. K.R. Chowdhary on Class 41: The Cake of Computing
  • Anon on Exams
  • Anon on Exams

Index

  • Classes
    • Class 1: Computing
    • Class 2: Language
    • Class 3: Rules of Evaluation
    • Class 4: Constructing Procedures
    • Class 5: Procedures Practice
    • Class 6: Programming with Data
    • Class 7: Programming with Lists
    • Class 8: Recursive List Procedures
    • Class 9: Consistent Hashing
  • Conveying Computing
  • Exams
  • Fractal Gallery
  • Guides
    • DrRacket Guide
    • Schemer’s Guide to Python
  • Problem Sets
    • Problem Set 0: Course Registration, Racket
    • Problem Set 1: Making Mosaics
      • PS1 Comments
    • Problem Set 2: Sequence Alignment
      • PS2 Comments
    • Problem Set 3: Limning L-System Fractals
      • PS3 – Comments
    • Problem Set 4: Constructing Colossi
      • PS4 – Comments
    • Problem Set 5: Wahoo! Auctions
      • PS5 Comments
    • Problem Set 6: Adventures in Charlottansville
      • PS6 Comments
    • Problem Set 7: Charming Snakes with Mesmerizing Memoizers
      • PS7 Comments
      • PS7 Responses
    • Problem Set 8 (Part 2): Typed Aazda
    • Problem Set 8: From Aazda to aaZda (Part 1)
      • PS8 Part 1 Comments
  • Syllabus
    • Course Pledge
  • Using These Materials

RSS BA Computer Science

RSS Jefferson’s Wheel

  • ICLR 2022: Understanding Intrinsic Robustness Using Label Uncertainty
  • Microsoft Research Summit: Surprising (and unsurprising) Inference Risks in Machine Learning
  • UVA News Article
  • Model-Targeted Poisoning Attacks with Provable Convergence

RSS Hacker News

  • Engineering students create edible adhesive tape to keep burrito wrapped tightly
  • Gitee to review all code by temporarily closing public open-source projects
  • Windows XP Delta Edition
  • Crypto Might Have an Insider Trading Problem
  • Pipewire to replace Pulseaudion on Ubuntu 22.10

RSS Babbage

cs1120 | RSS | Comments RSS | Book | Using These Materials | Login | Admin | Powered by Wordpress