Help with match

For the Compleat Fan
Locked
adamss3
Posts: 14
Joined: Mon Oct 14, 2002 11:53 am

Help with match

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

m35
Posts: 171
Joined: Wed Feb 14, 2007 12:54 pm

Post 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.
Last edited by m35 on Fri Mar 23, 2007 6:16 pm, edited 1 time in total.

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

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

adamss3
Posts: 14
Joined: Mon Oct 14, 2002 11:53 am

Post by adamss3 »

Thanks, that fixed it!

m35
Posts: 171
Joined: Wed Feb 14, 2007 12:54 pm

Post by m35 »

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

Locked