Macros need explanation

Started by shercipher, May 10, 2009, 10:27:57 AM

Previous topic - Next topic

shercipher

I hail from Common Lisp, where the entire idea of macros is completely different, it seems, from that of newLisp.



Could someone please explain to me exactly what a newLisp macro does? Does it evaluate its body and not evaluate its inputs? Or does it evaluate its inputs but not its body? How can I use these macros to turn newLisp into a programmable programming language - the Lisp I know and love?



And slightly off topic, but the (clean ...) function seems to have a bug that produces a segmentation fault.

Kazimir Majorinc

#1
Welcome!



Newlisp macro is like function that gets unevaluated arguments. Newlisp macros existed in pre CL Lisps under the name of fexprs, and it is usually considered fexprs are more expressive, but harder to compile efficiently than CL macros, and it was the main reason fexprs are dropped from CL. Because Newlisp is interpreter it doesn't matter much.



Obviously, they are very similar to functions:



(set 'f (lambda(x y) some-code ))

(set 'm (lambda-macro(x y) some-code ))



(m a b)
has exactly the same result as (f 'a 'b).



One difference between Newlisp and CL macro is that result of Newlisp macro is not evaluated once again in the body of the caller, like it is the case in Common Lisp macros. If you want that Newlisp macros work on similar way as CL macros, you have to add one eval.



(defmacro m(arguments) some-code)



is roughly equivalent to Newlisp



(define-macro (m arguments)(eval some-code))



Usually Newlisp macros have that extra eval, but it might be pushed deeper into macro body.



I have one blog post about that here:



http://kazimirmajorinc.blogspot.com/2009/01/challenged-by-common-lispers.html">http://kazimirmajorinc.blogspot.com/200 ... spers.html">http://kazimirmajorinc.blogspot.com/2009/01/challenged-by-common-lispers.html



Many other posts in my blog demonstrate some uses of macros.
http://kazimirmajorinc.com/\">WWW site; http://kazimirmajorinc.blogspot.com\">blog.

xytroxon

#2
Some things to consider while kicking the newLISP "tires" during a "test drive"...



LISP is a gear shift high performance Formula One race car*...



Scheme is a gear shift midget race car on a cross-country Formula One course in Europe...



newLISP is an automatic shift hybrid automobile on a modern American super highway...



While LISP and Scheme "cars" are certainly high performance modes of transportation, and fun to drive once you are trained and race qualified... newLISP aims to be a more practical and economical mode of programming "transportation"... All three share the same kind of technology "under the hood", but there are tradeoffs made for total cost of ownership versus performance... That is, I can compile my custom designed code in LISP and Scheme for speed, but newLISP can still get the job done even if it is a bit slower... And by using newLISP's built in convenience functions, I save huge time costs in developement...



While most people may prefer driving clunky Java/C++/PHP SUV's, a green newLISP hybrid is the future... (And with newLISP, I don't wrap it around a light pole every time I go to the supermarket for milk ;o)



-- xytroxon



*on a snowy and muddy road in Siberia ;o)
\"Many computers can print only capital letters, so we shall not use lowercase letters.\"

-- Let\'s Talk Lisp (c) 1976

shercipher

#3
I don't understand this misconception that Common Lisp/Scheme are faster just because they are compiled. In most benchmarks they perform worse than Perl (which we'll call a "fast" interpreted language) even when speed 3 safety 0 is on.



When I saw the mergesort benchmark, I was pretty impressed; newLisp seems to perform on par with Perl, so I have no trouble with speed in NL at all (if I did, I have old faithful C, and the fact that newLisp has a FFI that makes sense to turn to).



That aside, these concepts of macros seem to be a bit troublesome. In CL, you used macros to redefine the language and selectively expand/replace expressions, choosing which parts of the expression you wanted to eval now and which parts you wanted to eval later.



I suppose I would be a little more comfortable if NL had support for that `( , ,) syntax that I'm used to.



EDIT.



Cool, it clicked. So a macro in newlisp is just a function that doesn't evaluate its arguments when passing them, unlike in CL where its a replacement sort of thing.

Kazimir Majorinc

#4
Newlisp doesn't have ` , - but it has letex and expand serving similar purpose.


(defmacro my-dotimes(head body)`(dotimes ,head (progn ,body (print "hi"))))

is directly translated to


(define-macro (my-dotimes head body)
   (eval (expand
         '(dotimes head (begin body (print "hi"))) 'head 'body)))


After some practice one can write his own unquote and quasiquote.




QuoteCool, it clicked. So a macro in newlisp is just a function that doesn't evaluate its arguments when passing them, unlike in CL where its a replacement sort of thing.


Yes. And vice versa - Newlisp functions can do everything Newlisp macros can - except for that apostrophes. So, unlike in other Lisp dialects, one can define IF as a function.


(set 'IF (lambda()
           (eval
             ((args)
               (find (true? (eval (first (args))))
                     '(* true nil))))))
http://kazimirmajorinc.com/\">WWW site; http://kazimirmajorinc.blogspot.com\">blog.

shercipher

#5
I think you can in Scheme define if as a function; however, in CL, because the rest of a list is evaluated before the head is, if must be a special form, and cannot be written as a function.

cormullion

#6
Quote from: "xytroxon"newLISP is an automatic shift hybrid automobile on a modern American super highway...


:) Ah, the old car analogy. It's good, though, for broadening people's range of criteria.



Me, I think of newLISP as a bicycle... :) Small, light, portable, and quick off the mark...

xytroxon

#7
Quote from: "cormullion"Me, I think of newLISP as a bicycle... :) Small, light, portable, and quick off the mark...


The more metaphors (mixed or otherwise), the merrier! ;p)



To expand and expound on this "ailing equine"... (i.e. this topic maybe becoming the proverbial "dead horse")



The superhighway is of course the internet, which newLISP excels at traveling... Without the internet, newLISP is not going anywhere very interesting... But, if the internet disappears tomorrow, LISP and Scheme will just as happily stay on their own closed racetracks...



And the different fuel sources, that power the "super" hybrid newLISP "car"?



Windows - gasoline / petrol (cheap at first, very costly later)

Mac - electric battery (continually reincarnating /recharging "dead" techology)

Linux - ethanol (everyone can have their own distillery/distro)

Unix - solar (high tech fabrication required)



-- xytroxon
\"Many computers can print only capital letters, so we shall not use lowercase letters.\"

-- Let\'s Talk Lisp (c) 1976

TedWalther

#8
Newlisp is the hang glider for the Internet :-)



Newlisp is the Cherry-on-Top of Computer Science.
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence.  Nine months later, they left with a baby named newLISP.  The women of the ivory towers wept and wailed.  \"Abomination!\" they cried.

Kazimir Majorinc

#9
If programming languages are like cars ...



Common Lisp:

http://lh5.ggpht.com/abramsv/SEY3RAjmYaI/AAAAAAAATcw/3ncmHwdtnI0/s640/640328163_349cd111aa_o.jpg">



Scheme:

http://www.coolest-gadgets.com/wp-content/uploads/steampunk_dalek11.jpg">



Newlisp:

http://www.walyou.com/blog/wp-content/uploads/2009/04/steampunk-subway.jpg">
http://kazimirmajorinc.com/\">WWW site; http://kazimirmajorinc.blogspot.com\">blog.

cormullion

#10
:)



To return to the original post: I'm interested in the question:


QuoteHow can I use these macros to turn newLisp into a programmable programming language - the Lisp I know and love?


I think there's plenty of scope for exploring code-data interaction in newLISP, but I'm also kind of intrigued as to why the OP is looking for a 'fling' with newLISP after professing love for CL. :) Perhaps after such affairs the return to the familiar is a welcome home-coming...?!  Or is there some magic missing from the relationship...?



I'm currently trying to learn Objective-C, while leaving newLISP on the back burner for a while (still transitioning to v10). For now I'm trying to avoid any comparisons or translations. I want to learn the "objective-c-ness" of Objective-C as it is, rather than trying to find or force any "newLISPness". There might be some natural overlap between the two, although at the moment my biggest problem is that I keep starting each line of code with a "("...

m i c h a e l

#11
Quote from: "John Foderaro""Lisp is a programmable programming language."

— CACM, September 1991


The quote continues:


Quote"Not only can you program in Lisp (that makes it a programming language) but you can program the language itself. This is possible, in part, because Lisp programs are represented as Lisp data objects, and partly because there are places during the scanning, compiling and execution of Lisp programs where user-written programs are given control."


This makes me think of one's ability to modify an already defined user-function in newLISP:


(s)hell> newlisp
newLISP v.10.0.4 on OSX IPv4 UTF-8, execute 'newlisp -h' for more info.

> (define (cubed n) (* n n n))
(lambda (n) (* n n n))
> (cubed 3)
27
> (cubed 3.5)
27
> (nth 1 cubed)
(* n n n)
> (setf (nth 1 cubed) '(mul n n n)) ; notice the quote
(mul n n n)
> (cubed 3.5)
42.875
> _


This has nothing to do with macros, of course, but it does show that newLISP functions are data objects that can be manipulated anytime after definition.


Quote from: "cormullion"Perhaps after such affairs the return to the familiar is a welcome home-coming...?!


This was my experience when one language or another proved too difficult or simply just felt wrong to me. I would go back to my previous language and immediately feel right at home. The downside is this can also make it difficult to readily see the true merits of a new language. And speaking of other languages . . .



I don't envy you your current affair, cormullion. Especially if you are simultaneously trying to get your mind around the whole of Cocoa's API. I've tried twice and failed both times. If you already know Smalltalk, you will be way ahead (Objective-C's message calling is similar to Smalltalk's). Just think of the square brackets as newLISP parens ;-)



m i c h a e l



P.S. Great visualizations of the three languages, Kazimir :-)