Help with match

Started by adamss3, March 23, 2007, 10:55:50 AM

Previous topic - Next topic

adamss3

I've started using match and have run into a problem.  I want to store the items used for the search in variables and then use these in the match invocation.  In the code snippet below, I would have expected both printlns to show the same results.  I'm sure that this is something simple, but I'm not seeing it.



(set 'a 1)

(set 'b 2)

(set 'c '(0 1 3 2 4))

(println (match '( * 1 * 2 *) c))

(println (match '( * a * b *) c))

(exit)

m35

#1
Looks like a quoting issue (it always throws me off too).



From the newlisp interpreter

> '( * 1 * 2 *)

(* 1 * 2 *)

> '( * a * b *)

(* a * b *)



They evaluate to different things, so it makes sense that match would have different results.



But if you try

> (list '* a '* b '*)

(* 1 * 2 *)

then the symbols a and b will be evaluated, resulting in what I think you're looking for.



There's probably a better way to explain/execute it, but hopefully this helps a little.

cormullion

#2
well , I think it's because the a and b are inside a quoted list, so are not being expanded. So you're looking for (* a * b *) and would find it only if c was '(0 a 3 b 4).



You want a fix, I suppose. :-) I'm not sure, but it might involve expand...



Edit: Then again, it might not. As m35 suggests:


(println (match (list '* a '* b '*) c))

I always get a bit confused by those multiplication signs appearing as wild-cards in a quoted list... ;-)

adamss3

#3
Thanks, that fixed it!

m35

#4
Ah that's right, expand. I wasn't familiar with that one. Could be a lot cleaner looking than all those '*