Discussion:
loop and with-html-output
Tyler Smith
2011-12-21 22:16:28 UTC
Permalink
Content preview: Hi, I'm new to lisp in general, trying to get cl-sql and cl-who
working together. I'm stumped in my efforts to generate a definition list
from an sql query. The following works as expected: (multiple-value-bind
(records fields) (select [genus] [species] :from "specimens" :where [= [collector]
"Test"]) (loop for rec in records do (loop for label in fields for val in
rec do (fresh-line) (print label) (print val)))) [...]

Content analysis details: (-0.7 points, 5.0 required)

pts rule name description
---- ---------------------- --------------------------------------------------
-0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low
trust
[209.85.215.51 listed in list.dnswl.org]
0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider
(sedgeboy[at]gmail.com)
-0.0 SPF_PASS SPF: sender matches SPF record
0.0 T_DKIM_INVALID DKIM-Signature header exists but is not valid
Archived-At: <http://permalink.gmane.org/gmane.lisp.who.general/282>

Hi,

I'm new to lisp in general, trying to get cl-sql and cl-who working
together. I'm stumped in my
efforts to generate a definition list from an sql query. The following
works as expected:

(multiple-value-bind (records fields)
(select [genus] [species] :from "specimens" :where [= [collector] "Test"])
(loop for rec in records do
(loop for label in fields
for val in rec do
(fresh-line)
(print label)
(print val))))

=>

"GENUS"
"Scleria"

"SPECIES"
"triglomerata"

However, swapping print for with-output-to-html fails:

* (multiple-value-bind (records fields)
(select [genus] [species] :from "specimens" :where [= [collector] "Test"])
(loop for rec in records do
(loop for label in fields
for val in rec do
(fresh-line)
(with-html-output (*standard-output* nil :indent nil)
(:dt label)
(:dd val)))))


<dt></dt><dd></dd>
<dt></dt><dd></dd>

What have I missed? Any other suggestions for doing this in a lispier
way would also be
welcome.

Thanks,

Tyler
Jens Teich
2011-12-22 06:56:40 UTC
Permalink
Content preview: > * (multiple-value-bind (records fields) > (select [genus]
[species] :from "specimens" :where [= [collector] "Test"]) > (loop for rec
in records do > (loop for label in fields > for val in rec do > (fresh-line)
(with-html-output (*standard-output* nil :indent nil) > (:dt label) > (:dd
val))))) > [...]

Content analysis details: (0.0 points, 5.0 required)

pts rule name description
---- ---------------------- --------------------------------------------------
Archived-At: <http://permalink.gmane.org/gmane.lisp.who.general/283>
* (multiple-value-bind (records fields)
(select [genus] [species] :from "specimens" :where [= [collector] "Test"])
(loop for rec in records do
(loop for label in fields
for val in rec do
(fresh-line)
(with-html-output (*standard-output* nil :indent nil)
(:dt label)
(:dd val)))))
(:dt (str label))
(:dd (str val))

-jens
Sebastian Tennant
2011-12-22 15:53:59 UTC
Permalink
Content preview: http://weitz.de/cl-who/#syntax Hope this helps. Sebastian
-- Emacs' AlsaPlayer - Music Without Jolts Lightweight, full-featured and
mindful of your idyllic happiness. http://home.gna.org/eap [...]

Content analysis details: (-4.9 points, 5.0 required)

pts rule name description
---- ---------------------- --------------------------------------------------
-2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at http://www.dnswl.org/, medium
trust
[80.91.229.12 listed in list.dnswl.org]
-0.0 SPF_HELO_PASS SPF: HELO matches SPF record
-0.0 SPF_PASS SPF: sender matches SPF record
-2.6 RP_MATCHES_RCVD Envelope sender domain matches handover relay domain
Archived-At: <http://permalink.gmane.org/gmane.lisp.who.general/284>

http://weitz.de/cl-who/#syntax

Hope this helps.

Sebastian
--
Emacs' AlsaPlayer - Music Without Jolts
Lightweight, full-featured and mindful of your idyllic happiness.
http://home.gna.org/eap
Tyler Smith
2011-12-22 17:03:43 UTC
Permalink
Content preview: On Thu, Dec 22, 2011 at 10:53 AM, Sebastian Tennant <***@smolny.plus.com>
wrote: > http://weitz.de/cl-who/#syntax > > Hope this helps. > Not really,
that's the documentation that I'm confused about. It starts out with the
statement that strings will be printed verbatim. My variables are strings,
but they aren't printed. I understand from Jens' response that I can get
the function to work by wrapping my variables in (str ...), but I don't understand
why that's necessary. The documentation of str suggests it's not necessary
for strings: [...]

Content analysis details: (-0.7 points, 5.0 required)

pts rule name description
---- ---------------------- --------------------------------------------------
-0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low
trust
[209.85.215.51 listed in list.dnswl.org]
0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider
(sedgeboy[at]gmail.com)
-0.0 SPF_PASS SPF: sender matches SPF record
0.0 T_DKIM_INVALID DKIM-Signature header exists but is not valid
Archived-At: <http://permalink.gmane.org/gmane.lisp.who.general/285>

On Thu, Dec 22, 2011 at 10:53 AM, Sebastian Tennant
Post by Sebastian Tennant
http://weitz.de/cl-who/#syntax
Hope this helps.
Not really, that's the documentation that I'm confused about. It
starts out with the statement that strings will be printed verbatim.
My variables are strings, but they aren't printed. I understand from
Jens' response that I can get the function to work by wrapping my
variables in (str ...), but I don't understand why that's necessary.
The documentation of str suggests it's not necessary for strings:

A form which is neither a string nor a keyword nor a list beginning
with a keyword will be left as is except for the following
substitutions:

My variables are already strings:

* (multiple-value-bind (records fields)
(select [genus] [species] :from "specimens" :where [< [specimen_id] 4])
(loop for rec in records do
(loop for label in fields
for val in rec do
(fresh-line)
(princ (stringp label))
(princ (stringp val)))))


TT
TT
TT
TT
TT
TT
NIL
*

Can someone explain why my variables aren't being treated as strings
within with-html-output?

The definition of (str ...) continues:

Forms that look like (str form1 form*) will be substituted with (let
((result form1)) (when result (princ result s))).

I'm new to Lisp (as must be clear by now!): what is the difference between

(princ form1)

and

(let ((result form1))
(princ result))

Thanks for your help,

Tyler
Jeffrey Cunningham
2011-12-22 17:10:07 UTC
Permalink
Content preview: On Thu, 22 Dec 2011 09:03:43 -0800, Tyler Smith <***@mail.mcgill.ca>
wrote: > On Thu, Dec 22, 2011 at 10:53 AM, Sebastian Tennant > <***@smolny.plus.com>
wrote: >> http://weitz.de/cl-who/#syntax >> >> Hope this helps. >> > > Not
really, that's the documentation that I'm confused about. It > starts out
with the statement that strings will be printed verbatim. > My variables
are strings, but they aren't printed. I understand from > Jens' response that
I can get the function to work by wrapping my > variables in (str ...), but
I don't understand why that's necessary. > The documentation of str suggests
it's not necessary for strings: > > A form which is neither a string nor
a keyword nor a list beginning > with a keyword will be left as is except
for the following > substitutions: > > My variables are already strings: >
[...]

Content analysis details: (0.0 points, 5.0 required)

pts rule name description
---- ---------------------- --------------------------------------------------
Archived-At: <http://permalink.gmane.org/gmane.lisp.who.general/286>

On Thu, 22 Dec 2011 09:03:43 -0800, Tyler Smith
Post by Tyler Smith
On Thu, Dec 22, 2011 at 10:53 AM, Sebastian Tennant
Post by Sebastian Tennant
http://weitz.de/cl-who/#syntax
Hope this helps.
Not really, that's the documentation that I'm confused about. It
starts out with the statement that strings will be printed verbatim.
My variables are strings, but they aren't printed. I understand from
Jens' response that I can get the function to work by wrapping my
variables in (str ...), but I don't understand why that's necessary.
A form which is neither a string nor a keyword nor a list beginning
with a keyword will be left as is except for the following
Your variables are variables (symbols bound to addresses pointing to
strings), not strings.

--Jeff
Edi Weitz
2011-12-22 17:12:27 UTC
Permalink
Content preview: In your example, LABEL is not a string. It's a symbol that
happens to be bound to a string. A string is something that starts and ends
with double quotes. Note that, as the documentation says, WITH-OUTPUT-TO-STRING
is a macro. It has no way to look into the future and to know what value
the variable LABEL will have at a specific point in time... :) [...]

Content analysis details: (-100.7 points, 5.0 required)

pts rule name description
---- ---------------------- --------------------------------------------------
-0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low
trust
[209.85.210.51 listed in list.dnswl.org]
-100 USER_IN_WHITELIST From: address is in the user's white-list
0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider
(nhabedi[at]gmail.com)
-0.0 SPF_PASS SPF: sender matches SPF record
0.0 T_DKIM_INVALID DKIM-Signature header exists but is not valid
Archived-At: <http://permalink.gmane.org/gmane.lisp.who.general/287>

In your example, LABEL is not a string. It's a symbol that happens to
be bound to a string. A string is something that starts and ends with
double quotes.

Note that, as the documentation says, WITH-OUTPUT-TO-STRING is a
macro. It has no way to look into the future and to know what value
the variable LABEL will have at a specific point in time... :)

Cheers,
Edi.
Post by Tyler Smith
On Thu, Dec 22, 2011 at 10:53 AM, Sebastian Tennant
Post by Sebastian Tennant
http://weitz.de/cl-who/#syntax
Hope this helps.
Not really, that's the documentation that I'm confused about. It
starts out with the statement that strings will be printed verbatim.
My variables are strings, but they aren't printed. I understand from
Jens' response that I can get the function to work by wrapping my
variables in (str ...), but I don't understand why that's necessary.
 A form which is neither a string nor a keyword nor a list beginning
with a keyword will be left as is except for the following
* (multiple-value-bind (records fields)
   (select [genus] [species] :from "specimens" :where [< [specimen_id] 4])
 (loop for rec in records do
      (loop for label in fields
         for val in rec do
           (fresh-line)
           (princ (stringp label))
           (princ (stringp val)))))
TT
TT
TT
TT
TT
TT
NIL
*
Can someone explain why my variables aren't being treated as strings
within with-html-output?
 Forms that look like (str form1 form*) will be substituted with (let
((result form1)) (when result (princ result s))).
I'm new to Lisp (as must be clear by now!): what is the difference between
 (princ form1)
and
 (let ((result form1))
   (princ result))
Thanks for your help,
Tyler
_______________________________________________
cl-who-devel site list
http://common-lisp.net/mailman/listinfo/cl-who-devel
Tyler Smith
2011-12-22 17:19:25 UTC
Permalink
Content preview: On Thu, Dec 22, 2011 at 12:10 PM, Jeffrey Cunningham <***@jkcunningham.com>
wrote: > On Thu, 22 Dec 2011 09:03:43 -0800, Tyler Smith > <***@mail.mcgill.ca>
wrote: > >> On Thu, Dec 22, 2011 at 10:53 AM, Sebastian Tennant >> <***@smolny.plus.com>
wrote: >>> http://weitz.de/cl-who/#syntax >>> >>> Hope this helps. >>> >>
Post by Jeffrey Cunningham
Not really, that's the documentation that I'm confused about. It >> starts
out with the statement that strings will be printed verbatim. >> My variables
are strings, but they aren't printed. I understand from >> Jens' response
that I can get the function to work by wrapping my >> variables in (str ...),
but I don't understand why that's necessary. >> The documentation of str
suggests it's not necessary for strings: >> >> A form which is neither a string
nor a keyword nor a list beginning >> with a keyword will be left as is except
Post by Jeffrey Cunningham
Post by Sebastian Tennant
Post by Jeffrey Cunningham
Your variables are variables (symbols bound to addresses pointing
to > strings), not strings. > > --Jeff [...]

Content analysis details: (-0.7 points, 5.0 required)

pts rule name description
---- ---------------------- --------------------------------------------------
-0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low
trust
[209.85.215.51 listed in list.dnswl.org]
0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider
(sedgeboy[at]gmail.com)
-0.0 SPF_PASS SPF: sender matches SPF record
0.0 T_DKIM_INVALID DKIM-Signature header exists but is not valid
Archived-At: <http://permalink.gmane.org/gmane.lisp.who.general/288>

On Thu, Dec 22, 2011 at 12:10 PM, Jeffrey Cunningham
Post by Jeffrey Cunningham
On Thu, 22 Dec 2011 09:03:43 -0800, Tyler Smith
On Thu, Dec 22, 2011 at 10:53 AM, Sebastian Tennant
Post by Sebastian Tennant
http://weitz.de/cl-who/#syntax
Hope this helps.
Not really, that's the documentation that I'm confused about. It
starts out with the statement that strings will be printed verbatim.
My variables are strings, but they aren't printed. I understand from
Jens' response that I can get the function to work by wrapping my
variables in (str ...), but I don't understand why that's necessary.
  A form which is neither a string nor a keyword nor a list beginning
with a keyword will be left as is except for the following
Your variables are variables (symbols bound to addresses pointing to
strings), not strings.
--Jeff
Oh, I see. Is this a special circumstance of the macro then, or are
symbols bound to strings and actual strings not interchangeable in
Lisp?

Thanks for your patience,

Tyler
Edi Weitz
2011-12-22 17:30:11 UTC
Permalink
Content preview: On Thu, Dec 22, 2011 at 6:19 PM, Tyler Smith <***@mail.mcgill.ca>
wrote: > Oh, I see. Is this a special circumstance of the macro then, or
are > symbols bound to strings and actual strings not interchangeable in >
Lisp? [...]

Content analysis details: (-100.7 points, 5.0 required)

pts rule name description
---- ---------------------- --------------------------------------------------
-0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low
trust
[209.85.160.51 listed in list.dnswl.org]
-100 USER_IN_WHITELIST From: address is in the user's white-list
0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider
(nhabedi[at]gmail.com)
-0.0 SPF_PASS SPF: sender matches SPF record
0.0 T_DKIM_INVALID DKIM-Signature header exists but is not valid
Archived-At: <http://permalink.gmane.org/gmane.lisp.who.general/289>
Post by Tyler Smith
Oh, I see. Is this a special circumstance of the macro then, or are
symbols bound to strings and actual strings not interchangeable in
Lisp?
See my previous reply.

There's nothing specific about Lisp here. The concept you'll want to
look up is "literal".
Tyler Smith
2011-12-22 18:21:49 UTC
Permalink
Content preview: On Thu, Dec 22, 2011 at 12:30 PM, Edi Weitz <***@agharta.de>
wrote: > On Thu, Dec 22, 2011 at 6:19 PM, Tyler Smith <***@mail.mcgill.ca>
wrote: > >> Oh, I see. Is this a special circumstance of the macro then,
or are >> symbols bound to strings and actual strings not interchangeable
in >> Lisp? > > See my previous reply. > > There's nothing specific about
Lisp here. The concept you'll want to > look up is "literal". > [...]

Content analysis details: (-0.7 points, 5.0 required)

pts rule name description
---- ---------------------- --------------------------------------------------
-0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low
trust
[209.85.215.51 listed in list.dnswl.org]
0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider
(sedgeboy[at]gmail.com)
-0.0 SPF_PASS SPF: sender matches SPF record
0.0 T_DKIM_INVALID DKIM-Signature header exists but is not valid
Archived-At: <http://permalink.gmane.org/gmane.lisp.who.general/290>
Post by Edi Weitz
Post by Tyler Smith
Oh, I see. Is this a special circumstance of the macro then, or are
symbols bound to strings and actual strings not interchangeable in
Lisp?
See my previous reply.
There's nothing specific about Lisp here.  The concept you'll want to
look up is "literal".
Ok, I think I have a general understanding. I have to spend some more
time on macros and the distinction between run-time and compile time.

Thanks for your help,

Tyler
Vsevolod Dyomkin
2011-12-22 18:43:16 UTC
Permalink
Also if you want to use macros with CL-WHO, you might want to check
http://lisp-univ-etc.blogspot.com/2009/03/cl-who-macros.html

Best,
Vsevolod
Post by Tyler Smith
Post by Edi Weitz
Post by Tyler Smith
Oh, I see. Is this a special circumstance of the macro then, or are
symbols bound to strings and actual strings not interchangeable in
Lisp?
See my previous reply.
There's nothing specific about Lisp here. The concept you'll want to
look up is "literal".
Ok, I think I have a general understanding. I have to spend some more
time on macros and the distinction between run-time and compile time.
Thanks for your help,
Tyler
_______________________________________________
cl-who-devel site list
http://common-lisp.net/mailman/listinfo/cl-who-devel
Ron Garret
2011-12-22 18:38:43 UTC
Permalink
Content preview: On Dec 22, 2011, at 9:19 AM, Tyler Smith wrote: > On Thu,
Dec 22, 2011 at 12:10 PM, Jeffrey Cunningham > <***@jkcunningham.com>
wrote: >> On Thu, 22 Dec 2011 09:03:43 -0800, Tyler Smith >> <***@mail.mcgill.ca>
wrote: >> >>> On Thu, Dec 22, 2011 at 10:53 AM, Sebastian Tennant >>> <***@smolny.plus.com>
wrote: >>>> http://weitz.de/cl-who/#syntax >>>> >>>> Hope this helps. >>>>
Post by Tyler Smith
Post by Jeffrey Cunningham
Post by Sebastian Tennant
Post by Tyler Smith
Not really, that's the documentation that I'm confused about. It
starts out with the statement that strings will be printed verbatim. >>>
My variables are strings, but they aren't printed. I understand from >>>
Jens' response that I can get the function to work by wrapping my >>> variables
in (str ...), but I don't understand why that's necessary. >>> The documentation
of str suggests it's not necessary for strings: >>> >>> A form which is neither
a string nor a keyword nor a list beginning >>> with a keyword will be left
as is except for the following >>> substitutions: >>> >>> My variables are
already strings: >>> >> >> Your variables are variables (symbols bound to
addresses pointing to >> strings), not strings. >> >> --Jeff > > Oh, I see.
Is this a special circumstance of the macro then, [...]

Content analysis details: (-0.0 points, 5.0 required)

pts rule name description
---- ---------------------- --------------------------------------------------
-0.0 SPF_PASS SPF: sender matches SPF record
Archived-At: <http://permalink.gmane.org/gmane.lisp.who.general/292>
Post by Tyler Smith
On Thu, Dec 22, 2011 at 12:10 PM, Jeffrey Cunningham
Post by Jeffrey Cunningham
On Thu, 22 Dec 2011 09:03:43 -0800, Tyler Smith
On Thu, Dec 22, 2011 at 10:53 AM, Sebastian Tennant
Post by Sebastian Tennant
http://weitz.de/cl-who/#syntax
Hope this helps.
Not really, that's the documentation that I'm confused about. It
starts out with the statement that strings will be printed verbatim.
My variables are strings, but they aren't printed. I understand from
Jens' response that I can get the function to work by wrapping my
variables in (str ...), but I don't understand why that's necessary.
A form which is neither a string nor a keyword nor a list beginning
with a keyword will be left as is except for the following
Your variables are variables (symbols bound to addresses pointing to
strings), not strings.
--Jeff
Oh, I see. Is this a special circumstance of the macro then,
It's not a "special circumstance", it's what macros do. The S-expressions that are arguments to macros are passed verbatim (that is, without being evaluated) to the macro expander function for that macro. It gets confusing because one of the things that the macro expander can elect to do is to return that symbol as part of the macro expansion in a context where that symbol gets evaluated, so a symbol passed as an argument to a macro *might* become a variable reference, or it might not.
Post by Tyler Smith
or are
symbols bound to strings and actual strings not interchangeable in
Lisp?
Not in general, no. They are interchangeable only in contexts where a form is being evaluated for its value. That is a very common context, but there are others.

The case of WITH-HTML-OUTPUT is particularly confusing because it is a context where forms are evaluated, but NOT for their values. W-H-O operates by side-effect (in particular, by outputting HTML to a stream), an NOT by computing a return value. So when you write a literal string, W-H-O recognizes that and transforms it into a form that outputs that string to the HTML stream. When you write a symbol, CL-WHO leaves it untouched. So that symbol gets evaluated in the usual way, but then its value is discarded rather than output to the HTML stream. The STR form is there to tell CL-WHO that you want the value of the variable to be output to the HTML stream.

Try this:

(pprint (macroexpand '(with-html-output (*standard-output*) (:b "foo") (:b x))))

The result will look hairy, but don't be intimidated. Most of what you see is a MACROLET form that sets up some local macros. The relevant part will be the last three lines at the end.

rg
Edi Weitz
2011-12-22 19:40:47 UTC
Permalink
Content preview: On Thu, Dec 22, 2011 at 7:38 PM, Ron Garret <***@flownet.com>
wrote: > Try this: > > (pprint (macroexpand '(with-html-output (*standard-output*)
(:b "foo") (:b x)))) http://weitz.de/cl-who/#show-html-expansion [...]

Content analysis details: (-100.7 points, 5.0 required)

pts rule name description
---- ---------------------- --------------------------------------------------
-0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low
trust
[209.85.210.51 listed in list.dnswl.org]
-100 USER_IN_WHITELIST From: address is in the user's white-list
0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider
(nhabedi[at]gmail.com)
-0.0 SPF_PASS SPF: sender matches SPF record
0.0 T_DKIM_INVALID DKIM-Signature header exists but is not valid
Archived-At: <http://permalink.gmane.org/gmane.lisp.who.general/293>
Post by Ron Garret
(pprint (macroexpand '(with-html-output (*standard-output*) (:b "foo") (:b x))))
http://weitz.de/cl-who/#show-html-expansion
Tyler Smith
2011-12-22 22:04:46 UTC
Permalink
Content preview: On Thu, Dec 22, 2011 at 1:38 PM, Ron Garret <***@flownet.com>
wrote: > > It's not a "special circumstance", it's what macros do. The S-expressions
that are arguments to macros are passed verbatim (that is, without being
evaluated) to the macro expander function for that macro. It gets confusing
because one of the things that the macro expander can elect to do is to return
that symbol as part of the macro expansion in a context where that symbol
gets evaluated, so a symbol passed as an argument to a macro *might* become
a variable reference, or it might not. > [...]

Content analysis details: (-0.7 points, 5.0 required)

pts rule name description
---- ---------------------- --------------------------------------------------
-0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low
trust
[209.85.215.51 listed in list.dnswl.org]
0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider
(sedgeboy[at]gmail.com)
-0.0 SPF_PASS SPF: sender matches SPF record
0.0 T_DKIM_INVALID DKIM-Signature header exists but is not valid
Archived-At: <http://permalink.gmane.org/gmane.lisp.who.general/294>
It's not a "special circumstance", it's what macros do.  The S-expressions that are arguments to macros are passed verbatim (that is, without being evaluated) to the macro expander function for that macro.  It gets confusing because one of the things that the macro expander can elect to do is to return that symbol as part of the macro expansion in a context where that symbol gets evaluated, so a symbol passed as an argument to a macro *might* become a variable reference, or it might not.
Ah, I see this now. Even within W-H-O, the same variable gets
evaluated in some places: if I use a variable for an attribute value
it gets evaluated, but but not when I use it as the tag body. This is
clearly spelled out in the docs, I see now, but it took me a while to
wrap my head around it.

Thanks!

Tyler

Loading...