| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| I am sure this was discussed before, so a pointer to a relevant hyperspec topic should suffice. Why the difference between these two: (define-symbol-macro m12 (/ (* m1 m2) (+ m1 m2))) which works just fine: CL-USER> (let ((m1 1.0) (m2 2.0)) m12) 0.6666667 CL-USER> And the following. I first do the following: (defmacro def-mij (i j) (let* ((si (format nil "~D" i)) (sj (format nil "~D" j)) (mi (intern (concatenate 'string "m" si))) (mj (intern (concatenate 'string "m" sj))) (mij (intern (concatenate 'string "m" si sj)))) `(define-symbol-macro ,mij (/ (* ,mi ,mj) (+ ,mi ,mj))))) This then happens CL-USER> (def-mij 1 2) |m12| ;;; why the vertical bars CL-USER> (let ((|m1| pi) ;; I have to use |m1| and |m2| instead of just m1, m2 (|m2| pi)) |m12|) 1.5707963267948966193L0 CL-USER> Where do the vertical bars come from? And how do I get rid of them? I am trying to do numerical calculations by using symbol expansions instead of defuns. What I am striving for is to do (def-mij 1 1) (def-mij 1 2) (def-mij 2 2) (and many other symbols) and then use m11, m12, m22 in my computations instead of (m 1 1) (m 1 2) or (m 2 2). I hope that by using that, I make my code easier to read. Thanks, Mirko Mirko |
|
#2
| |||
| |||
| In article <0181ff5a-14ee-4fcf-8226-5f24d4c79655@x41g2000hsb.googlegroups.com>, Mirko.Vukovic@gmail.com wrote: > I am sure this was discussed before, so a pointer to a relevant > hyperspec topic should suffice. Why the difference between these two: > > (define-symbol-macro m12 (/ (* m1 m2) (+ m1 m2))) > > which works just fine: > > CL-USER> (let ((m1 1.0) > (m2 2.0)) > m12) > > 0.6666667 > CL-USER> > > And the following. I first do the following: > > (defmacro def-mij (i j) > (let* ((si (format nil "~D" i)) > (sj (format nil "~D" j)) > (mi (intern (concatenate 'string "m" si))) > (mj (intern (concatenate 'string "m" sj))) > (mij (intern (concatenate 'string "m" si sj)))) > `(define-symbol-macro ,mij (/ (* ,mi ,mj) (+ ,mi ,mj))))) I think you need some backquoting in the DEFINE-SYMBOL-MACRO body. Otherwise, it's going to perform the division when you define use DEF-MIJ, rather than defining M12 as a macro that expands into the division. > > This then happens > > CL-USER> (def-mij 1 2) > |m12| ;;; why the vertical bars > CL-USER> (let ((|m1| pi) ;; I have to use |m1| and |m2| instead of > just m1, m2 > (|m2| pi)) > |m12|) > > 1.5707963267948966193L0 > CL-USER> > > Where do the vertical bars come from? And how do I get rid of them? By default, the CL reader translates unescaped letters in symbols to uppercase. Lowercase letters in symbols have to be escaped to preserve them. Since you're creating symbols with lowercase letters in them, they have to be escaped, either by preceding each of them with backslash, or surrounding the whole thing with vertical bars. Use uppercase letters and you won't have this problem. > I am trying to do numerical calculations by using symbol expansions > instead of defuns. What I am striving for is to do > > (def-mij 1 1) > (def-mij 1 2) > (def-mij 2 2) > (and many other symbols) > > and then use m11, m12, m22 in my computations instead of (m 1 1) (m 1 > 2) or (m 2 2). > I hope that by using that, I make my code easier to read. > > Thanks, > > Mirko > > Mirko -- Barry Margolin, barmar@alum.mit.edu Arlington, MA *** PLEASE post questions in newsgroups, not directly to me *** *** PLEASE don't copy me on replies, I'll read them in the group *** |
|
#3
| |||
| |||
| On Aug 25, 9:47 pm, Barry Margolin <bar...@alum.mit.edu> wrote: > In article > <0181ff5a-14ee-4fcf-8226-5f24d4c79...@x41g2000hsb.googlegroups.com>, > > > > Mirko.Vuko...@gmail.com wrote: > > I am sure this was discussed before, so a pointer to a relevant > > hyperspec topic should suffice. Why the difference between these two: > > > (define-symbol-macro m12 (/ (* m1 m2) (+ m1 m2))) > > > which works just fine: > > > CL-USER> (let ((m1 1.0) > > (m2 2.0)) > > m12) > > > 0.6666667 > > CL-USER> > > > And the following. I first do the following: > > > (defmacro def-mij (i j) > > (let* ((si (format nil "~D" i)) > > (sj (format nil "~D" j)) > > (mi (intern (concatenate 'string "m" si))) > > (mj (intern (concatenate 'string "m" sj))) > > (mij (intern (concatenate 'string "m" si sj)))) > > `(define-symbol-macro ,mij (/ (* ,mi ,mj) (+ ,mi ,mj))))) > > I think you need some backquoting in the DEFINE-SYMBOL-MACRO body. > Otherwise, it's going to perform the division when you define use > DEF-MIJ, rather than defining M12 as a macro that expands into the > division. > > > > > This then happens > > > CL-USER> (def-mij 1 2) > > |m12| ;;; why the vertical bars > > CL-USER> (let ((|m1| pi) ;; I have to use |m1| and |m2| instead of > > just m1, m2 > > (|m2| pi)) > > |m12|) > > > 1.5707963267948966193L0 > > CL-USER> > > > Where do the vertical bars come from? And how do I get rid of them? > > By default, the CL reader translates unescaped letters in symbols to > uppercase. Lowercase letters in symbols have to be escaped to preserve > them. Since you're creating symbols with lowercase letters in them, > they have to be escaped, either by preceding each of them with > backslash, or surrounding the whole thing with vertical bars. > > Use uppercase letters and you won't have this problem. > Thanks! It works. The macro expansion works as inended (there was a backquote in the definition) (DEFINE-SYMBOL-MACRO M12 (/ (* M1 M2) (+ M1 M2))) So, thanks! Mirko |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.