[LISP] Replace an item

[LISP] Replace an item

iBlachu
Advocate Advocate
1,133 Views
10 Replies
Message 1 of 11

[LISP] Replace an item

iBlachu
Advocate
Advocate

Hi,

 

How to replace 1 with 7 in the list below with sublists?

 

(setq A
(list
(list 0 0 0)
(list 0 1 0)
(list 0 0 0)
)
)

 

 

 

0 Likes
Accepted solutions (2)
1,134 Views
10 Replies
Replies (10)
Message 2 of 11

hak_vz
Advisor
Advisor

Is this a matrix representation or just a sample?

 

Here you have some helper functions

 

(defun plusp (num) (cond ((numberp num) (>= num 0.0))))

;Sets n-th element of a list to new value
;(set_nth '(1 2 3 4) 2 5) -> (1 2 5 4)
(defun set_nth (lst n value)
  (cond 
    ((and (plusp n) (<= n (length lst)))
      (cond
       ((zerop n)
         (cons value (cdr lst))
        )
        (t (cons (car lst) (set_nth  (cdr lst) (1- n) value)))
      )
    )
  )
)
(defun set_m_n (mat m n val / row el)
  (setq 
    row (nth m mat)
    row (set_nth row n val)
    mat (set_nth mat m row)
  )
)

 

 

 

Command: (setq A '((0 0 0)(0 1 0)(0 0 0)))
((0 0 0) (0 1 0) (0 0 0))
Command: (setq A (set_m_n A 1 1 7))
((0 0 0) (0 7 0) (0 0 0))

 

Elements are counted as (row, column) starting from 0. You can  change only existing rows and columns.

 

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 3 of 11

ВeekeeCZ
Consultant
Consultant

How would identify that it's 1 to be replaced? By its position? Or by its value? Or both?

0 Likes
Message 4 of 11

iBlachu
Advocate
Advocate

I wanted to simplify the example. It is not a matrix, it can be any list with different elements, even text elements.

 

I need to replace eg 2 element in 8 sublist with eg "NEW".

0 Likes
Message 5 of 11

hak_vz
Advisor
Advisor

You can use this code to make changes to i-j element of a list with what ever data type

 

Command: (setq a (set_m_n A 1 1 (list 2222 3333)))
((0 0 0) (0 (2222 3333) 0) (0 0 0))
Command: (setq a (set_m_n A 1 1 "Some text"))
((0 0 0) (0 "Some text" 0) (0 0 0))

If you need to change whole i-th sublist you can use function set_nth

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 6 of 11

ВeekeeCZ
Consultant
Consultant

@iBlachu wrote:

I need to replace eg 2 element in 8 sublist with eg "NEW".


What do you mean by that.... like changing 2nd item in 8th line of list?

Make an example...

 

0 Likes
Message 7 of 11

iBlachu
Advocate
Advocate

(setq A
(list
(list 0 0 0);0
(list 0 0 0);1
(list 0 0 0);2
(list 0 0 0);3
(list 0 0 0);4
(list 0 0 0);5
(list 0 0 0);6
(list 0 0 0);7
(list 0 0 0);8
(list 0 0 0);9
(list 0 0 0);10
)
)

 

(setq A
(list
(list 0 0 0);0
(list 0 0 0);1
(list 0 0 0);2
(list 0 0 0);3
(list 0 0 0);4
(list 0 0 0);5
(list 0 0 0);6
(list 0 0 0);7
(list 0 0 "NEW");8
(list 0 0 0);9
(list 0 0 0);10
)
)

0 Likes
Message 8 of 11

hak_vz
Advisor
Advisor
Accepted solution

(setq a (set_m_n A 8 2 "NEW")) 

Is this so complicated?

(set_m_n A row col val)

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 9 of 11

hak_vz
Advisor
Advisor

Here is my code used for matrix calculus, and you can find there how used this two functions for different algorithms

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 10 of 11

calderg1000
Mentor
Mentor
Accepted solution

Regards @iBlachu 

Here you have two examples to do it

(setq A
(list
(list 0 0 0)
(list 0 1 0)
(list 0 0 0)
)
)
;First form
(setq a(subst '(0 7 0) (cadr a) a))
;Second way
(subst (subst 7 (nth 1 (cadr a)) (cadr a)) (nth 1 a) a)

 

 


Carlos Calderon G
EESignature
>Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

Message 11 of 11

ВeekeeCZ
Consultant
Consultant

One more func - with unlimited list depth. 

;; list - list of positions from top to sub, 0 idx as first '(0 1 1) to get '((0 (0 "target"))) - new item
(defun lth-rep (lst lev new)
  (mapcar '(lambda (itm) (if (= (car lev) (vl-position itm lst))
			   (if (and (cdr lev) (listp itm))
			     (lth-rep itm (cdr lev) new)
			     new)
			   itm))
	  lst))

Example for your case: (lth-rep A '(7 2) "new")

 

 

 

Or if you prefer 1 based indexing:

;; list - list of positions from top to sub, 1 idx as first '(1 2 2) to get '((0 (0 "target"))) - new item
(defun lth-rep1 (lst lev new)
  (mapcar '(lambda (itm) (if (= (car lev) (1+ (vl-position itm lst)))
			   (if (and (cdr lev) (listp itm))
			     (lth-rep1 itm (cdr lev) new)
			     new)
			   itm))
	  lst))

(lth-rep1 A '(8 3) "new")