< Back to IRCAM Forum

Variables in OM

Hi all,

I’m a relative newcomer to OM, please forgive me if this info is well documented somewhere. My question involves the treatment of variables in OM. I am working on a patch to generate pitch material, and want to feed it a start pitch, then the following type of list:

((direction interval) direction direction (direction new-interval) direction direction etc)

I’m easily able to make it work using omloop if I retype the interval every time, like this:

(direction interval) (direction interval-again) etc.)

It just seems like I should just be able to store “interval” as a variable, access it when not-a-list comes through, and update it when a list comes through.

Like this:

(setf intervals '((1 400) -1 1 (1 200) 1 -1))

(defun interval-list (full-list)
(let ((result nil))
(dolist (e full-list (reverse result))
(push (if (listp e)
(* (first e)
(setf multiplier (second e)))
(* e multiplier))
result))))

  • or -

(defun interval-list (element)
(if (listp element)
(* (first element)
(setf multiplier (second element)))
(* element multiplier)))

(mapcar #'interval-list intervals)

I realize I could just use this lisp code in OM, but I suspect it would be helpful in general to know how to store a variable. Or perhaps I’m thinking of this in the wrong way?

I’m happy to be pointed to documentation where this is addressed, just can’t find any that applies. Thanks for any help!
Per

Hello Per, dealing with variables/memory in pure functional contexts is not straightforward (there is no such thing as push or setf in OM functional graphs). However, there is a few tricks to do it.
The STORE object is a simple means to just store and retrieve a value: you can use it in a patch (eventually, with a “slots” accessor box) as a variable that you can read and write.

Seems you’re well into lisp coding. But your example, or anything similar where the structure of data changes or gets modified between steps of iteration, can well be solved using recursion, and stay purely functional.

Here are two patches which uses your list of intervals and yields the output you want: interval-list-rec (which calls itself recursively), and interval-list, which calls the recursive function with the wanted input list.

interval-list-rec.omp (5.75 KB)

Anders’ solution is better :slight_smile:
Here is what you could do with an OMLoop + STORE

Capture-d’écran-2018-12-20-à-11.06.31.png

Jean - thanks so much for this guide - it explains the STORE/SLOTS usage beautifully! That was exactly what I was hoping to figure out.

Anders - Thanks for yours as well! I haven’t had a chance to tackle it yet, recursion always requires a few more cycles for me to come to terms with. I love the elegance though, looking forward to digging in.