Changeset 8729 in project
- Timestamp:
- 02/23/08 23:24:12 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
wiki/chicken-for-ruby-programmers
r8728 r8729 745 745 your program, which is usually the best time to do it. 746 746 747 TODO 748 747 Scheme macros ''rewrite'' your code during compile time. They can 748 range from simple to complex, with some macros defining entire 749 "sublanguages" embedded in Scheme. 750 751 Some people call Rails' {{acts_as_foo}} functions macros. This 752 description is not wrong, as these functions do ''rewrite'' your 753 classes in a similar way to Scheme macros, but they are not quite 754 as powerful. 755 756 Here is a simple example of a task that is easy in Scheme, but 757 much, much harder using Ruby's eval. Say you were debugging a 758 program and found yourself printing out variables at certain 759 points in the execution, along with the name of the variable 760 so you could tell what you were looking at. 761 762 <enscript highlight=scheme> 763 (print "myvar: " myvar) 764 </enscript> 765 766 You decide that repeatedly typing the variable name twice (once to 767 indicate which variable, once to get the value) is a waste of time. 768 Using a macro, you can quickly and easily abstract away the common 769 syntax into one place. 770 771 <enscript highlight=scheme> 772 (define-macro (ez-debug var) 773 (let ((name (symbol->string var))) 774 `(print ,name ": " ,var))) 775 776 (define myvar '(this is a list)) 777 778 (ez-debug myvar) 779 </enscript> 780 781 This simply wouldn't be possible with a function. By the time a 782 procedure is called, syntactic information like variables names 783 has been optimized away. 784 785
Note: See TracChangeset
for help on using the changeset viewer.