[plt-scheme] Can I abuse with quasi...quotes?

From: Jos Koot (jos.koot at telefonica.net)
Date: Thu Jul 19 06:20:44 EDT 2007

Hi Paulo,

----- Original Message ----- 
From: "Paulo J. Matos" <pocm at soton.ac.uk>
To: "Jos Koot" <jos.koot at telefonica.net>
Cc: "mzscheme Mailing List" <plt-scheme at list.cs.brown.edu>
Sent: Thursday, July 19, 2007 11:20 AM
Subject: Re: [plt-scheme] Can I abuse with quasi...quotes?

> On 19/07/07, Jos Koot <jos.koot at telefonica.net> wrote:
>> Both methods are allright and they are equivalent to each other.
> OK, so using `(and ,@(1 2 3)) also results in a mutable cons?

Yes, the resulting lists are mutable.

>> In fact the quasiquote form is expanded into an expression that does the same 
>> as
>> your example with cons.
>> In your example I dont see any abuse of quasiquote.
> When I said abuse I meant in whole my source code, I use them heavily,
> so I used the word abuse in the sense that I didn't know if using them
> a lot would have any performance penalty when compared to using cons.

Hardly any difference at run time (assuming your program is not very frequently 
evaluating uncompiled sexprs at run time) Compilation, may take some more time, 
because of the expansion of the quasiquote forms, but the penalty will be 
noticable only in very large programs or in programs that frequently evaluate 
uncompiled sexprs at run time.
>> ,@ must be followed by an expression that evaluates to a list.
>> This list is spliced in. This means that , at expr is replaced by the list but
>> without its (outer) pair of parentheses.
>> , at expr must be part of a list (possibly an improper list) Your correct 
>> example
>> makes me think that you already know this.
> If `(and 1 ,@(2 3 4)) results in a mutable cons of 1 and (2 3 4),
> would it be more efficient PLT-Scheme -wise to use cons-immutable?

If 'more efficient' means faster, I dont think so, but it may be more efficient 
with respect to your own time spent coding programs with the help of quasiquote, 
assuming you are writing the code by hand. I for myself make less mistakes using 
quasiquote than writing explicit applications of cons, list and append and 
adding all quotes at the right places. The interpreter/compiler takes may be 
1/10000 of the time I need myself writing correctly all quotations and all 
applications of cons, list and append.
Another aspect is protection against mutation, if that is your goal. I don't 
know of a quasiquote producing immutable pairs, but it is not difficult to 
rewrite the syntaxes quasiquote, unquote and unquote-splicing such as to produce 
immutable lists. But yet easier would be just to disable set-car! and set-cdr! 
and the like (e.g. by means of a language defining module that does not provide 

I for myself focus more on the following points.
- To be sure of efficient algorithms (as opposed to their encoding into a 
computer program) Some decennia ago I was punished hard for writing very 
efficient code for an inefficient algorithm.
- To choose a method of coding that is not error prone.
- This includes writing in conventional style so the code is humanly 
understandable and maintainable.
- To choose the right language, the right data representation and so on. Scheme 
is a good language for symbolic manipulations on sexprs.
- To let the compilers do optimization for me. The're good at it.

As far as I can judge from your information, I think you should not be afraid of 
using quasiquote.
Cheers, Jos Koot

> Cheers,
> Paulo Matos
> Yes, this fortunately I already know. :) Thank you!
>> Good luck, Jos Koot
>> ----- Original Message -----
>> From: "Paulo J. Matos" <pocm at soton.ac.uk>
>> To: "mzscheme Mailing List" <plt-scheme at list.cs.brown.edu>
>> Sent: Thursday, July 19, 2007 12:21 AM
>> Subject: [plt-scheme] Can I abuse with quasi...quotes?
>> > Hi all,
>> >
>> > I've been working a lot with propositional logic formulas where the
>> > operator is prefixed, like:
>> > '(and x1 (or x2 x3) (<=> x4 (not x1)))
>> >
>> > Now, to build formulas I'm using quasiquote and sometimes even abusing it.
>> > For example, given two list of variables of equal length. I do the
>> > following to create an assertion of equivalence between them:
>> > `(and ,@(map (lambda (var1 var2) `(<=> ,var1 ,var2)) varlst1 varlst2))
>> >
>> > I don't really understand what happens inside with ,@ and , but I keep
>> > wondering if this is better:
>> > (cons 'and (map (lambda (var1 var2) (list '<=> var1 var2)) varlst1 
>> > varlst2))
>> >
>> > I don't know if it matter but for the record, I don't use list
>> > mutation, so is this better than the first form?
>> > Is there a better form?
>> >
>> > Cheers,
>> >
>> > --
>> > Paulo Jorge Matos - pocm at soton.ac.uk
>> > http://www.personal.soton.ac.uk/pocm
>> > PhD Student @ ECS
>> > University of Southampton, UK
>> > _________________________________________________
>> >  For list-related administrative tasks:
>> >  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>> >
> -- 
> Paulo Jorge Matos - pocm at soton.ac.uk
> http://www.personal.soton.ac.uk/pocm
> PhD Student @ ECS
> University of Southampton, UK

Posted on the users mailing list.