From: refun on 16 Feb 2010 17:53 In article <hlf6ts$ehl$1(a)gregory.bnet.hr>, fbogdanovic(a)xnet.hr says... > > Can I make my own code forms like, for example: > > (for i in list do |i| > body > ) > > or everything is in form of (macro-name arguments) ? > Or here would 'for' be a macro name and everything else an argument ? > That 'body' would also be just an argument ? I don't really know what you mean by |i| (in CL, |...| is notation for escaping symbol names). Control-flow macros like FOR are reasonably simple to do, but why not use what CL already provides, have you looked at DO, DOLIST, DOTIMES and especially LOOP (they're all macros part of CL)? LOOP's syntax is quite similar to yours. If you dislike LOOP, you might want to look at the ITERATE library. Here's an example usage of LOOP: (defun number-list (list) (loop for i from 1 for element in list collect (cons i element))) ;CL-USER> (number-list '(a b c)) ;((1 . A) (2 . B) (3 . C)) If you're curious how a macro works, macroexpand some example code. You might also like to use SLIME's macroexpand mode.
From: Haris Bogdanovic on 16 Feb 2010 18:16 I just used for loop as a simple example. I put |i| because I'm interested if I can put any symol, letter or number sequence for lisp to understand in a macro ? That |i| is from Ruby.
From: Paul Donnelly on 17 Feb 2010 01:32 "Haris Bogdanovic" <fbogdanovic(a)xnet.hr> writes: > I just used for loop as a simple example. > I put |i| because I'm interested if I can put any symol, letter or number > sequence for lisp to understand in a macro ? > That |i| is from Ruby. Macros get expanded after code is read, so your macros can take any data the reader can read. If you want to read an arbitrary sequence of characters, then you need a reader macro, which is something different.
From: Ron Garret on 17 Feb 2010 02:39 In article <hlf1mo$4ge$1(a)gregory.bnet.hr>, "Haris BogdanoviƦ" <fbogdanovic(a)xnet.hr> wrote: > I don't get what's about that 'get it' in lisp ? > That code and data can be and is represented as a tree ? > Not much of a 'get it'. The part that you don't get is the difference between "can be" and "is". Code in any language CAN BE represented as a tree. But in Lisp (and only in Lisp) code IS represented as a tree, and furthermore, that tree is available for manipulation by the programmer as a first-class data structure. That turns out to have deep and profound implications. Is. Or is not. Es gibt kein "can be." (With apologies to Yoda.) rg
From: Ron Garret on 17 Feb 2010 02:46 In article <ac0b5518-5de4-4894-bef6-4f36d706ea20(a)15g2000yqa.googlegroups.com>, Mark Tarver <dr.mtarver(a)ukonline.co.uk> wrote: > On 16 Feb, 21:11, "Haris BogdanoviƦ" <fbogdano...(a)xnet.hr> wrote: > > I don't get the difference between defun and defmacro ? > > > > If code is data and data is code then macros are functions and functions are > > macros > > and everything is everything and then comes Monty Python's foot on top of > > that. > > > > I don't get what's about that 'get it' in lisp ? > > That code and data can be and is represented as a tree ? > > Not much of a 'get it'. > > People might give various answers to your question. But here is one. > > The great usefulness of macros lies in their ability to change the > default flow on control, which like many functional languages, is > strict applicative order. But sometimes you don't want that, and > there are certain functions which absolutely require you to change > that flow. > > For instance, in my work I wanted to define my own 'if' that worked > with 'true' and 'false' and not T and NIL. A simple definition is > > (define if > true X Y -> X > false X Y -> Y) > > But that's no good because my 'if' evaluates all of its arguments. > Hence I need to change the flow of control. A macro does that. > > (DEFMACRO if (X Y Z) > `(LET ((*C* ,X)) > (COND ((EQ *C* 'true) ,Y) ((EQ *C* 'false) ,Z) > (T (error "~S is not a boolean~%" *C*))))) > > Mark It's also handy to be able to change the surface syntax. For example, I decided that I didn't like the way my code kept creeping off the right side of the screen. So I designed my own construct that I call a BINDING BLOCK (BB for short). So instead of, say: (let ((x (foo))) (frotz) (let ((y (snoz))) (bratz) (let ((z (florb))) ... I can instead write: (bb x (foo) (frotz) y (snoz) (bratz) z (florb) ... ) Writing BB as a Lisp macro is an elementary exercise, five minutes of work and half a dozen lines of code even for a beginner. In any other language it would be an advanced exercise that would require hacking the innerds of the compiler and that only a guru would even contemplate attempting. rg
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 5 Prev: CFP - SLE'2010 Next: %%% top 10 Lovely place for Lovers %%%% |