The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Lisp Macros Are Very Cool

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Joey Gibson

Posts: 71
Nickname: joeygibson
Registered: Jun, 2003

Joey Gibson is an opinionated software architect who blogs about technology, politics, music, etc.
Lisp Macros Are Very Cool Posted: Aug 18, 2003 8:11 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Joey Gibson.
Original Post: Lisp Macros Are Very Cool
Feed Title: Joey Gibson's Blog
Feed URL: http://www.jobkabob.com/index.html
Feed Description: Thoughts, musings, ramblings and rants on Java, Ruby, Python, obscure languages, politics and other exciting topics.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Joey Gibson
Latest Posts From Joey Gibson's Blog

Advertisement
So I'm playing around with Lisp, reading Successful Lisp and thoroughly enjoying myself. I really like Lisp, I just haven't gotten to use it on anything other than test stuff yet. One of the things that I find the most interesting, and powerful, is the macro facility. Sure, some languages like C have macros that are processed by a preprocessor, but Lisp's macros are in a league of their own. Consider this code (lifted wholesale from Successful Lisp)

 1  (defmacro def-i/o (writer-name reader-name (&rest vars))
 2    (let ((file-name (gensym))
 3          (var (gensym))
 4          (stream (gensym)))
 5      `(progn
 6         (defun ,writer-name (,file-name)
 7           (with-open-file (,stream ,file-name
 8                                    :direction :output
 9                                    :if-exists :supersede)
10                           (dolist (,var (list ,@vars))
11                             (declare (special ,@vars))
12                             (print ,var ,stream))))
13
14         (defun ,reader-name (,file-name)
15           (with-open-file (,stream ,file-name
16                                    :direction :input
17                                    :if-does-not-exist :error)
18                           (dolist (,var ',vars)
19                             (set ,var (read ,stream)))))
20         t)))

What does this mass of parentheses, backquotes, commas and colons do? Lots. Executing the macro thusly
(def-i/o save-checks load-checks (*checks* *next-check-number* *payees*))
will define two functions, one called save-checks and the other called load-checks, that will store and retrieve the global variables *checks*, *next-check-number* and *payees* to and from a given file name. These methods could be called thusly
(save-checks "checks.dat")
(load-checks "checks.dat")
This macro could be included in any program for which we needed to have reader and writer functions for marshaling data to and from disk files. This example was for a fictional bank, but let's say I had a program to process data about the Tour de France and I had buckets for teams, riders, jerseys and sponsors. I could do this
(def-i/o save-tdf-info restore-tdf-info (*riders* *teams* *jersyes* *sponsors*)
and would get save-tdf-info and restore-tdf-info functions that could be called thusly
(save-tdf-info "tdf.dat")
(restore-tdf-info "tdf.dat")
Maybe I'm just easily impressed, but I think that's pretty cool.

Read: Lisp Macros Are Very Cool

Topic: Embedding The Ruby Interpreter in C++ Previous Topic   Next Topic Topic: Article on Ruby at NewsFactor Network

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use