Group Admins

  • Profile picture of Karim
  • Profile picture of Jean

OpenMusic

Public Group active 41 minutes ago

User group for OpenMusic and computer-aided composition. Visit the Forum for discussions.

loop question

Author 3 Subscribed Users |
Profile photo of Frank GRATKOWSKI
Frank GRATKOWSKI

Hello

I try to create a function that substitutes more than one item at a time.

it should work like this
(list-substitute ‘(1 2 3 4 5) ‘((1 8) (2 29))) => (8 29 3 4 5)

My attempt is:

(defun list-substitute (l subs)

(loop for i in subl
for x = (substitute (first subl) (second subs) l) then (substitute (first subl) (second subs) x)
return x ))

Unfortunately list in the first inlet doesn’t get modified.

What am I doing wrong?

Thanks

Frank

March 4, 2018 at 20:13 #25723
Profile photo of fdsdb
fdsdb

Hi,

Perhaps this discussion (and resolution) could be useful for you:

http://forumnet.ircam.fr/user-groups/openmusic/forum/topic/map-lists/

Best

fdsdb

March 5, 2018 at 10:00 #25724
Profile photo of Frank GRATKOWSKI
Frank GRATKOWSKI

Thanks for the reply. Unfortunately it doesn’t solve my problem.

I would like to get the specific function I’m trying to program to work but it seems to be something wrong.
Using a recursion instead of the loop would work to but my skills are a bit to limited for that.

March 5, 2018 at 18:22 #25738
Profile photo of Frank GRATKOWSKI
Frank GRATKOWSKI

Just in case, I did some mistakes in typing my function. This is the correct one.

(defun list-substitute (l subl)
(loop for i in subl
for x = (substitute (first subl) (second subl) l) then (substitute (first subl) (second subl) x)
return x ))

I would still appreciate any help why it is not working. Also if there is a recursive solution instead…

Thanks Frank

March 5, 2018 at 19:15 #25739
Profile photo of Karim
Karim

Dear Frank,

Here is maybe what you are looking for :

(defun lst-subst (liste subst)
(let ((clone (clone liste)))
(loop for i in subst
do (nsubstitute (second i) (car i) clone))
clone))

(lst-subst ‘(1 2 3 4 5) ‘((1 8) (2 29)))

OM > (8 29 3 4 5)

The thing is to use nsubstitute instead of substitue, since the first function is a destructive one. That’s why i cloned the input list

Hope this helps

Best
K

March 5, 2018 at 19:56 #25740
Profile photo of Frank GRATKOWSKI
Frank GRATKOWSKI

Dear Karim,
thanks a lot. It’s exactly what I was looking for.
best
frank

March 6, 2018 at 12:03 #25750

You must be logged in to reply to this topic.

Log in now