* unusual let binding (of lp) - in function element->xml from sxml simple
@ 2024-02-03 21:51 Andreas Reuleaux
2024-02-03 21:55 ` Thompson, David
2024-02-04 8:46 ` Damien Mattei
0 siblings, 2 replies; 6+ messages in thread
From: Andreas Reuleaux @ 2024-02-03 21:51 UTC (permalink / raw)
To: guile-user
Hi,
I have a hard time understanding the let binding lp ...
in the function element->xml - from module sxml simple -
repeated here for convenience.
--8<---------------cut here---------------start------------->8---
(define (element->xml tag attrs body port)
(check-name tag)
(display #\< port)
(display tag port)
(if attrs
(let lp ((attrs attrs))
(if (pair? attrs)
(let ((attr (car attrs)))
(display #\space port)
(if (pair? attr)
(attribute->xml (car attr) (cdr attr) port)
(error "bad attribute" tag attr))
(lp (cdr attrs)))
(if (not (null? attrs))
(error "bad attributes" tag attrs)))))
(if (pair? body)
(begin
(display #\> port)
(let lp ((body body))
(cond
((pair? body)
(sxml->xml (car body) port)
(lp (cdr body)))
((null? body)
(display "</" port)
(display tag port)
(display ">" port))
(else
(error "bad element body" tag body)))))
(display " />" port)))
--8<---------------cut here---------------end--------------->8---
Some background: sxml simple is - as the name implies -
simple (or should be so). - It's implements
* functions for parsing xml to s-expression
xml->sxml, ...
* and functions for serializing sxml to xml
sxml->xml, ... (and among them: element->xml, as above)
This second task (group of functions) is even simpler
(than the parsing stuff) - I would say.
Now these function (sxml->xml, element->xml) work for
me: I can test them with some simple s-expression foo for example
--8<---------------cut here---------------start------------->8---
(define foo
'(d:para (d:emphasis (@ (role "strong")) "system analyst") " for " (d:link (@ (xl:href "http://www.blah.com")) (d:emphasis (@ (role "strong")) "Blah")) " in Berlin, and so on"))
--8<---------------cut here---------------end--------------->8---
--8<---------------cut here---------------start------------->8---
scheme@(guile-user) [13]> (sxml->xml foo)
<d:para><d:emphasis role="strong">system analyst</d:emphasis> for
<d:link xl:href="http://www.blah.com"><d:emphasis role="strong">Blah</d:emphasis></d:link> in Berlin, and so on</d:para>
--8<---------------cut here---------------end--------------->8---
sxml->xml making use of element->xml above.
Now with regards to the binding / usage of lp above: it is
bound / used several times:
(let lp ((attrs attrs)) ...
let lp be a function that ... does what?
(lp (cdr attrs)))
now call this function with (cdr attrs).
What does it do?
set attrs to (cdr attrs)?
then similar below
(let lp ((body body)) ...
???
(lp (cdr body)))
???
For one thing I am used to
(let bindings body)
with bindings being of the form
((x 'foo)
(y 'bar))
(i.e. bind x with 'foo, y with 'bar when executing body)
for example, lp is not of this style. Maybe if there is just one variable bound
one can shortcut this to
let x 'foo body
Is this what's going on above? And if so:
then lp is bound to a list of one pair? :
((attrs attrs))
how come can we call this as a function then later.
And if attrs is a function (which I doubt) that what
is the application of attrs to attrs?
Anyway: confused. - Thanks in advanc,
A'
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: unusual let binding (of lp) - in function element->xml from sxml simple
2024-02-03 21:51 unusual let binding (of lp) - in function element->xml from sxml simple Andreas Reuleaux
@ 2024-02-03 21:55 ` Thompson, David
2024-02-03 22:16 ` Andreas Reuleaux
2024-02-04 8:46 ` Damien Mattei
1 sibling, 1 reply; 6+ messages in thread
From: Thompson, David @ 2024-02-03 21:55 UTC (permalink / raw)
To: Andreas Reuleaux; +Cc: guile-user
Hi Andreas,
On Sat, Feb 3, 2024 at 4:49 PM Andreas Reuleaux <rx@a-rx.info> wrote:
> sxml->xml making use of element->xml above.
> Now with regards to the binding / usage of lp above: it is
> bound / used several times:
>
>
> (let lp ((attrs attrs)) ...
> let lp be a function that ... does what?
>
>
> (lp (cdr attrs)))
> now call this function with (cdr attrs).
> What does it do?
> set attrs to (cdr attrs)?
>
>
> then similar below
> (let lp ((body body)) ...
> ???
>
>
> (lp (cdr body)))
> ???
>
>
> For one thing I am used to
>
> (let bindings body)
>
> with bindings being of the form
> ((x 'foo)
> (y 'bar))
>
> (i.e. bind x with 'foo, y with 'bar when executing body)
>
> for example, lp is not of this style. Maybe if there is just one variable bound
> one can shortcut this to
>
> let x 'foo body
>
> Is this what's going on above? And if so:
> then lp is bound to a list of one pair? :
>
> ((attrs attrs))
>
> how come can we call this as a function then later.
>
> And if attrs is a function (which I doubt) that what
> is the application of attrs to attrs?
This is a form of 'let' known as "named let". In the code you
reference, the variable 'lp' is a procedure, short for "loop", and the
let bindings give initial values for the loop. Calling 'lp' will bring
control back to the beginning of the 'let' with the bindings rebound
to new values. It's a convenient shorthand for simple loops.
See https://www.gnu.org/software/guile/manual/html_node/while-do.html#index-let-1
for more info.
Hope this helps,
- Dave
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: unusual let binding (of lp) - in function element->xml from sxml simple
2024-02-03 21:55 ` Thompson, David
@ 2024-02-03 22:16 ` Andreas Reuleaux
2024-02-04 6:30 ` tomas
0 siblings, 1 reply; 6+ messages in thread
From: Andreas Reuleaux @ 2024-02-03 22:16 UTC (permalink / raw)
To: Thompson, David; +Cc: guile-user
"Thompson, David" <dthompson2@worcester.edu> writes:
> This is a form of 'let' known as "named let". In the code you
> reference, the variable 'lp' is a procedure, short for "loop", and the
> let bindings give initial values for the loop. Calling 'lp' will bring
> control back to the beginning of the 'let' with the bindings rebound
> to new values. It's a convenient shorthand for simple loops.
>
> See https://www.gnu.org/software/guile/manual/html_node/while-do.html#index-let-1
> for more info.
>
> Hope this helps,
>
> - Dave
OK, thanks a lot ... and yes: this does help indeed,
(and I am list replying)
-Andreas
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: unusual let binding (of lp) - in function element->xml from sxml simple
2024-02-03 22:16 ` Andreas Reuleaux
@ 2024-02-04 6:30 ` tomas
0 siblings, 0 replies; 6+ messages in thread
From: tomas @ 2024-02-04 6:30 UTC (permalink / raw)
To: guile-user
[-- Attachment #1: Type: text/plain, Size: 790 bytes --]
On Sat, Feb 03, 2024 at 10:16:55PM +0000, Andreas Reuleaux wrote:
>
>
> "Thompson, David" <dthompson2@worcester.edu> writes:
>
>
> > This is a form of 'let' known as "named let". In the code you
> > reference, the variable 'lp' is a procedure, short for "loop", and the
> > let bindings give initial values for the loop. Calling 'lp' will bring
> > control back to the beginning of the 'let' with the bindings rebound
> > to new values. It's a convenient shorthand for simple loops.
> >
> > See https://www.gnu.org/software/guile/manual/html_node/while-do.html#index-let-1
> > for more info.
> >
> > Hope this helps,
> >
> > - Dave
>
>
> OK, thanks a lot ... and yes: this does help indeed,
I always think of it as letrec's little sister :-)
Cheers
--
t
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: unusual let binding (of lp) - in function element->xml from sxml simple
2024-02-03 21:51 unusual let binding (of lp) - in function element->xml from sxml simple Andreas Reuleaux
2024-02-03 21:55 ` Thompson, David
@ 2024-02-04 8:46 ` Damien Mattei
2024-02-04 15:37 ` Andreas Reuleaux
1 sibling, 1 reply; 6+ messages in thread
From: Damien Mattei @ 2024-02-04 8:46 UTC (permalink / raw)
To: Andreas Reuleaux; +Cc: guile-user
Hi Andreas,
a good explanation is here:
https://stackoverflow.com/questions/31909121/how-does-the-named-let-in-the-form-of-a-loop-work
personally i avoid to use it, as i can do the same with internal
definitions .
regards,
Damien
On Sat, Feb 3, 2024 at 10:49 PM Andreas Reuleaux <rx@a-rx.info> wrote:
> Hi,
>
> I have a hard time understanding the let binding lp ...
> in the function element->xml - from module sxml simple -
> repeated here for convenience.
>
>
> --8<---------------cut here---------------start------------->8---
> (define (element->xml tag attrs body port)
> (check-name tag)
> (display #\< port)
> (display tag port)
> (if attrs
> (let lp ((attrs attrs))
> (if (pair? attrs)
> (let ((attr (car attrs)))
> (display #\space port)
> (if (pair? attr)
> (attribute->xml (car attr) (cdr attr) port)
> (error "bad attribute" tag attr))
> (lp (cdr attrs)))
> (if (not (null? attrs))
> (error "bad attributes" tag attrs)))))
> (if (pair? body)
> (begin
> (display #\> port)
> (let lp ((body body))
> (cond
> ((pair? body)
> (sxml->xml (car body) port)
> (lp (cdr body)))
> ((null? body)
> (display "</" port)
> (display tag port)
> (display ">" port))
> (else
> (error "bad element body" tag body)))))
> (display " />" port)))
> --8<---------------cut here---------------end--------------->8---
>
> Some background: sxml simple is - as the name implies -
> simple (or should be so). - It's implements
>
> * functions for parsing xml to s-expression
> xml->sxml, ...
>
> * and functions for serializing sxml to xml
> sxml->xml, ... (and among them: element->xml, as above)
>
> This second task (group of functions) is even simpler
> (than the parsing stuff) - I would say.
>
> Now these function (sxml->xml, element->xml) work for
> me: I can test them with some simple s-expression foo for example
>
> --8<---------------cut here---------------start------------->8---
> (define foo
> '(d:para (d:emphasis (@ (role "strong")) "system analyst") " for "
> (d:link (@ (xl:href "http://www.blah.com")) (d:emphasis (@ (role
> "strong")) "Blah")) " in Berlin, and so on"))
> --8<---------------cut here---------------end--------------->8---
>
>
> --8<---------------cut here---------------start------------->8---
> scheme@(guile-user) [13]> (sxml->xml foo)
> <d:para><d:emphasis role="strong">system analyst</d:emphasis> for
> <d:link xl:href="http://www.blah.com"><d:emphasis
> role="strong">Blah</d:emphasis></d:link> in Berlin, and so on</d:para>
> --8<---------------cut here---------------end--------------->8---
>
>
> sxml->xml making use of element->xml above.
> Now with regards to the binding / usage of lp above: it is
> bound / used several times:
>
>
> (let lp ((attrs attrs)) ...
> let lp be a function that ... does what?
>
>
> (lp (cdr attrs)))
> now call this function with (cdr attrs).
> What does it do?
> set attrs to (cdr attrs)?
>
>
> then similar below
> (let lp ((body body)) ...
> ???
>
>
> (lp (cdr body)))
> ???
>
>
> For one thing I am used to
>
> (let bindings body)
>
> with bindings being of the form
> ((x 'foo)
> (y 'bar))
>
> (i.e. bind x with 'foo, y with 'bar when executing body)
>
> for example, lp is not of this style. Maybe if there is just one variable
> bound
> one can shortcut this to
>
> let x 'foo body
>
> Is this what's going on above? And if so:
> then lp is bound to a list of one pair? :
>
> ((attrs attrs))
>
> how come can we call this as a function then later.
>
> And if attrs is a function (which I doubt) that what
> is the application of attrs to attrs?
>
> Anyway: confused. - Thanks in advanc,
> A'
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2024-02-04 15:37 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-02-03 21:51 unusual let binding (of lp) - in function element->xml from sxml simple Andreas Reuleaux
2024-02-03 21:55 ` Thompson, David
2024-02-03 22:16 ` Andreas Reuleaux
2024-02-04 6:30 ` tomas
2024-02-04 8:46 ` Damien Mattei
2024-02-04 15:37 ` Andreas Reuleaux
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).