match and list patterns

Started by cormullion, July 26, 2006, 03:20:10 PM

Previous topic - Next topic

cormullion

Another puzzler. How do you insert patterns into match?


(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

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

Lutz

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



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


Lutz

cormullion

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

cormullion

#4
And what about putting a list:


(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

#5
Do you mean this?


> (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

#6

(letex (p 2 q 3) (match '(* p q *) numbers))


or the method DMI suggested:



(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

#7
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

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


(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