Homework 1: Basic Macros

How to Design Programs: An Introduction to Programming and Computing

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
WPI CS536 (F07) Homework 1 (Basic Macros) CS 536 Homework 1: Basic Macros Due: September 13, hardcopy in class and electronic via turnin (asgmt name hwk1 ) If you already know basic Scheme programming, expand your skills by learning how to use macros. I provide a crash course below. In addition, Paul Graham's On Lisp provides an excellent and readable introduction to macros, what they can and should do, and how to think about them. His book uses different macro syntax than what I explain below, but gives a wonderful intro nonetheless. The macro material starts in chapter 7. The course text also has two chapters on macros, but work this assignment without working through the text, as the text shows solutions to many of the problems in this assignment. Basics of Scheme Macros Macros differ from functions in that evaluating a function yields a value, wihle evaluating a macro yields an expression. Macros are useful for many reasons. Among them, they let you introduce new syntax into your programs, and they you alter how Scheme would otherwise evaluate an expression if it were a function call instead of a macro. The or operator provides a classic example of the latter. Consider the expression (or true (+ 4 'a)) . As or short-circuits in Scheme, this program should return true. Assume we wrote or as a function: (define (or e1 e2) (if e1 true e2)) Then evaluating (or true (+ 4 'a)) would yield an error because Scheme evaluates its arguments before evaluating the body of the function. Thus, we cannot implement or with a function. We could, however, implement it as a macro using the following code: (define-syntax my-or (syntax-rules () [(my-or e1 e2) (if e1 true e2)])) file:///C|/Documents%20and%20Settings/Linda%20Grauer. ..Institute%20(WPI)-H3/CS536/Homework/hwk1-macros.html (1 of 5) [2/6/2008 12:10:46 PM]
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
WPI CS536 (F07) Homework 1 (Basic Macros) Now, evaluating (my-or true (+ 4 'a)) returns true, rather than an error, as it should. The general form of a Scheme macro is:
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 5

Homework 1: Basic Macros - WPI CS536 (F07) Homework 1...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online