Page 1 of 1

Help with match

Posted: Fri Mar 23, 2007 5:55 pm
by 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)

Posted: Fri Mar 23, 2007 6:16 pm
by m35
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.

Posted: Fri Mar 23, 2007 6:16 pm
by cormullion
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:

Code: Select all

(println (match (list '* a '* b '*) c))
I always get a bit confused by those multiplication signs appearing as wild-cards in a quoted list... ;-)

Posted: Fri Mar 23, 2007 7:12 pm
by adamss3
Thanks, that fixed it!

Posted: Wed Mar 28, 2007 1:24 am
by m35
Ah that's right, expand. I wasn't familiar with that one. Could be a lot cleaner looking than all those '*