< Back to IRCAM Forum

Beginners question: remove a sequence from a list

This is probably easy, but I don’t know how to do it:
I’d like to filter out one particular sequence from a list.
Like “remove” - but not for a single element but for several elements, and only if they come in that combination.
For instance to filter out (a a a) from (1 a a a 2 a a 1) so that it returns (1 2 a a 1).

Is there a simple function for this?

Thank you and all best,
Soren

Hi Soren,

From what I remember, there’s no native function to do so (I may be wrong).
But here’s a function that works :

(defun remove-pattern (l pattern)
(let ((res l)
(pos (search pattern l)))
(loop while pos
do
(setq res (append (subseq res 0 pos)
(subseq res (+ pos (length pattern))))
pos (search pattern res)))
res))

Best,

Dimitri

Thank you very much, Dimitri.
This is great.

I have managed to use this by copying it into the lisp editor, then saving it as remove-pattern.lisp on the desktop, then using the “list>load file” command. I can then type it into a patch and the function is there and works.
When I close and restart OM the function is gone, though. (Death skull) and I have to reload it and revive it. Can I add this to my usr-library, so that it stays? I know that this must be very basic, but I’ve tried various ways of dragging and copying, but I could find out how.

Thank you for your help!

I found it here on the forums:
http://forumnet.ircam.fr/user-groups/openmusic/forum/topic/writing-an-om-library/

Thank you, Dimitri.

I gave you raw lisp code to define a function, that is why you were confused and found that you need to define your own library.
However, you could just use the function body in a lambda function in a patch.
To do so, type “lisp” in a patch.
It creates an object called “lispfunction”.
Note that if you type “lisp remove-pattern” instead, the object will be called “remove-pattern”.
Then, opening this object will open a lisp editor with some basic code.
The parenthesis right after the word “lambda” must be a list of arguments (here : (l pattern)).
Then replace the (om-beep) with the body of the function I gave you.

See attached picture :slight_smile:

Best,

Dimitri

Capture-d’écran-2016-05-27-à-15.08.41.png

Thank you you, Dimitri. That is super helpful.
I created a lisp function like you suggested and then used that as a method for a new generic function. Works perfectly.
Thank you for your help!
All best,
Soren