match and list patterns

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

match and list patterns

Post by cormullion »

Another puzzler. How do you insert patterns into match?

Code: Select all

(set 'numbers '(1 2 3 4 5 6 7))
(set 'matches 
		(match '(* 5 * ) numbers))
(println matches)
;-> ((1 2 3 4) (6 7))

(set 'n 5)
(set 'matches 
		(match '(* n * ) numbers))
(println matches)
;-> nil
How do I create a list pattern?

Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

Post by Dmi »

Code: Select all

> (set 'matches
>            (match (list '* n '*) numbers)) 
((1 2 3 4) (6 7))
WBR, Dmi

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

... and here is a second way to do it:

Code: Select all

> (letex (n 5) (match '(* n * ) numbers))
((1 2 3 4) (6 7))
> 
Lutz

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

Post by cormullion »

Cool, thanks! In the cold light of the morning it looks more sensible.. ;-)

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

Post by cormullion »

And what about putting a list:

Code: Select all

(set 'numbers '(1 2 3 4 5 6 7)) 
(set 'p '(2 3))
(set 'matches 
      (match (list '* p '*) numbers)) 
want this to return a match but it won't find it unless p is expanded somehow...?

HPW
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

Post by HPW »

Do you mean this?

Code: Select all

> (set 'numbers '(1 (2 3) 4 5 6 7))
(1 (2 3) 4 5 6 7)
> (set 'p '(2 3))
(2 3)
> (set 'matches (match (list '* p '*) numbers))
((1) (4 5 6 7))
Hans-Peter

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

Code: Select all

(letex (p 2 q 3) (match '(* p q *) numbers))
or the method DMI suggested:

Code: Select all

(set 'p 2 'q 3)
(match (list '* p q '*) numbers)
Each spec in the match pattern describes an element of the search list, which could be another list (as in HPWs example) but not a sublist. This is why you have to break up p into p,q.

Lutz

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

Post by cormullion »

Thanks again, guys! Do you ever have one of those days when your brain just doesn't want to understand something no matter how hard you try? I'm having one of those days today...

HPW's code works when p is a nested list and you want to find that nested list. Lutz' code works when you can specify the elements of list in advance. I want to specify the elements in advance but unnest them...

So all I need to do is to use *flat* to flatten the list stored in p... :-)

It took me too long to realise this. So it must be a time for a holiday!

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

yes, 'flat' seems to be the best way to go:

Code: Select all

(set 'p '(2 3))
(match (flat (list '* p '*)) numbers)

 ; or this

(letex (p '(2 3)) (match (flat '(* p *)) numbers))
the 'flat' solution is probably the best because it is independend from the number of members in 'p'. It will always work to find a sublist in another list, regardles of the length.

Lutz

Locked