Alex Rivera | Logout

Can someone explain the concept of 'hygiene' to me (I'm a scheme programmer)?

Asked 2010-06-09T22:36:33.247
30

So... I'm new to scheme r6rs, and am learning macros. Can somebody explain to me what is meant by 'hygiene'?

Thanks in advance.

Edit
Report

2 Answers

2

Here's what I found. Explaining what it means is another matter altogether!

http://www.r6rs.org/final/html/r6rs-lib/r6rs-lib-Z-H-1.html#node_toc_node_sec_12.1

answered 2010-06-09T22:53:49.227
2

Macros transform code: they take one bit of code and transform it into something else. As part of that transformation, they may surround that code with more code. If the original code references a variable a, and the code that's added around it defines a new version of a, then the original code won't work as expected because it will be accessing the wrong a: if

(myfunc a)

is the original code, which expects a to be an integer, and the macro takes X and transforms it to

(let ((a nil)) X)

Then the macro will work fine for

(myfunc b)

but (myfunc a) will get transformed to

(let ((a nil)) (myfunc a))

which won't work because myfunc will be applied to nil rather than the integer it is expecting.

A hygienic macro avoids this problem of the wrong variable getting accessed (and a similar problem the other way round), by ensuring that the names used are unique.

Wikipedia has a good explanation of hygienic macros.

answered 2010-06-09T22:59:19.620

Your Answer