Lisp programming doubts

Lisp programming doubts

Anonymous
Not applicable
768 Views
4 Replies
Message 1 of 5

Lisp programming doubts

Anonymous
Not applicable

Dear all

 

Good day 🙂

 

Am new to lisp programming and i need some help from you.

So i will go straight into my query..

 

for example

 

(setq a ( 12 20 23 35))

 returns the value (12 20 23 35)

(subst 50 12 a)

returns the value (50 20 23 35)

 

my doubt is to select the entire list and  what is the function used for it?

 

for example 

 

(subst 1 all a)

 

should return the value (1) for which it was previously (12 20 23 35)

 

Thanks in advance 

 

Regards,

DMC

0 Likes
769 Views
4 Replies
Replies (4)
Message 2 of 5

Anonymous
Not applicable

You can use mapcar or foreach for using some functions on all of the elements.

 

 

But in your example

 

"(subst 1 all a)

 

should return the value (1) for which it was previously (12 20 23 35)"

 

if you want only "1" in the list you can do a new list have only "1"

 

(setq a (list 1))

 

 

 

 

 

0 Likes
Message 3 of 5

Kent1Cooper
Consultant
Consultant

@di****hmc wrote:

.... 

my doubt is to select the entire list and  what is the function used for it?

 

for example 

 

(subst 1 all a)

 

should return the value (1) for which it was previously (12 20 23 35)

 

....

[By the way, the word you want is "question" rather than "doubt" -- they have related, but different, meanings.]

 

As @Anonymous suggested, the way to do that particular thing is not to select  the entire list somehow, but just to replace  the list that is stored in the 'a' variable with a different  new list.

 

There may be other situations in which you want to work in other ways with entire lists, for which you could [depending on what you need to do] make use of certain other functions -- (foreach), (mapcar), and a variety of others.  There's a whole page of list manipulation functions in Help [that link is for Acad2016, but I expect hardly anything will be any different in other versions].

Kent Cooper, AIA
0 Likes
Message 4 of 5

dbroad
Mentor
Mentor

One method:

(setq lst '(12 20 23 35))
(setq lst (mapcar '(lambda (x) 1) lst))
;returns 
(1 1 1 1) 

 

Architect, Registered NC, VA, SC, & GA.
0 Likes
Message 5 of 5

john.uhden
Mentor
Mentor

I like @dbroad's example.

 

Presuming...

Command:  (setq a '(12 20 23 35))

 

These are cute too...

Comand:  (mapcar '1+  a) (13 21 24 36)

  or

Command: (mapcar '1-  a) (11 19 22 34)

  or

Command: (apply '+ a) 90

 or

Command: (mapcar 'sqrt a) (3.4641 4.47214 4.79583 5.91608)

 

But one of my favorites is converting a list of points into a flat list

  for feeding coordinates to a polyline vla-object...

Command: (while (setq b (getpoint " Point: "))(setq c (cons b c)))
Point: Point: Point: Point: Point: Point:
((9.38056 5.58757 0.0) (8.70207 6.37566 0.0) (7.72378 5.83975 0.0) (6.93483
6.80123 0.0) (5.9092 5.91856 0.0))

 

 

Command: (apply 'append c)
(9.38056 5.58757 0.0 8.70207 6.37566 0.0 7.72378 5.83975 0.0 6.93483 6.80123
0.0 5.9092 5.91856 0.0)

John F. Uhden

0 Likes