[plt-scheme] Image identity hackery

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Thu Dec 3 20:56:56 EST 2009

Just to clarify, you can also do this: 

  (require 2htdp/universe)

  (define id-image (color-list->image empty 0 0 0 0))
  (define id-image2 (circle 0 'solid 'white))
  (define id-image3 (circle 0 'solid 'red))
  (define id-image4 (rectangle 0 0 'solid 'red))

  (image=? id-image id-image2)
  (image=? id-image2 id-image3)
  (image=? id-image3 id-image4)

i.e., your image is a 0-sized image. Several of us 
begged Robby until he added this -- precisely because
it is the identity element of the image "algebra". 
(I don't know how to get inverses for all images, 
so images + overlay really just a monoid -- but 
I wouldn't mind being convinced otherwise.) 

And once you have that, it is trivial to define 
your function as 

  ;; make-swatches : loc number number -> image
  ;; creates a rectangle showing the colors in the list from left to
  ;; right, each as rectangle with width w and height h.
  (define (make-swatches l w h)
    (foldr (lambda (f r) (overlay/xy (nw:rectangle w h 'solid f) w 0 r)) ID l))

Keep in mind that fold{l|r} needs the identity element
to do this elegantly. 

-- Matthias





On Dec 3, 2009, at 8:21 PM, Jordan Johnson wrote:

> 
> Hi all,
> 
> Last year I had a student ask me:  Hey, if
> 
>    (sum empty)
> 
> is 0, and
> 
>    (product empty)
> 
> is 1, what can we use for some function that makes an image?
> 
> This led to a neat little discussion of inverses with respect to a given
> operation, and we didn't pursue the image question much beyond musing
> that there's no obvious image I such that
> 
>    (overlay X I) = X
> or
>    (overlay I X) = X
> 
> for all x (in the sense of image=?).
> 
> Today I gave the students an assignment involving color-lists, and one
> student observed that
> 
>    (image->color-list (color-list->image empty 0 0 0 0))
> 
> produces the empty list.  We experimented, and found that the image
> given by
> 
>    (color-list->image empty 0 0 0 0)
> 
> satisfies the overlay equations given above.  For that matter, it also
> satisfies
> 
>    (overlay/xy I 0 0 X) = X
> and (overlay/xy X m n I) = X
> 
> for all m in [0, (image-width X)] and n in [0, (image-height X)].
> 
> I get the impression the HtDP image code wasn't intentionally written to
> handle this as an "identity image" -- it breaks rather gracelessly when
> used as a background scene for place-image, for example -- but it still
> afforded me a neat conversation with the student and some interesting
> coding after class (see below).
> 
> Has anybody else played with this at all?
> 
> Best,
> jmj
> 
> ;; ----- sample code -----
> 
> ;; loc = listof[color]
> 
> (define id-image (color-list->image empty 0 0 0 0))
> 
> ;; make-swatches : loc number number -> image
> ;; creates a rectangle showing the colors in the list from left to
> ;; right, each as rectangle with width w and height h.
> (define (make-swatches aloc w h)
>  (cond
>    [(empty? aloc) id-image]
>    [(cons? aloc)
>     (overlay/xy (nw:rectangle w h 'solid (first aloc))
>                 w 0
>                 (make-swatches (rest aloc) w h))]))
> 
> (check-expect (make-swatches (list 'red 'blue 'green) 20 30)
>              (overlay/xy (nw:rectangle 20 30 'solid 'red)
>                          20 0
>                          (overlay/xy (nw:rectangle 20 30 'solid 'blue)
>                                      20 0
>                                      (nw:rectangle 20 30 'solid 'green))))
> _________________________________________________
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme



Posted on the users mailing list.