Re: Macro tutorials?

2011-10-10 Thread Larry Johnson
Hi, Nicolas, and thanks.

I'm new to clojure (I've been  working through Programming Clojure), and
most of my long work life has gravitated around c, shell scripts, and perl.
That being said, I've tinkered with Lisp dialects for the past twenty-five
years (mostly elisp, scheme, and common lisp), and have always struggled
with both the overall meaning of Lisp (when should I use it, when should I
not?), and with macros.

I downloaded and read the intro to On Lisp, and it looks like exactly what I
need.  I'm going to work through the book, and make comments here about how
it relates to my learning of clojure (assuming that it isn't deemed
off-topic noise).

Regards,
Larry



On Mon, Oct 10, 2011 at 7:33 AM, Nicolas  wrote:

> A good book to learn lisp macros, is On Lisp from Paul Graham. This
> book really cover advanced topics and concepts, and has many chapters
> related to macros.
>
> The book is freely available in online format from Paul Graham
> Website: http://www.paulgraham.com/onlisp.html
>
> On Oct 6, 1:02 pm, Michael Jaaka  wrote:
> > Thanks to all! You have helped a lot!
> > Also I will consider reading "Practical Common Lisp".
> >
> > On Oct 6, 9:42 am, Stefan Kamphausen  wrote:
> >
> >
> >
> >
> >
> >
> >
> > > Hi.
> >
> > > You might consider reading Peter Seibel's excellent Practical Common
> Lisp
> > > which has some nice macro-work in it. If after that you're still hungry
> for
> > > more, consider Let Over Lambda by Doug Hoyte.
> >
> > > Admitted, both cover Common Lisp, but the differences will not keep you
> from
> > > getting a deeper understanding of how macros work and where and how
> they can
> > > be used.
> >
> > > (This is more an answer to the subject of this thread, less to the
> question
> > > in your body :)
> >
> > > Regards,
> > > Stefan
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
>



-- 

*Off the Beaten Path in Technology
http://otbeatenpath.wordpress.com
*

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: Macro tutorials?

2011-10-10 Thread Nicolas
A good book to learn lisp macros, is On Lisp from Paul Graham. This
book really cover advanced topics and concepts, and has many chapters
related to macros.

The book is freely available in online format from Paul Graham
Website: http://www.paulgraham.com/onlisp.html

On Oct 6, 1:02 pm, Michael Jaaka  wrote:
> Thanks to all! You have helped a lot!
> Also I will consider reading "Practical Common Lisp".
>
> On Oct 6, 9:42 am, Stefan Kamphausen  wrote:
>
>
>
>
>
>
>
> > Hi.
>
> > You might consider reading Peter Seibel's excellent Practical Common Lisp
> > which has some nice macro-work in it. If after that you're still hungry for
> > more, consider Let Over Lambda by Doug Hoyte.  
>
> > Admitted, both cover Common Lisp, but the differences will not keep you from
> > getting a deeper understanding of how macros work and where and how they can
> > be used.
>
> > (This is more an answer to the subject of this thread, less to the question
> > in your body :)
>
> > Regards,
> > Stefan

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Macro tutorials?

2011-10-06 Thread Michael Jaaka
Thanks to all! You have helped a lot!
Also I will consider reading "Practical Common Lisp".

On Oct 6, 9:42 am, Stefan Kamphausen  wrote:
> Hi.
>
> You might consider reading Peter Seibel's excellent Practical Common Lisp
> which has some nice macro-work in it. If after that you're still hungry for
> more, consider Let Over Lambda by Doug Hoyte.  
>
> Admitted, both cover Common Lisp, but the differences will not keep you from
> getting a deeper understanding of how macros work and where and how they can
> be used.
>
> (This is more an answer to the subject of this thread, less to the question
> in your body :)
>
> Regards,
> Stefan

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Macro tutorials?

2011-10-06 Thread Stefan Kamphausen
Hi.

You might consider reading Peter Seibel's excellent Practical Common Lisp 
which has some nice macro-work in it. If after that you're still hungry for 
more, consider Let Over Lambda by Doug Hoyte.  

Admitted, both cover Common Lisp, but the differences will not keep you from 
getting a deeper understanding of how macros work and where and how they can 
be used.

(This is more an answer to the subject of this thread, less to the question 
in your body :)

Regards,
Stefan

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: Macro tutorials?

2011-10-05 Thread Michał Marczyk
On 5 October 2011 11:38, Michael Jaaka  wrote:
> In fact I still don't know the macros works, I don't know when
> and how is evaluated and how symbols are evaluated.

Macros are just functions manipulating list structure (the data
structures produced by the reader when reading in Clojure code).

E.g. say we want to write our own 'and macro (which differs from
clojure.core/and in that it returns true if all xs evaluate to truthy
values at run time (rather than the last x)):

(defmacro my-and [& xs]
  (if (seq xs)
`(let [x# ~(first xs)]
   (if x# (my-and ~@(next xs)) x#))
true))

Actually, to make it clearer what's happening, we could also write it like so:

(defmacro my-and [& xs]
  (let [xsym (gensym "x")]
(if (seq xs)
  (list `let [xsym (first xs)]
(list `if xsym (list* `my-and (next xs)) xsym))
  true)))

Now if you write (my-and 1 2 false 3) in your code, the compiler will
notice that this is a list whose first symbol names a macro function
and call the macro function:

(apply #'my-and nil nil '(1 2 false 3))
; => (clojure.core/let [x12 1] (if x12 (user/my-and 2 false 3) x12))

(Don't worry about the nils -- they're needed because macro functions
in Clojure expect to receive extra magic arguments beyond those
explicitly named in their signatures. These are very useful
occasionally, but for the sake of simplicity, let's just forget about
them -- they're not used by my-and anyway.)

The return value is just another list; as it happens, clojure.core/let
is a macro too, so it too gets expanded. Only when the result of macro
expansion is no longer itself a macro call is JVM bytecode actually
emitted by the compiler.

Now for a more complex example:

(def x false)

(my-and 1 2 x 3)

We would expect this to return false at run time, and indeed it does,
but the macro function itself doesn't care about that -- it just
returns something like

(let [x1 1] (if x1 (my-and 2 x 3) x1))

Eventually the inner call gets expanded, so the code looks like

(let [x1 1] (if x1 (let [x2 2] (if x2 (my-and x 3) x2)) x1))

Ultimately we obtain

(let [x1 1]
  (if x1
(let [x2 2]
  (if x2
(let [x3 x] ; <- notice the x
  (if x3
(let [x4 3]
  (if x4
true
x4))
x3))
x2))
 x1))

The 'x here is still just a symbol; the value of the Var it names only
becomes relevant at run time.

In fact, evaluation of arguments passed to macros only ever happens at
run time (well, there's the possibility of calling eval from inside a
macro to force a "recursive compilation" followed by evaluation of a
form; this is likely to be useful only in fairly esoteric cases). If
you want to prevent if from happening even then, you can wrap the
arguments in a 'quote form in the expansion:

(defmacro list-of-unevaled [& xs]
  (list* `list (map #(list 'quote %) xs)))

(list-of-unevaled (+ 1 2) (/ 5 0))
; => ((+ 1 2) (/ 5 0))

Hope this helps,
Michał

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Macro tutorials?

2011-10-05 Thread Baishampayan Ghose
On Wed, Oct 5, 2011 at 4:16 PM, Meikel Brandmeyer (kotarak)  
wrote:
> And just for fun some more:
>
> (defmacro map-fn
>   [& fns]
>   (into {} (map (fn [[n & tail]] [(keyword n) `(fn ~@tail)]) fns)))

That one looks very cool, Meikel :-)

Regards,
BG

-- 
Baishampayan Ghose
b.ghose at gmail.com

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Macro tutorials?

2011-10-05 Thread Meikel Brandmeyer (kotarak)
And just for fun some more:

(defmacro map-fn
  [& fns]
  (into {} (map (fn [[n & tail]] [(keyword n) `(fn ~@tail)]) fns)))

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: Macro tutorials?

2011-10-05 Thread Baishampayan Ghose
Hi,

> I wrote few apps with clojure. I have used many times macro to expand
> expressions and change some control flows. I thought that I know
> macros, but now I know that doing some programming by analogy is not
> enough. In fact I still don't know the macros works, I don't know when
> and how is evaluated and how symbols are evaluated. Problem below is
> something which I'm not able to solve with my current "knowledge".
>
> Let say that I wrote macro map-fnc
>
> and I want to use it like
>
> (map-fnc (function1 [] "hello")
>        (function2 [a] (println a)) )
>
> The result is map in which keys are names of methods as strings and
> body is a function with proper arity.
> So I can evalutate this:
>
>
> ((get (map-fnc (function1 [] "hello")
>                (function2 [a] (println a)) ) "function1"))
>
> or this
>
>
> ((get (map-fnc (function1 [] "hello")
>                (function2 [a] (println a)) ) "function2") "hello world")
>
> Any help?! I belive that solving this problem might give me more
> insight on how macro works.

I might write it like this -

(defmacro map-fnc
  [& fn-specs]
  (let [fn-names (map (comp str first) fn-specs)
fn-decls (map #(cons 'fn (rest %)) fn-specs)
fn-map (zipmap fn-names fn-decls)]
`~fn-map))

See if that works for you.

Regards,
BG

-- 
Baishampayan Ghose
b.ghose at gmail.com

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Macro tutorials?

2011-10-05 Thread Michael Jaaka
I have manage to write something like this. But nothing more, all my
ideas are wrong, because bad knowledge about evaluation and namespace
expansion time.


(defmacro map-fnc [ & methods ]
`(reduce (fn[ acc# m# ] (assoc acc# (str (first m#)) (list 'fn (rest
m#))  )) {} '~methods ))

(map-fnc (function1 [] "hello")
(function2 [a] (println a)))

On Oct 5, 11:38 am, Michael Jaaka 
wrote:
> Hi!
>
> I wrote few apps with clojure. I have used many times macro to expand
> expressions and change some control flows. I thought that I know
> macros, but now I know that doing some programming by analogy is not
> enough. In fact I still don't know the macros works, I don't know when
> and how is evaluated and how symbols are evaluated. Problem below is
> something which I'm not able to solve with my current "knowledge".
>
> Let say that I wrote macro map-fnc
>
> and I want to use it like
>
> (map-fnc (function1 [] "hello")
>         (function2 [a] (println a)) )
>
> The result is map in which keys are names of methods as strings and
> body is a function with proper arity.
> So I can evalutate this:
>
> ((get (map-fnc (function1 [] "hello")
>                 (function2 [a] (println a)) ) "function1"))
>
> or this
>
> ((get (map-fnc (function1 [] "hello")
>                 (function2 [a] (println a)) ) "function2") "hello world")
>
> Any help?! I belive that solving this problem might give me more
> insight on how macro works.
>
> Thanks in advance!

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en