unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* string parsing/preparation for latex
@ 2005-11-07 13:53 David Pirotte
  2005-11-07 15:49 ` Jose Roberto B. de A. Monteiro
  2005-11-08  0:34 ` Jon Wilson
  0 siblings, 2 replies; 13+ messages in thread
From: David Pirotte @ 2005-11-07 13:53 UTC (permalink / raw)


does anyone understands why this does not return the expected result

	(define *texu/reserved-characters*
	  ;; % \ { } ~ $ & # ^ _
	  '(
	    #\%		;; comments
	    #\\		;; command(follows by a space)
	    #\{		;; definition of treatment block(main_arg)
	    #\}		
	    #\~		;; indivisible space
	    #\$		;; mathematical mode 
	    #\&		;; tabulation ...
	    #\#		;; symbol of parameter zone
	    #\^		;; exponent
	    #\_		;; index
	    )
	  )

	(define (texu/prep-str-for-tex str)
	  (let ((str-lst (string->list str))
		(result (list)))
	    (for-each (lambda (chr)
			(if (member chr *texu/reserved-characters*)
			    (set! result (cons chr (cons #\\ result)))
			    (set! result (cons chr result))))
		      str-lst)
	    (reverse-list->string result)
	    ))

	(texu/prep-str-for-tex ";; % \ { } ~ $ & # ^ _")
	";; \\%  \\{ \\} \\~ \\$ \\& \\# \\^ \\_"

[the \ disapeared rather then being escaped]

		


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: string parsing/preparation for latex
  2005-11-07 13:53 string parsing/preparation for latex David Pirotte
@ 2005-11-07 15:49 ` Jose Roberto B. de A. Monteiro
  2005-11-08 14:52   ` David Pirotte
  2005-11-08  0:34 ` Jon Wilson
  1 sibling, 1 reply; 13+ messages in thread
From: Jose Roberto B. de A. Monteiro @ 2005-11-07 15:49 UTC (permalink / raw)


Em Seg, 2005-11-07 às 14:53 +0100, David Pirotte escreveu:
> does anyone understands why this does not return the expected result
> 
> 	(define *texu/reserved-characters*
> 	  ;; % \ { } ~ $ & # ^ _
> 	  '(
> 	    #\%		;; comments
> 	    #\\		;; command(follows by a space)
> 	    #\{		;; definition of treatment block(main_arg)
> 	    #\}		
> 	    #\~		;; indivisible space
> 	    #\$		;; mathematical mode 
> 	    #\&		;; tabulation ...
> 	    #\#		;; symbol of parameter zone
> 	    #\^		;; exponent
> 	    #\_		;; index
> 	    )
> 	  )
> 
> 	(define (texu/prep-str-for-tex str)
> 	  (let ((str-lst (string->list str))
> 		(result (list)))
> 	    (for-each (lambda (chr)
> 			(if (member chr *texu/reserved-characters*)
> 			    (set! result (cons chr (cons #\\ result)))
> 			    (set! result (cons chr result))))
> 		      str-lst)
> 	    (reverse-list->string result)
> 	    ))

Because you should have this string: ";; % \\ { } ~ $ & # ^ _", with two
backslashes instead of one... the same for "

> 	(texu/prep-str-for-tex ";; % \ { } ~ $ & # ^ _")
> 	";; \\%  \\{ \\} \\~ \\$ \\& \\# \\^ \\_"
> 
> [the \ disapeared rather then being escaped]
> 
> 		
> 
> 
> _______________________________________________
> Guile-user mailing list
> Guile-user@gnu.org
> http://lists.gnu.org/mailman/listinfo/guile-user


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: string parsing/preparation for latex
  2005-11-07 13:53 string parsing/preparation for latex David Pirotte
  2005-11-07 15:49 ` Jose Roberto B. de A. Monteiro
@ 2005-11-08  0:34 ` Jon Wilson
  2005-11-08 14:26   ` David Pirotte
  1 sibling, 1 reply; 13+ messages in thread
From: Jon Wilson @ 2005-11-08  0:34 UTC (permalink / raw)


Hi David,
This is interesting...  Just for the sake of my curiousity, could you 
describe what project this is for?
Regards,
Jon

David Pirotte wrote:
> does anyone understands why this does not return the expected result
> 
> 	(define *texu/reserved-characters*
> 	  ;; % \ { } ~ $ & # ^ _
> 	  '(
> 	    #\%		;; comments
> 	    #\\		;; command(follows by a space)
> 	    #\{		;; definition of treatment block(main_arg)
> 	    #\}		
> 	    #\~		;; indivisible space
> 	    #\$		;; mathematical mode 
> 	    #\&		;; tabulation ...
> 	    #\#		;; symbol of parameter zone
> 	    #\^		;; exponent
> 	    #\_		;; index
> 	    )
> 	  )
> 
> 	(define (texu/prep-str-for-tex str)
> 	  (let ((str-lst (string->list str))
> 		(result (list)))
> 	    (for-each (lambda (chr)
> 			(if (member chr *texu/reserved-characters*)
> 			    (set! result (cons chr (cons #\\ result)))
> 			    (set! result (cons chr result))))
> 		      str-lst)
> 	    (reverse-list->string result)
> 	    ))
> 
> 	(texu/prep-str-for-tex ";; % \ { } ~ $ & # ^ _")
> 	";; \\%  \\{ \\} \\~ \\$ \\& \\# \\^ \\_"
> 
> [the \ disapeared rather then being escaped]
> 
> 		
> 
> 
> _______________________________________________
> Guile-user mailing list
> Guile-user@gnu.org
> http://lists.gnu.org/mailman/listinfo/guile-user


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: string parsing/preparation for latex
  2005-11-08  0:34 ` Jon Wilson
@ 2005-11-08 14:26   ` David Pirotte
  2005-11-08 20:42     ` Jon Wilson
  0 siblings, 1 reply; 13+ messages in thread
From: David Pirotte @ 2005-11-08 14:26 UTC (permalink / raw)


On Mon, 07 Nov 2005 18:34:16 -0600
Jon Wilson <j85wilson@fastmail.fm> wrote:

> Hi David,
> This is interesting...  Just for the sake of my curiousity, could you 
> describe what project this is for?

Hi John,

I'm writing a little peace of software in guile-gnome, which interface
with postgres for the data repository, and this part is to prepare reports
to be printed

David


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: string parsing/preparation for latex
  2005-11-07 15:49 ` Jose Roberto B. de A. Monteiro
@ 2005-11-08 14:52   ` David Pirotte
  2005-11-08 16:35     ` Jose Roberto B. de A. Monteiro
  2005-11-08 16:56     ` Stephen Compall
  0 siblings, 2 replies; 13+ messages in thread
From: David Pirotte @ 2005-11-08 14:52 UTC (permalink / raw)


On Mon, 07 Nov 2005 13:49:01 -0200
"Jose Roberto B. de A. Monteiro" <betoes@sel.eesc.usp.br> wrote:

> Em Seg, 2005-11-07 às 14:53 +0100, David Pirotte escreveu:
> > does anyone understands why this does not return the expected result
> > 
> > 	(define *texu/reserved-characters*
> > 	  ;; % \ { } ~ $ & # ^ _
> > 	  '(
> > 	    #\%		;; comments
> > 	    #\\		;; command(follows by a space)
> > 	    #\{		;; definition of treatment block(main_arg)
> > 	    #\}		
> > 	    #\~		;; indivisible space
> > 	    #\$		;; mathematical mode 
> > 	    #\&		;; tabulation ...
> > 	    #\#		;; symbol of parameter zone
> > 	    #\^		;; exponent
> > 	    #\_		;; index
> > 	    )
> > 	  )
> > 
> > 	(define (texu/prep-str-for-tex str)
> > 	  (let ((str-lst (string->list str))
> > 		(result (list)))
> > 	    (for-each (lambda (chr)
> > 			(if (member chr *texu/reserved-characters*)
> > 			    (set! result (cons chr (cons #\\ result)))
> > 			    (set! result (cons chr result))))
> > 		      str-lst)
> > 	    (reverse-list->string result)
> > 	    ))
> 
> Because you should have this string: ";; % \\ { } ~ $ & # ^ _", with two
> backslashes instead of one... the same for "

but that is not possible, because the string (here simulated) is a user typed in string
 (through a gtk interface programmed in guile-gnome) upon which I have no control. 
I precisly wish to parse it to create a .tex file later processed by latex ...


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: string parsing/preparation for latex
  2005-11-08 14:52   ` David Pirotte
@ 2005-11-08 16:35     ` Jose Roberto B. de A. Monteiro
  2005-11-08 16:56     ` Stephen Compall
  1 sibling, 0 replies; 13+ messages in thread
From: Jose Roberto B. de A. Monteiro @ 2005-11-08 16:35 UTC (permalink / raw)


Em Ter, 2005-11-08 às 15:52 +0100, David Pirotte escreveu:
> On Mon, 07 Nov 2005 13:49:01 -0200
> "Jose Roberto B. de A. Monteiro" <betoes@sel.eesc.usp.br> wrote:
> 
> > Em Seg, 2005-11-07 às 14:53 +0100, David Pirotte escreveu:
> > > does anyone understands why this does not return the expected result
> > > 
> > > 	(define *texu/reserved-characters*
> > > 	  ;; % \ { } ~ $ & # ^ _
> > > 	  '(
> > > 	    #\%		;; comments
> > > 	    #\\		;; command(follows by a space)
> > > 	    #\{		;; definition of treatment block(main_arg)
> > > 	    #\}		
> > > 	    #\~		;; indivisible space
> > > 	    #\$		;; mathematical mode 
> > > 	    #\&		;; tabulation ...
> > > 	    #\#		;; symbol of parameter zone
> > > 	    #\^		;; exponent
> > > 	    #\_		;; index
> > > 	    )
> > > 	  )
> > > 
> > > 	(define (texu/prep-str-for-tex str)
> > > 	  (let ((str-lst (string->list str))
> > > 		(result (list)))
> > > 	    (for-each (lambda (chr)
> > > 			(if (member chr *texu/reserved-characters*)
> > > 			    (set! result (cons chr (cons #\\ result)))
> > > 			    (set! result (cons chr result))))
> > > 		      str-lst)
> > > 	    (reverse-list->string result)
> > > 	    ))
> > 
> > Because you should have this string: ";; % \\ { } ~ $ & # ^ _", with two
> > backslashes instead of one... the same for "
> 
> but that is not possible, because the string (here simulated) is a user typed in string
>  (through a gtk interface programmed in guile-gnome) upon which I have no control. 
> I precisly wish to parse it to create a .tex file later processed by latex ...

Ok, look at the foloowing:

guile> (define s ";; % \\ { } ~ $ & # ^ _")

We need the double backslash here to define one backslash in the
internal string code. Then:

guile> s
";; % \\ { } ~ $ & # ^ _"
guile> (texu/prep-str-for-tex s)
";; \\% \\\\ \\{ \\} \\~ \\$ \\& \\# \\^ \\_"

Well, I believe that your code is working properly. Each pair of '\' is
in reality one '\'...





_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: string parsing/preparation for latex
  2005-11-08 14:52   ` David Pirotte
  2005-11-08 16:35     ` Jose Roberto B. de A. Monteiro
@ 2005-11-08 16:56     ` Stephen Compall
  1 sibling, 0 replies; 13+ messages in thread
From: Stephen Compall @ 2005-11-08 16:56 UTC (permalink / raw)


David Pirotte writes: 
> but that is not possible, because the string (here simulated) 
> is a user typed in string  (through a gtk interface 
> programmed in guile-gnome) upon which I have no control. 
> I precisly wish to parse it to create a .tex file later 
> processed by latex ...

If you expect users to type in strings as Scheme expressions, I would
think you'd have no problem expecting them to know that backslashes have
to be escaped.

In other words, I think you're confusing reading strings, in the sense
of the `read' function for Scheme expressions, and reading raw character
data *into* strings, which is the usual practice when grabbing GUI form
fields, unless of course the form field is supposed to contain a Scheme
expression.

guile> (use-modules (ice-9 rdelim))
guile> (texu/prep-str-for-tex (read-line))
;; % \ { } ~ $ & # ^ _
=> ";; \\% \\\\ \\{ \\} \\~ \\$ \\& \\# \\^ \\_"
guile> (begin (display (texu/prep-str-for-tex (read-line))) (newline))
;; % \ { } ~ $ & # ^ _
-| ;; \% \\ \{ \} \~ \$ \& \# \^ \_

Where guile> is a prompt, => means a return value, -| means printed
line, all else is typed.

--
Stephen Compall
http://scompall.nocandysoftware.com/blog



_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: string parsing/preparation for latex
  2005-11-08 14:26   ` David Pirotte
@ 2005-11-08 20:42     ` Jon Wilson
  2005-11-08 21:08       ` klaus schilling
  0 siblings, 1 reply; 13+ messages in thread
From: Jon Wilson @ 2005-11-08 20:42 UTC (permalink / raw)


Hi David,
If this LaTeX + guile stuff goes well, might you consider releasing it
separately from guile-gnome?  I think it could be very useful in its
own right.
Is it intended to parse LaTeX, or to create LaTeX?  If create, from
what?  If parse, for what purpose?

Sorry for all the questions.  I've been mentally toying with the idea
of writing a set of macros that take guile mathematical expressions as
input and then output a string which can be cut and pasted into a
LaTeX document in math mode.  So, seeing your post piqued my interest.

ie:

(scheme->LaTeX (+ 7 (/ 18 5)))
=>
"7 + \frac{18}{5}"
Regards,
Jon

David Pirotte wrote:
> On Mon, 07 Nov 2005 18:34:16 -0600
> Jon Wilson <j85wilson@fastmail.fm> wrote:
> 
> 
>>Hi David,
>>This is interesting...  Just for the sake of my curiousity, could you 
>>describe what project this is for?
> 
> 
> Hi John,
> 
> I'm writing a little peace of software in guile-gnome, which interface
> with postgres for the data repository, and this part is to prepare reports
> to be printed
> 
> David
> 
> 
> _______________________________________________
> Guile-user mailing list
> Guile-user@gnu.org
> http://lists.gnu.org/mailman/listinfo/guile-user



_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: string parsing/preparation for latex
  2005-11-08 20:42     ` Jon Wilson
@ 2005-11-08 21:08       ` klaus schilling
  2005-11-09 13:24         ` Jon Wilson
  0 siblings, 1 reply; 13+ messages in thread
From: klaus schilling @ 2005-11-08 21:08 UTC (permalink / raw)
  Cc: guile-user

Jon Wilson writes:
 > 
 > (scheme->LaTeX (+ 7 (/ 18 5)))
 > =>
 > "7 + \frac{18}{5}"


shouldn't the argument be quoted? otherwise it will be evaluated 
straight to 10.6 (or whatever style of representation has been
chosen for rationals, the unstable versions for guile seem to 
provide for more possibilities)

Klaus Schilling


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: string parsing/preparation for latex
  2005-11-08 21:08       ` klaus schilling
@ 2005-11-09 13:24         ` Jon Wilson
  2005-11-14  3:17           ` Stephen Compall
  0 siblings, 1 reply; 13+ messages in thread
From: Jon Wilson @ 2005-11-09 13:24 UTC (permalink / raw)


Hi Klaus,
scheme->LaTeX would have to be a macro, then it receives its arguments
before they are evaluated.  Unless of course I'm mistaken about the
nature of macros.  (I'm still relatively new to the idea.)

eg (simple example, its 7am here and I got to sleep at 2.):
(define-macro (+->LaTeX +expr)
  (string-append
    (number->string (cadr +expr))
    "+"
    (number->string (caddr +expr))))

(+->LaTeX (+ 9 15))
=>
"9+15"

Unless I've screwed something up in my sleepiness, but I just tested
it and yeah that seems to work as far as a proof of concept goes.
Guile version 1.6.4, I don't know if things have changed in unstable
version.

Regards,
Jon

klaus schilling wrote:
> Jon Wilson writes:
>  > 
>  > (scheme->LaTeX (+ 7 (/ 18 5)))
>  > =>
>  > "7 + \frac{18}{5}"
> 
> 
> shouldn't the argument be quoted? otherwise it will be evaluated 
> straight to 10.6 (or whatever style of representation has been
> chosen for rationals, the unstable versions for guile seem to 
> provide for more possibilities)
> 
> Klaus Schilling



_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: string parsing/preparation for latex
  2005-11-09 13:24         ` Jon Wilson
@ 2005-11-14  3:17           ` Stephen Compall
  2005-11-15  1:50             ` Jon Wilson
  0 siblings, 1 reply; 13+ messages in thread
From: Stephen Compall @ 2005-11-14  3:17 UTC (permalink / raw)
  Cc: guile-user


[-- Attachment #1.1: Type: text/plain, Size: 1248 bytes --]

On Wed, 2005-11-09 at 07:24 -0600, Jon Wilson wrote:
> scheme->LaTeX would have to be a macro, then it receives its arguments
> before they are evaluated.  Unless of course I'm mistaken about the
> nature of macros.  (I'm still relatively new to the idea.)

Yes, but you'd lose more than you'd gain by making it a macro.  Unless
you really wanted to do something crazy like:

(define-syntax scheme->LaTeX
  (syntax-rules (+ /)
    ((_ (+ f1 f2 ... f3))
     (string-append "(" (scheme->LaTeX (+ f1 f2 ...))
                    " + " (scheme->LaTeX f3) ")"))
    ((_ (+ f1))
     (scheme->LaTeX f1))
    ((_ (/ f1 f2 f3 ...))
     (string-append "\frac{" (scheme->LaTeX f1)
                    "}{" (scheme->LaTeX (* f2 f3 ...)) "}"))
    ;;and so on....
    ))

Of course that has many drawbacks of its own.  Better to just handle it
in functions, which, for example, wouldn't have the spurious parenthesis
problem of the multiple + argument syntax, and so on.  Not to mention
the added benefit of scheme->LaTeXing forms unknown at load-time without
using eval.

Remember Norvig's first step to writing a macro: Decide if the macro is
really necessary.

-- 
Stephen Compall
http://scompall.nocandysoftware.com/blog

[-- Attachment #1.2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

[-- Attachment #2: Type: text/plain, Size: 140 bytes --]

_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: string parsing/preparation for latex
  2005-11-14  3:17           ` Stephen Compall
@ 2005-11-15  1:50             ` Jon Wilson
  2005-11-16  2:52               ` Stephen Compall
  0 siblings, 1 reply; 13+ messages in thread
From: Jon Wilson @ 2005-11-15  1:50 UTC (permalink / raw)


Hi Stephen,
Yeah, I guess you're right.  Like I said, I'm new to the whole idea of
macros, and I'm kind of looking for a really good use-case for the
things.  Can you suggest a non-contrived situation in which a macro
would be the best solution?
Regards,
Jon

Stephen Compall wrote:
> On Wed, 2005-11-09 at 07:24 -0600, Jon Wilson wrote:
> 
>>scheme->LaTeX would have to be a macro, then it receives its arguments
>>before they are evaluated.  Unless of course I'm mistaken about the
>>nature of macros.  (I'm still relatively new to the idea.)
> 
> 
> Yes, but you'd lose more than you'd gain by making it a macro.  Unless
> you really wanted to do something crazy like:
> 
> (define-syntax scheme->LaTeX
>   (syntax-rules (+ /)
>     ((_ (+ f1 f2 ... f3))
>      (string-append "(" (scheme->LaTeX (+ f1 f2 ...))
>                     " + " (scheme->LaTeX f3) ")"))
>     ((_ (+ f1))
>      (scheme->LaTeX f1))
>     ((_ (/ f1 f2 f3 ...))
>      (string-append "\frac{" (scheme->LaTeX f1)
>                     "}{" (scheme->LaTeX (* f2 f3 ...)) "}"))
>     ;;and so on....
>     ))
> 
> Of course that has many drawbacks of its own.  Better to just handle it
> in functions, which, for example, wouldn't have the spurious parenthesis
> problem of the multiple + argument syntax, and so on.  Not to mention
> the added benefit of scheme->LaTeXing forms unknown at load-time without
> using eval.
> 
> Remember Norvig's first step to writing a macro: Decide if the macro is
> really necessary.
> 



_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: string parsing/preparation for latex
  2005-11-15  1:50             ` Jon Wilson
@ 2005-11-16  2:52               ` Stephen Compall
  0 siblings, 0 replies; 13+ messages in thread
From: Stephen Compall @ 2005-11-16  2:52 UTC (permalink / raw)
  Cc: guile-user


[-- Attachment #1.1: Type: text/plain, Size: 3804 bytes --]

On Mon, 2005-11-14 at 19:50 -0600, Jon Wilson wrote: 
> Yeah, I guess you're right.  Like I said, I'm new to the whole idea of
> macros, and I'm kind of looking for a really good use-case for the
> things.  Can you suggest a non-contrived situation in which a macro
> would be the best solution?

WARNING: All this is wildly untested, debugging is left as an exercise
for those more patient

Implicit begin when you don't care about the else case (from R5RS spec):

(define-syntax when
  (syntax-rules ()
    ((_ test when-form wf2 ...)
     (if test (begin when-form wf2 ...)))))

Example: (when (some-test)
           (do-this) (and-do-that) and-return-this)

A list collector:

(use-modules (srfi srfi-1))

(define-macro (with-collect setters . body-forms)
  ;;note: use make-symbol instead of gensym in 1.7
  (let ((syms (map (lambda (ign) (gensym)) setters)))
    `(let ,(map (lambda (sym) (list sym ''())) syms)
       (let ,(map (lambda (sym setter)
                    (list setter `(lambda (elt)
                                    (set! ,sym (cons elt ,sym)))))
                  syms setters)
         ,@body-forms
         (values ,@(map (lambda (sym) (list 'reverse! sym)) syms))))))

Example: (with-collect (c) (c 1) (c 3)) => (1 3)

As you can see, with-collect could be implemented as the function
call-with-collect, by instead of passing body forms you pass a 1-arg
closure that receives the setter function.  However, the above supports
multiple collects, whereas a function would have to receive another
argument telling it how many collecting functions to pass.  Your
decision, of course, depends on whether you think supporting more
collectors is important.

The problem with-collect fixes directly is the (set! var (cons elt
var)) ... (reverse! var) pattern.  This is called push/nreverse in Lisp,
the former of which you can easily implement with a macro:

(define-macro (push elt place)
  (if (pair? place)             ;using generalized set?
      (let ((place-vars (map (lambda (ign) (gensym)) place)))
        `(let ,(map list place-vars place)
           (set! ,place-vars (cons ,elt ,place-vars))))
      `(set! ,place (cons ,elt ,place))))

Example: (let ((l '())) (push 1 l) (push 2 l) l) => (2 1)

Also in with-collect, I used a common pattern for map several times.
Perhaps that wants a macro, with a set of predefined names or something,
exploiting the common occurrence of inline lambda forms containing just
one body form.

cond with each clause unwrapped:

(define-macro (flat-cond . clauses)
  (cons 'cond
        (with-collect (add-cond-form)
          (let lp ((whats-left clauses))
            (cond ((null? whats-left) #f)
                  ((and (pair? whats-left) (pair? (rest whats-left)))
                   (add-cond-form
                    (list (first whats-left) (second whats-left)))
                   (lp (cddr whats-left)))
                  (else (scm-error 'misc-error "flat-cond"
                                   "bad cond clause ~S in ~S"
                                   (list whats-left clauses) #f)))))))

Example: (flat-cond (null? whats-left) #f
                    (pair? whats-left) (add-cond-form something)
                    else (error "bad flat-cond clause"))

I suppose one rubric for macros might be whether it improves clarity of
expression, if you share that goal in choosing Scheme as a programming
language.  map is great, but maybe a macro version with an implicit
lambda-form and lambda list might be very helpful in the definition of
with-collect, in terms of clarity.  Also, while you could implement
flat-cond as a function, that would ultimately get in your way.

-- 
Stephen Compall
http://scompall.nocandysoftware.com/blog

[-- Attachment #1.2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

[-- Attachment #2: Type: text/plain, Size: 140 bytes --]

_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2005-11-16  2:52 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-11-07 13:53 string parsing/preparation for latex David Pirotte
2005-11-07 15:49 ` Jose Roberto B. de A. Monteiro
2005-11-08 14:52   ` David Pirotte
2005-11-08 16:35     ` Jose Roberto B. de A. Monteiro
2005-11-08 16:56     ` Stephen Compall
2005-11-08  0:34 ` Jon Wilson
2005-11-08 14:26   ` David Pirotte
2005-11-08 20:42     ` Jon Wilson
2005-11-08 21:08       ` klaus schilling
2005-11-09 13:24         ` Jon Wilson
2005-11-14  3:17           ` Stephen Compall
2005-11-15  1:50             ` Jon Wilson
2005-11-16  2:52               ` Stephen Compall

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).