This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: CS1102: Introduction to Macros, part 2 Kathi Fisler, WPI September 25, 2007 1 Using Macros to Fix More of the Slideshow Language Now that we have a better idea of how macros work, lets return to cleaning up the slideshow language. We wanted to clean up several parts of the code. Weve already gotten rid of the lambda s around slides with the myslide macro we wrote earlier. We fixed the problem with specifying the booleans in the pointlists by adding some helper functions. That leaves us with two main issues to address: the lists, and the let statement for defining the slides. 1.1 Cleaning Up the Talk Specification Heres the current talk program, using the format of the posted code: ( define talk2 ( let ([ intro-slide ( myslide ...)] [ arith-eg-slide ( myslide ( format Example a example-index ) ( pointlist-bulleted ( list ( + ( * 2 3) 6) ( + 6 6) 12 )))] [ func-eg-slide ( myslide ...)] [ summary-slide ( myslide ...)]) ( make-talk ( list ( make-display make-intro-slide ) ( make-timecond ( lambda ( time-in-talk ) ( > 5 time-in-talk )) ( list ( make-display make-arith-eg-slide )) empty ) ( make-display make-func-eg-slide ) ( make-display make-summary-slide ))))) First, lets clean up the talk specification to hide the details of the lists. We want to write a macro mytalk that takes a sequence of commands as arguments and adds the outer list structure. The following macro achieves this: ( define-syntax mytalk ( syntax-rules () [( mytalk cmd1 ...) ( make-talk ( list cmd1 ...))])) This introduces a new feature of macros: the ellipses. What do they mean and let us do? The ellipses say there can be any number of the pattern immediately preceding me. In this case, they say there can be any number of commands following the mytalk . Down in the macro body, we can use the ellipses again to say take all the remaining commands and drop them into the list. We can now rewrite the talk body using mytalk , and Scheme will convert it into the same talk ( talk2 ) that we had previously. ( define talk3 1 ( let ([ intro-slide ( myslide--- )] [ arith-eg-slide ( myslide ( format Example a example-index ) ( pointlist-bulleted ( list ( + ( * 2 3) 6) ( + 6 6) 12 )))] [ func-eg-slide ( myslide--- )] [ summary-slide ( myslide--- )]) ( mytalk ( make-display make-intro-slide ) ( make-timecond ( lambda ( time-in-talk ) ( > 5 time-in-talk )) ( make-section ( list ( make-display make-arith-eg-slide ))) ( make-section empty )) ( make-display make-func-eg-slide ) ( make-display make-summary-slide )))) [ Note the in talk3 are shorthand for the rest of the slide specifications that Ive left out of the notes to reduce clutter....
View Full Document
This note was uploaded on 02/06/2008 for the course CS 1102 taught by Professor Fisler during the Fall '07 term at WPI.
- Fall '07