Hi Kristof, and welcome to the list!
> However I came across the following strange behaviour. In the
> equation:
>
> test '(A B) = ('A, 'B);
>
> I would expect test '(a (2 + 3)) to evaluate to ('a, '(2 + 3)),
Just make `test' a special form, then you get the behaviour that you
want, see below.
> however it evaluates to ('a, '5). Is this the defined behaviour?
Yes, it is, but only since Q 7.1. The general rule is that, while
special arguments of a function will never be evaluated, deferred
subterms inside a _non-special_ arg are evaluated automatically when
they are needed in the pattern matching process. This is a convenience
so that non-special functions can assume matched subterms to be in
(weak) normal form, which is important to make functions operating on
lazy data structures such as streams do the right thing.
For instance, consider the following definition:
deinterleave {} = {};
deinterleave {X,Y|Xs} = {(X,Y)|deinterleave Xs};
To match the left-hand side of the second rule (which is actually a
shorthand for {X|{Y|Xs}}, the pattern matcher has to evaluate the tail
of the actual stream parameter. Note that otherwise this equation would
never match a stream with deferred tail like {1..} = {1|iterate (+1)
((+1) 1)}.
This only happens when a pattern reaches inside a deferred subterm. If
you have an equation like
test 'X = '(X+1);
then X is _not_ evaluated, because it is unnecessary to decide the match:
==> test '(2 + 3)
'(2+3+1)
But in your example
test '(A B) = ('A, 'B);
the pattern reaches inside the argument of ('), so the pattern matcher
evaluates the argument to decide whether this value is an application or
not. (') is handled just like any other special data constructor here.
OTOH, if you make test itself a special form, then argument evaluation
is always inhibited and arguments are passed as is. So the following
will do what you want:
special test X;
test '(A B) = ('A, 'B);
Example:
==> test '(a (2 + 3))
('a,'(2+3))
I know that this is somewhat confusing, and in a program I recently
wrote I also ran into the very same trap. ;-) But this new "call by
need" way of doing pattern matching makes defining functions operating
on lazy data structures so much more convenient.
> Also, when using the Q interpreter to write scripts, it always prints
> the last object to stdout. Is there an option to turn it off? If
> I want to write a script that outputs some text, I wouldn't want the
> output to be clobbered with an object.
Explicitly exiting from the program with `exit 0' probably does what you
want.
Cheers,
Albert
--
Dr. Albert Gr"af
Dept. of Music-Informatics, University of Mainz, Germany
Email: Dr....@t-..., ag...@mu...
WWW: http://www.musikinformatik.uni-mainz.de/ag
|