1 / 7

A Common Pitfall set! of a function parameter doesn’t work as you might think!

A Common Pitfall set! of a function parameter doesn’t work as you might think! (define mylist '(1 2 3)) ==> mylist (define (alter1 <function>) (method ((s <list>)) (set! s '(4 5 6)))) ==> alter1 (alter1 mylist) ==> (4 5 6) mylist ==> ???. A Common Pitfall

marnin
Download Presentation

A Common Pitfall set! of a function parameter doesn’t work as you might think!

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. A Common Pitfall • set! of a function parameter • doesn’t work as you might think! • (define mylist '(1 2 3)) • ==> mylist • (define (alter1 <function>) • (method ((s <list>)) (set! s '(4 5 6)))) • ==> alter1 • (alter1 mylist) • ==> (4 5 6) • mylist • ==> ???

  2. A Common Pitfall • set! of a function parameter • doesn’t work as you might think! • (define mylist '(1 2 3)) • ==> mylist • (define (alter1 <function>) • (method ((s <list>)) (set! s '(4 5 6)))) • ==> alter1 • (alter1 mylist) • ==> (4 5 6) • mylist • ==> (1 2 3)

  3. However: (define mylist '(1 2 3)) ==> mylist (define (alter2 <function>) (method ((l <list>)) (set! (tail l) '(4 5 6)))) (alter2 mylist) ==> (4 5 6) mylist ==> ???

  4. However: (define mylist '(1 2 3)) ==> mylist (define (alter2 <function>) (method ((l <list>)) (set! (tail l) '(4 5 6)))) (alter2 mylist) ==> (4 5 6) mylist ==> (1 4 5 6)

  5. Operations on a priority queue (make-prioq) return empty structure (insert! e pq) put entry e in pq (extract-min! pq) remove highest priority entry in pq and return it

  6. Vectors (a.k.a. arrays) (n-vector k) space for k things, indices 0 to k-1 (vector x1 ...) evaluates x1 ... and puts them into a vector (index vec i) get i'th element in O(1) time (index-setter! i val vec) put val at element i of vector vec

  7. ? (define v (n-vector 13)) ==> v ? v ==> [#f #f #f #f #f #f #f #f #f #f #f #f #f] ? (define u (vector 1 2 3 4 5)) ==> u ? u ==> [1 2 3 4 5] ? (index-setter! 3 u 100) ==> 100 ? u ==> [1 2 3 100 5] ? (index u 0) ==> 1 ? (index u 5) ==> {Error : "Index : Index 5 out of bounds for vector."}

More Related