newLISP Fan Club

Forum => Anything else we might add? => Topic started by: cormullion on July 26, 2006, 03:20:10 PM

Title: match and list patterns
Post by: cormullion on July 26, 2006, 03:20:10 PM
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?
Title:
Post by: Dmi on July 26, 2006, 04:22:16 PM
> (set 'matches
>            (match (list '* n '*) numbers))
((1 2 3 4) (6 7))
Title:
Post by: Lutz on July 26, 2006, 06:53:21 PM
... and here is a second way to do it:



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


Lutz
Title:
Post by: cormullion on July 27, 2006, 12:58:03 AM
Cool, thanks! In the cold light of the morning it looks more sensible.. ;-)
Title:
Post by: cormullion on July 27, 2006, 03:24:33 AM
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...?
Title:
Post by: HPW on July 27, 2006, 05:18:32 AM
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))
Title:
Post by: Lutz on July 27, 2006, 05:23:29 AM

(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
Title:
Post by: cormullion on July 27, 2006, 06:00:20 AM
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!
Title:
Post by: Lutz on July 27, 2006, 08:52:53 AM
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