From: Pieter Breed
Subject: LSharp Macros
Date: 
Message-ID: <1155210134.452251.24300@i3g2000cwc.googlegroups.com>
Hi All,

Sorry, I realise this is not /really/ the forum to post this, but its
the closest I seem to have... I am hoping that someone who has
knowledge of LSharp will help, or that someone else present has enough
experience with CL's macros, and who is willing to give LSharp a try,
will help me.

I am trying to get a very simple macro working in LSharp
(http://www.lsharp.org/). This is merely to learn about macros in the
first place. I am trying to create a macro that will implement LSharp's
for loop, so it needs to have a body parameter.

This is what I came up with:
(= do-for
   (macro ((a) &body body)
	  `(for (= ,a 1)
		(< ,a 10)
		(= ,a (+ ,a 1))
		,@body)))

The idea is to call it like this:
(do-for (p) (lpr p))

and get:
1
2
...
8
9
return -> false

Its not working though. Any ideas why?

Regards,
Pieter Breed

From: Sidney Markowitz
Subject: Re: LSharp Macros
Date: 
Message-ID: <44db25cd$0$34576$742ec2ed@news.sonic.net>
Pieter Breed wrote, On 10/8/06 11:42 PM:
> (do-for (p) (lpr p))

Did you really try it with lpr instead of prl? The print line function is
named prl.

I don't have a .Net environment to try it with and that's the only thing wrong
I noticed from visual inspection just now.

-- 
    Sidney Markowitz
    http://www.sidney.com
From: Sidney Markowitz
Subject: Re: LSharp Macros
Date: 
Message-ID: <44db29e7$0$34525$742ec2ed@news.sonic.net>
Pieter Breed wrote, On 10/8/06 11:42 PM:
> (= do-for
>    (macro ((a) &body body)
> 	  `(for (= ,a 1)
> 		(< ,a 10)
> 		(= ,a (+ ,a 1))
> 		,@body)))
> 
> The idea is to call it like this:
> (do-for (p) (lpr p))

And of course I see more as soon as I click the send button...

I didn't notice anything in the reference documentation or the examples I
looked at that says that macros can destructure an argument in parentheses
like you have for (a). Perhaps you have to do something like

(macro (al &body body)
 (let a (car al)
   `(for (= ,a 1)     and so on

Also, you should be able to verify what the macro is doing by looking at

(macroexpand '(do-for (p) (lpr p))

-- 
    Sidney Markowitz
    http://www.sidney.com
From: Pieter Breed
Subject: Re: LSharp Macros
Date: 
Message-ID: <1155233161.048538.20670@i42g2000cwa.googlegroups.com>
Hi Sidney,

Thanks for the reply.

I tried to boil it down to the simplest form I could:

(= do-surround
   (macro (argument &rest body)
	  `(,@body)))

ie, this is supposed to do nothing.

If you call it like this:
(do-surround (4) (prl 5))

I would expect to see 5 on the screen. No such luck. I have included
the (unhelpfull) error message)

> (do-surround (3) (prl 5))
System.InvalidCastException: Unable to cast object of type
'LSharp.Cons' to type 'LSharp.Symbol'.
   at LSharp.Runtime.Eval(Object expression, Environment environment)
   at LSharp.Runtime.Eval(Object expression, Environment environment)
   at LSharp.Functions.Eval(Cons args, Environment environment)
   at LSharp.Runtime.Apply(Object function, Object arguments,
Environment environment)
   at LSharp.Runtime.Eval(Object expression, Environment environment)
   at LSharp.Runtime.EvalString(String expression, Environment
environment)
   at LSharp.TopLoop.Run(TextReader reader, TextWriter writer,
TextWriter error)

Regards,
Pieter



Sidney Markowitz wrote:
> Pieter Breed wrote, On 10/8/06 11:42 PM:
> > (= do-for
> >    (macro ((a) &body body)
> > 	  `(for (= ,a 1)
> > 		(< ,a 10)
> > 		(= ,a (+ ,a 1))
> > 		,@body)))
> >
> > The idea is to call it like this:
> > (do-for (p) (lpr p))
>
> And of course I see more as soon as I click the send button...
>
> I didn't notice anything in the reference documentation or the examples I
> looked at that says that macros can destructure an argument in parentheses
> like you have for (a). Perhaps you have to do something like
>
> (macro (al &body body)
>  (let a (car al)
>    `(for (= ,a 1)     and so on
>
> Also, you should be able to verify what the macro is doing by looking at
>
> (macroexpand '(do-for (p) (lpr p))
> 
> -- 
>     Sidney Markowitz
>     http://www.sidney.com
From: Ari Johnson
Subject: Re: LSharp Macros
Date: 
Message-ID: <871wro2y29.fsf@bender.theari.com>
"Pieter Breed" <············@gmail.com> writes:

> I tried to boil it down to the simplest form I could:
> 
> (= do-surround
>    (macro (argument &rest body)
> 	  `(,@body)))
> 
> ie, this is supposed to do nothing.
> 
> If you call it like this:
> (do-surround (4) (prl 5))
> 
> I would expect to see 5 on the screen. No such luck. I have included
> the (unhelpfull) error message)
> 
> > (do-surround (3) (prl 5))
> System.InvalidCastException: Unable to cast object of type
> 'LSharp.Cons' to type 'LSharp.Symbol'.
>    at LSharp.Runtime.Eval(Object expression, Environment environment)
>    at LSharp.Runtime.Eval(Object expression, Environment environment)
>    at LSharp.Functions.Eval(Cons args, Environment environment)
>    at LSharp.Runtime.Apply(Object function, Object arguments,
> Environment environment)
>    at LSharp.Runtime.Eval(Object expression, Environment environment)
>    at LSharp.Runtime.EvalString(String expression, Environment
> environment)
>    at LSharp.TopLoop.Run(TextReader reader, TextWriter writer,
> TextWriter error)

The error message actually was helpful to me.  The error is that
LSharp wants to do something that it can only do with a symbol, and
you gave it a cons.  Try macroexpanding your test case - I am doing it
in my head and I get the following result:

(do-surround (3) (prl 5)) => macro call with argument = (3)
  and body = ((prl 5)), so:
`(,@body) => ((prl 5))

The translation is therefore:
(do-surround (3) (prl 5)) => ((prl 5))

The Lisp evaluator evaluates a cons by checking whether the symbol
located in its car denotes a special operator, a macro, or neither.
The key word in that sentence was "symbol."

(car '((prl 5))) => (prl 5)

(prl 5) is a cons, not a symbol, and that is your problem.  What you
probably wanted was for the expansion to be (progn (prl 5)).
From: Pieter Breed
Subject: Re: LSharp Macros
Date: 
Message-ID: <1155301467.343846.82740@b28g2000cwb.googlegroups.com>
Hi Ari,

Your suggestion was exactly right, thank you very much.

Even though LSharp does not have a progn from, it does have something
similar, ie do. The version of do-nothing that works is below for
future refence:

(defmacro do-surround (argument &rest body)
  `(do ,@body))

Will try to access arguments next...

Regards,
Pieter Breed


Ari Johnson wrote:
> "Pieter Breed" <············@gmail.com> writes:
>
> > I tried to boil it down to the simplest form I could:
> >
> > (= do-surround
> >    (macro (argument &rest body)
> > 	  `(,@body)))
> >
> > ie, this is supposed to do nothing.
> >
> > If you call it like this:
> > (do-surround (4) (prl 5))
> >
> > I would expect to see 5 on the screen. No such luck. I have included
> > the (unhelpfull) error message)
> >
> > > (do-surround (3) (prl 5))
> > System.InvalidCastException: Unable to cast object of type
> > 'LSharp.Cons' to type 'LSharp.Symbol'.
> >    at LSharp.Runtime.Eval(Object expression, Environment environment)
> >    at LSharp.Runtime.Eval(Object expression, Environment environment)
> >    at LSharp.Functions.Eval(Cons args, Environment environment)
> >    at LSharp.Runtime.Apply(Object function, Object arguments,
> > Environment environment)
> >    at LSharp.Runtime.Eval(Object expression, Environment environment)
> >    at LSharp.Runtime.EvalString(String expression, Environment
> > environment)
> >    at LSharp.TopLoop.Run(TextReader reader, TextWriter writer,
> > TextWriter error)
>
> The error message actually was helpful to me.  The error is that
> LSharp wants to do something that it can only do with a symbol, and
> you gave it a cons.  Try macroexpanding your test case - I am doing it
> in my head and I get the following result:
>
> (do-surround (3) (prl 5)) => macro call with argument = (3)
>   and body = ((prl 5)), so:
> `(,@body) => ((prl 5))
>
> The translation is therefore:
> (do-surround (3) (prl 5)) => ((prl 5))
>
> The Lisp evaluator evaluates a cons by checking whether the symbol
> located in its car denotes a special operator, a macro, or neither.
> The key word in that sentence was "symbol."
>
> (car '((prl 5))) => (prl 5)
>
> (prl 5) is a cons, not a symbol, and that is your problem.  What you
> probably wanted was for the expansion to be (progn (prl 5)).