zbigniewsz@gmail.com | My Groups | Help | Sign out
Go to Google Groups Home   
Web    Images    Groups    News    Froogle    Local    more »
  Advanced Groups Search
  Preferences
comp.lang.scheme > Newbie Macro Question
Fixed font - Proportional font
o...@pobox.com   Apr 29, 4:10 pm     show options
Newsgroups: comp.lang.scheme
From: o...@pobox.com - Find messages by this author
Date: 29 Apr 2005 13:10:44 -0700
Local: Fri,Apr 29 2005 4:10 pm
Subject: Re: Newbie Macro Question
Reply | Reply to Author | Forward | Print | View Thread | Show original | Report Abuse

Tom P wrote:
> (move nearer randomly at angle 45 using the 5 th closest particle)
> (move farther 7 at angle 0 using the 2 nd  farthest particle)

Perhaps you might be willing to adjust your syntax slightly so it
would look like

        (move :nearer 'randomly :angle 0 :with-particle 5)

That is, using the keyword arguments. The order of keyword-value pairs
is arbitrary. If your Scheme supports DSSSL extensions (as I think
Chicken does), the keyword arguments are available and so you're done.
If DSSSL extensions are not available, or if the change of the syntax
is way too much (for example, you would like to use 'randomly without
the quote [*]), you may want to look at

        http://pobox.com/~oleg/ftp/Sch eme/macros.html#keyword-args

which shows how to add keyword arguments to any function or a
*macro*. DSSSL extensions cannot be used for hygienic macros. Also,
your languages doesn't have to have keywords: symbols or any type of
data whatsoever will suffice as a label. For example, numbers can be
labels too. The technique of course permits assigning defaults to some
arguments. So, you can indeed write something like

  (move farther 7 angle 0 using the 2 nd  farthest particle)

Here `farther', `angle', `using', `2', `farthest' are labels, and `7',
`0', `the', `nd' and `particle' are the corresponding values.

[*] If one wishes to write randomly without a quotation, one merely
needs to add (define randomly 'randomly) and so can use that `name'
with or without the quotation.

Actually, I have tried this out, using the code in Appendix B of the
keyword-arg-macro.txt article, to which I added

(gen:define-labeled-arg-macro move
  (move-positional  ; positional procedure
            ; the following are the descriptors for lookup's three
arguments
            ; the order corresponds to the positions of move-positional
  (farther #f)  ; optional, #f is default value
  (nearer  #f)
  (angle)    ; required, no default
  (using)    ; required
  (1 #f)     ; some numerical labels
  (2 #f)
  (3 #f)
  (farthest #f)
  (nearest #f)
  ))

(define the 'the)
(define randomly 'randomy)
(define nd 'nd)
(define particle 'particle)

; note that the order corresponds to the order of the descriptors above
(define (move-positional farther nearer angle using arg1 arg2 arg3
          farthest nearest)
  (for-each display
    (list
      (if farther (list 'farther farther)
        (if nearer (list 'nearer nearer)
          (error "no direction")))
      " at angle " angle
      " using the particle number: "
      (cond (arg1 1) (arg2 2) (arg3 3))
      " which is " (cond (farthest "farthest") (nearest "nearest"))
      #\newline)))

(move farther 7 angle 0 using the 2 nd  farthest particle)

; The output:
; (farther 7) at angle 0 using the particle number: 2 which is farthest

tried using Scheme48, SCM and Petite Chez Scheme.

Reply

Google Home - Terms of Use - Privacy Policy - Jobs, Press, & Help

©2005 Google