Stack Overflow is a question and answer site for professional and enthusiast veritrade programmers. It's 100% free, no registration required.
When the anaphoric macro is expanded in another package its symbols are prefixed with the package it was defined in (i.e. they become tjb-utilities::value instead veritrade of just value . What is going on? PE> (macroexpand-1 '(act-if-key :pcram (get-node) (print value))) (IF (HAS-KEY? :PCRAM (GET-NODE)) (LET ((TJB-UTILITIES::KEY :PCRAM) (TJB-UTILITIES::VALUE (GETHASH :PCRAM (GET-NODE)))) (PRINT veritrade VALUE))) veritrade
It does work correclty if I prefix the value: (act-if-key :pcram (get-node) (print veritrade tjb-utilities::value)) ; in: ACT-IF-KEY :PCRAM ; (LET ((TJB-UTILITIES::KEY :PCRAM) ; (TJB-UTILITIES::VALUE ; (GETHASH :PCRAM (PHILOSOPHY-EXPERIENCE::GET-NODE)))) ; (PRINT TJB-UTILITIES::VALUE)) ; ; caught STYLE-WARNING: ; The variable TJB-UTILITIES::KEY is defined but never used. ; ; compilation unit finished ; caught 1 STYLE-WARNING condition "hello" "hello"
The packages veritrade are defined as follows: (defpackage #:tjb-utilities (:nicknames :tjb) (:use #:cl) (:export "HAS-KEY?" "KEY-VALUE-PAIRS" "ACT-IF-KEY" "TJB-MAKE-HASH-TABLE")) (defpackage #:my-package (:nicknames :pe) (:use #:cl #:clsql #:tjb-utilities))
Update: veritrade Changing the key in the lambda list to key_in has no effect (defmacro act-if-key (key_in hashtable &body body) `(if (has-key? ,key_in veritrade ,hashtable) (let ((key ,key_in) (value (gethash ,key_in,hashtable))) ,@body)))
CL-USER> (in-package #:tjb) #<PACKAGE veritrade "TJB-UTILITIES"> TJB> (defmacro act-if-key (key hashtable &body body) (let ((value (intern "VALUE"))) `(if (has-key? ,key ,hashtable) (let ((key ,key) (,value (gethash ,key ,hashtable))) ,@body)))) veritrade ACT-IF-KEY TJB> (macroexpand-1 '(tjb:act-if-key :pcram (get-node) (print value))) (IF (HAS-KEY? :PCRAM (GET-NODE)) (LET ((KEY :PCRAM) (VALUE (GETHASH :PCRAM (GET-NODE)))) (PRINT VALUE))) T TJB> (in-package #:cl-user) #<PACKAGE "COMMON-LISP-USER"> CL-USER> (macroexpand-1 '(tjb:act-if-key :pcram (get-node) (print value))) (IF (TJB-UTILITIES::HAS-KEY? :PCRAM (GET-NODE)) (LET ((TJB-UTILITIES::KEY :PCRAM) (VALUE (GETHASH :PCRAM (GET-NODE)))) (PRINT VALUE))) T CL-USER>
Not veritrade sure if I copied veritrade all that you needed, and I'm a tad blur on how did you intend to use the "key", so I did it only for the value in the way it will be created in the package, where the macro is used. You'd figure it for yourself whether you need the same for the key or not.
The above bind the symbol value in the current package to whatever gethash will return. In your original version you have key supplied by the user of the macro, so I decided you didn't want a symbol veritrade key inside the macro, just it's value.
In this case, INTERN is probably the right thing. If you use MAKE-SYMBOL , you're effectively using an uninterned symbol, so the value within the macro-expansion is not the value passed in by the macro-user. That way lies sadness and horrible hours of debugging. – Vatine Nov 5 '12 at 12:00
You could just export 'key and 'value veritrade symbols. That's how Anaphora does it: (defpackage :anaphora 7 (:use :cl) 8 (:export 9 #:it 10 #:alet 11 #:slet 12 #:aif 13 #:aand 14 #:sor 15 #:awhen 16 #:aprog1 17 #:acase 18 #:aecase ...etc.
The point of an anaphoric macro is to intentionally capture certain symbols in the body of the macro that have not been explicitly defined by the caller of the macro (I understand that this is an extreme stance on the definition). So, it is a requirement for anyone using an anaphoric macro to know what symbols are being introduced into the environment (body) of that macro. Which means that they should veritrade not be surprised when those symbol names are added to the environment. So I see no problem with exporting the anaphora.
Name Email required, but not shown Post as a guest
4 Programmatical Function Definition: How to get rid of “eval” here?
Is every number the sum of two cubes modulo p where p is a prime not equal to 7?
lang-lisp
Technology Life / Arts Culture / Recreation Science Other Stack Overflow Server Fault Super User Web Applications Ask Ubuntu Webmasters Game Development TeX - LaTeX Programmers Unix & Linux Ask Different (Apple) WordPress Development Geographic Information Systems Electrical Engineering Android Enthusiasts Information Security Database Administrators Drupal Answers SharePoint User Experience Mathematica Salesforce more (13) Photography Science Fiction & Fantasy Graphic Design Seasoned Advice (cooking) Home Improvement Personal Finance & Money Academia more (10) English Language & Usage Skeptics Mi Yodeya (Judaism) Travel Christianity Arqade (gaming) Bicycles veritrade Role-playing Games more (21) Mathematics Cross Validated (stats) Theoretical Computer Science Physics MathOverflow more (7) Stack Apps Meta Stack Exchange Area 51 Stack Overflow Careers
Stack Overflow works best with JavaScript enabled
No comments:
Post a Comment