add to lists inside list

add to lists inside list

DGRL
Advisor Advisor
3,729 Views
21 Replies
Message 1 of 22

add to lists inside list

DGRL
Advisor
Advisor

Dear forum members

 

 

I am looking for a way to add "X" to all lists inside a list

 

This is the list

(("ISO_PIPESPEC" "n.v.t.1" "Prompt 1") ("ISO_MEDIUM" "n.v.t.2" "Prompt 2") ("ISO_DESIGN_PRESS" "n.v.t.3" "Prompt 3"))

 

And I want to get it like this

(("ISO_PIPESPEC" "n.v.t.1" "Prompt 1" "X") ("ISO_MEDIUM" "n.v.t.2" "Prompt 2" "X") ("ISO_DESIGN_PRESS" "n.v.t.3" "Prompt 3" "X"))

 

 

Below is what I tried and didn't work out

 

Trying it like this

(foreach x vier (setq a (append a (car vier) '("X")))) will make 1 long list and I need multiple lists inside a list

 

trying this
(foreach x (car vier) (setq a (append a vier '("")))) will make the list how I want it only it placed the "X" 1 time as last list

 

Trying this

(foreach x vier (setq a (append a x '("")))) will make 1 long list and I need multiple lists inside a list 

Trying this will add the value to each member of the lists

(mapcar '(lambda ( x ) (strcat "insert text here" x)) 'vier)

 

 

 

 

 

vier is the var name I used that contains the list

 

 

 

If this was of any help please kudo and/or Accept as Solution
Kind Regards
0 Likes
Accepted solutions (4)
3,730 Views
21 Replies
Replies (21)
Message 2 of 22

ВeekeeCZ
Consultant
Consultant
Accepted solution

(mapcar

  '(lambda (x) (append x '("X")))

  '(("ISO_PIPESPEC" "n.v.t.1" "Prompt 1") ("ISO_MEDIUM" "n.v.t.2" "Prompt 2") ("ISO_DESIGN_PRESS" "n.v.t.3" "Prompt 3"))

  )

Message 3 of 22

_gile
Consultant
Consultant
Accepted solution

Hi,

 

(mapcar '(lambda (x) (append x '("X"))) vier)

but as cons is cheaper than append (depends on list length), you can also do:

 

(mapcar '(lambda (x) (reverse (cons "X" (reverse x)))) vier)


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 22

ActivistInvestor
Mentor
Mentor

@_gile wrote:

Hi,

 

(mapcar '(lambda (x) (append x '("X"))) vier)

but as cons is cheaper than append (depends on list length), you can also do:

 

(mapcar '(lambda (x) (reverse (cons "X" (reverse x)))) vier)

Hi @_gile.  Have you done any profiling of the above?

 

In this case, I very much doubt that cons + (reverse * 2) is cheaper than append, and the length of the list affects the cost of both reverse and append. 

 

What makes append 'expensive' is the fact that it creates and returns a new list.

 

But so does reverse.

 

0 Likes
Message 5 of 22

_gile
Consultant
Consultant

@ActivistInvestor wrote:

@_gile wrote:

Hi,

 

(mapcar '(lambda (x) (append x '("X"))) vier)

but as cons is cheaper than append (depends on list length), you can also do:

 

(mapcar '(lambda (x) (reverse (cons "X" (reverse x)))) vier)

Hi @_gile.  Have you done any profiling of the above?

 

In this case, I very much doubt that cons + reverse is cheaper than append, and the length of the list affects the cost of both reverse and append. 

 

 


No, I didn't and you're certainly right (one more time).

Let's say it's a learning example...



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 6 of 22

DGRL
Advisor
Advisor

HI @ActivistInvestor and @_gile

May I ask you what do you mean with cheaper and expensiver?

Im still learning a lot with coding so sorry for asking

 

 

If this was of any help please kudo and/or Accept as Solution
Kind Regards
0 Likes
Message 7 of 22

ActivistInvestor
Mentor
Mentor

Cheaper and expensive (or 'cost') relates to performance or how slow/fast an algorithm is.

 

In this case, the reverse function is actually more expensive than append because it has to do more work, and creates a new list that it returns.

 

So, its use is defeating the purpose of avoiding append.

 

Append is bad when it is mis-used to iteratively append elements to the end of a list, one-at-a time, with each new element requiring one call to append:

 

(foreach new-element list-of-new-elements
     (setq result (append result (list new-element)))
)

So most programmers who know that will instead use cons iteratively to construct the list in reverse, because cons is much 'cheaper' than append when it is used in this way (iteratively), and then use reverse to put the list in the correct order:

 

(foreach new-element list-of-new-elements
    (setq result (cons new-element result))
)

(setq result (reverse result))

But in your case, append isn't being used iteratively to construct a list, so it isn't any more expensive (and probably cheaper) than 2 calls to reverse.

Message 8 of 22

_gile
Consultant
Consultant

I'll try an explaination, but my English is not so good.

 

The memory cost of append function is greater than the cons one, because append completely re-build the lists and cons just link a new item to a list in memory

 

May be I can illustrate this with a little example using the difference between the eq and the equal function.

 

(setq l1 '(3 4 5))
(setq l2 '(3 4 5))
(setq l3 l2)

(equal l1 l2) and (equal l1 l3) return both T because l1, l2 and l3 have the same contents.

But (eq l1 l2) return nil because l1 and l2 refer to two different address in memory.

And (eq l1 l3) return T because both refer to the same address.

 

That said, we build 2 new lists using l1 and the cons and append functions.

 

(setq l4 (append '(1 2) l1))
(setq l5 (cons 1 (cons 2 l1)))

Both l4 and l5 contains (1 2 3 4 5) so (equal l4 l5) returns T.

 

Now, if we compare the cddr of l4 and l5 with l1

(equal l1 (cddr l4)) and (equal l1 (cddr l5)) obviously returns T.

But, (eq l1 (cddr l4)) returns nil and (eq l1 (cddr l5)) returns T because append completely rebuilt the list instead cons linked 1 and 2 to l1.

 

This is due to the fact a LISP list is a linked list and cons the fundamental function use to build lists.

(list 1 2 3) is a shortcut for (cons 1 (cons 2 (cons 3 nil)))



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 9 of 22

DGRL
Advisor
Advisor

Thanks gentleman for this explanation.

 

 

 

If this was of any help please kudo and/or Accept as Solution
Kind Regards
0 Likes
Message 10 of 22

DGRL
Advisor
Advisor

To make this complete

What do I need to do to remove the X from the list again

I am sure I can use that in the future

Just tried some things myself and I always end upwith same result

 

 

When trying this

(setq ab (mapcar 'car newlst))

it does not gives back the list without the X

 

And all I want is to remove the X

 

Besides the correct code I also want to know why I get back the wrong list
I cant find any using google about this.

 

 

 

 

 

If this was of any help please kudo and/or Accept as Solution
Kind Regards
0 Likes
Message 11 of 22

ВeekeeCZ
Consultant
Consultant

Think about that... one easy way is to use the CDR function. But there is one trick... up to you to find it out!

0 Likes
Message 12 of 22

DGRL
Advisor
Advisor

HI @ВeekeeCZ


All it does is removing the first item of the list
But now I type this there are some lights popping up

 

 

 

As you know I have multiple lists inside 1 list

this is 1 list of all the lists I have

("ISO_PIPESPEC" "n.v.t.1" "Prompt 1" "")

 

When trying (setq ab (mapcar 'cdr newlst)) it returns

("n.v.t.1" "Prompt 1" "")

When doing 'cddr and 'cdddr it will remove 1 more from the list
So I end up with a list like this
("Prompt 1" "") and ("") LOL

I tried reverse of the list but it reversed the whole list and not the individual listst
Anyway I will try to reverse the individual lists to get what I need lol
I can also try it with NTH

And the trick you mention is until now unknown for me but give me some time and a little push in the back
Its better to learn yourself then asking
And I am here to learn and to help not to ask and push my hand forwards to receive 🙂

 

If this was of any help please kudo and/or Accept as Solution
Kind Regards
0 Likes
Message 13 of 22

ВeekeeCZ
Consultant
Consultant
There is nothing to add -- all you need to know has been already said.
Message 14 of 22

DGRL
Advisor
Advisor

That gives me stuff to think about lol

If my answer is already here in this post I will find it 🙂

Thanks for all the afford 🙂
Really appreciate it you guys trying to learn me stuff

 

For now have a very nice evening and 1 more day to weekend

 

Best regards,

 

If this was of any help please kudo and/or Accept as Solution
Kind Regards
0 Likes
Message 15 of 22

DGRL
Advisor
Advisor

@ВeekeeCZ

 

 

Found it 😄

 

(mapcar

  '(lambda (x) (append x '("X")))

  '(("ISO_PIPESPEC" "n.v.t.1" "Prompt 1") ("ISO_MEDIUM" "n.v.t.2" "Prompt 2") ("ISO_DESIGN_PRESS" "n.v.t.3" "Prompt 3"))

  )

 

Simply replace the append with VL-REMOVE and done

 

If this was of any help please kudo and/or Accept as Solution
Kind Regards
Message 16 of 22

ВeekeeCZ
Consultant
Consultant

@DGRL wrote:

@ВeekeeCZ

 

 

Found it 😄

 

(mapcar

  '(lambda (x) (append x '("X")))

  '(("ISO_PIPESPEC" "n.v.t.1" "Prompt 1") ("ISO_MEDIUM" "n.v.t.2" "Prompt 2") ("ISO_DESIGN_PRESS" "n.v.t.3" "Prompt 3"))

  )

 

Simply replace the append with VL-REMOVE and done

 


Sure, that's a proper solution.

 

But in this case i would call that as cheating. Sorry, no vl-* functions allowed here. You wanna learn? Learn the basics.

0 Likes
Message 17 of 22

DGRL
Advisor
Advisor

LOL
it does not even give me the result I need

 

 

And where can I find some proper documentation?
Coz all I have is http://help.autodesk.com/view/OARX/2018/ENU/ and this is for me not enough to get the info I need to learn this proper

 

And I agree

no cheating allowed

If this was of any help please kudo and/or Accept as Solution
Kind Regards
0 Likes
Message 18 of 22

ВeekeeCZ
Consultant
Consultant
Accepted solution

@DGRL wrote:

LOL
it does not even give me the result I need

 

 

...

Just minor hiccups... see the help HERE

 

(mapcar 

  '(lambda (x) (vl-remove "X" x))

  '(("ISO_PIPESPEC" "n.v.t.1" "Prompt 1") ("ISO_MEDIUM" "n.v.t.2" "Prompt 2") ("ISO_DESIGN_PRESS" "n.v.t.3" "Prompt 3")))

0 Likes
Message 19 of 22

DGRL
Advisor
Advisor

Thanks for pointing that out

It is fine with me but i thought you considered vl-remove as cheating?

 

if there is a way to do it without vl-remove i will find it

Just need to practice more with the mapcar and lambda functions

 

🙂

If this was of any help please kudo and/or Accept as Solution
Kind Regards
Message 20 of 22

Kent1Cooper
Consultant
Consultant
Accepted solution

@DGRL wrote:

....
I tried reverse of the list but it reversed the whole list and not the individual listst
.... 


It would involve reversing inside the (lambda) function  that is going to be applied to each sub-list in the overall list, applying (cdr) to take the first thing off the reversed  sub-list, and re-reversing the result to get the original order back.  I could spell it out, but it sounds like you want to work out the details for yourself, which is preferable.

Kent Cooper, AIA