newLISP Fan Club

Forum => Anything else we might add? => Topic started by: adamss3 on March 23, 2007, 10:55:50 AM

Title: Help with match
Post by: adamss3 on March 23, 2007, 10:55:50 AM
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)
Title:
Post by: m35 on March 23, 2007, 11:16:14 AM
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.
Title:
Post by: cormullion on March 23, 2007, 11:16:18 AM
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... ;-)
Title:
Post by: adamss3 on March 23, 2007, 12:12:24 PM
Thanks, that fixed it!
Title:
Post by: m35 on March 27, 2007, 06:24:56 PM
Ah that's right, expand. I wasn't familiar with that one. Could be a lot cleaner looking than all those '*