Variable is getting lists in the loop.

Variable is getting lists in the loop.

Anonymous
Not applicable
1,201 Views
12 Replies
Message 1 of 13

Variable is getting lists in the loop.

Anonymous
Not applicable

Hello.

Can variable is getting lists individually in the loop that is "while" and "repeat" function?.

For example, If repeat function set 64, The variable that is (1 2) is getting list that is ((1 2) (2 3)) when the loop secondly repeat.

Therefore, 54s repeating variable list will be ((1 2) (2 3) (3 4) (4 5)................(53 54)).

 

I always thanks !.

0 Likes
Accepted solutions (1)
1,202 Views
12 Replies
Replies (12)
Message 2 of 13

patrick_35
Collaborator
Collaborator

Hi

 

if I understand the question
(apply '+ (mapcar 'length '((1 2) (2 3) (3 4) (4 5) (5 6))))
or
(length '((1 2) (2 3) (3 4) (4 5) (5 6)))
@+
0 Likes
Message 3 of 13

Ranjit_Singh
Advisor
Advisor

Based on what I understand from Therefore, 54s repeating variable list will be ((1 2) (2 3) (3 4) (4 5)................(53 54)) seems like a function is needed which could take an argument, say 54, and return the list upto 54 in pairs (12)(23)(34) etc.... If so try the below function. Call the function by passing argument 54 like (somefunc 54)

 

(defun somefunc (x) (somefunc2 x ()))
(defun somefunc2 (x y) (cond ((or (not (numberp x)) (minusp x) (null x) (= x 0)) (cdr y)) (T (somefunc2 (- x 1) (append (list (list (- x 1) x)) y)))))

 

calling (somefunc 54) gives

 

((1 2) (2 3) (3 4) (4 5) (5 6) (6 7) (7 8) (8 9) (9 10) (10 11) (11 12) (12 13) (13 14) (14 15) (15 16) (16 17) (17 18) (18 19) (19 20) (20 21) (21 22) (22 23) (23 24) (24 25) (25 26) (26 27) (27 28) (28 29) (29 30) (30 31) (31 32) (32 33) (33 34) (34 35) (35 36) (36 37) (37 38) (38 39) (39 40) (40 41) (41 42) (42 43) (43 44) (44 45) (45 46) (46 47) (47 48) (48 49) (49 50) (50 51) (51 52) (52 53) (53 54))
0 Likes
Message 4 of 13

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

Hello.

Can variable is getting lists individually in the loop that is "while" and "repeat" function?.

For example, If repeat function set 64, The variable that is (1 2) is getting list that is ((1 2) (2 3)) when the loop secondly repeat.

Therefore, 54s repeating variable list will be ((1 2) (2 3) (3 4) (4 5)................(53 54)).

 

I always thanks !.


Here is one that gives a result in which the second number of the last pair is the number you give it as an argument:

(defun listpairs (n)
  (repeat (1- n)
    (setq pairslist (cons (reverse (list n (setq n (1- n)))) pairslist))
  )
)

USAGE:

 

 

(listpairs 13)

returns

((1 2) (2 3) (3 4) (4 5) (5 6) (6 7) (7 8) (8 9) (9 10) (10 11) (11 12) (12 13))

and saves that into the 'pairslist' variable.

 

HOWEVER, that may not be what you really want.  It makes a list ending with the number given as the second number in the last pair, but that's only [in this case] 12 sublists, not 13.  In the beginning of your description, the number of repeats is the first number in the last pair [on the 2nd repeat, the last pair starts with a 2 -- it doesn't end with a 2], so that with 54 repeats, the last pair should be (54 55) rather than (53 54).  If that's really what you want, this will do it:

 

(defun listpairs (n)
  (repeat n
    (setq pairslist (cons (reverse (list (1+ n) (1+ (setq n (1- n))))) pairslist))
  )
)

with which:

 

 

(listpairs 10)

returns
((1 2) (2 3) (3 4) (4 5) (5 6) (6 7) (7 8) (8 9) (9 10) (10 11))

 

[It could be a little shorter with the introduction of an additional variable, but I decided to try it without any variables, using only than the 'n' argument.]

 

Set the 'pairslist' variable to nil before every try, so that it doesn't keep adding more to previous lists, and if needed, build that in or make it a localized variable.

Kent Cooper, AIA
0 Likes
Message 5 of 13

john.uhden
Mentor
Mentor
Um, er, I believe that pairslist is a variable as you allude in your last paragraph.

John F. Uhden

0 Likes
Message 6 of 13

Kent1Cooper
Consultant
Consultant

@john.uhden wrote:
Um, er, I believe that pairslist is a variable as you allude in your last paragraph.

Yes, but in those functions as written it is not a localized one.  I left it un-localized so that it would "survive" past the end of the function, and could be used for something afterwards.  If it is localized:

 

(defun listpairs (n / pairslist)

 

then you would not need to set it to nil before another run of the function, but you would also not be able to call for it afterwards to do anything with it or some part(s) of it, because it would no longer exist past the end of the function.  I have no idea how the OP intends to use the results, or whether such a function would be embedded into something larger, so they would need to decide how to handle it.

Kent Cooper, AIA
Message 7 of 13

john.uhden
Mentor
Mentor
The eloquent answer I was expecting from you. Kudos!

John F. Uhden

Message 8 of 13

Anonymous
Not applicable

Thanks, Kent cooper !.

I have a question for you.

When I set n to 12, The result return value will be ((1 2) (2 3)....).

I am still trying to make that return value will be ((1 110) (2 112) (3 114)...)) , But I do not still fail.

Anyway, All i want to do is that first elements will be n and second elements will be setting that i want.

 

 

0 Likes
Message 9 of 13

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

....

I am still trying to make that return value will be ((1 110) (2 112) (3 114)...)) , But I do not still fail.

Anyway, All i want to do is that first elements will be n and second elements will be setting that i want. 


Am I correct that you would want a routine to ask the User for the first number to be paired with 1 [in your example, 110], and the amount to adjust the value of the second number in each pair [in your example, increase by 2 every time]?

Kent Cooper, AIA
0 Likes
Message 10 of 13

Ranjit_Singh
Advisor
Advisor

Here's one way to do it [x - number of pairs needed in list, y - start number for first pair, z - increment/decrement amount]

 

(defun somefunc (x y z) (if (and (numberp x) (numberp y) (numberp z) (not (minusp x)) (not (zerop x))) (somefunc2 x y z ())))
(defun somefunc2 (w x y z) (cond ((= w 0) z) (T (somefunc2 (1- w) x y (append (list (list w (+ x (* y (1- w))))) z)))))

 

some example runs

 

Command: (somefunc 5 110 2)
((1 110) (2 112) (3 114) (4 116) (5 118))
Command: (somefunc 5 110 -2)
((1 110) (2 108) (3 106) (4 104) (5 102))
Command: (somefunc 5 -110 2)
((1 -110) (2 -108) (3 -106) (4 -104) (5 -102))
Command: (somefunc 5 110 3)
((1 110) (2 113) (3 116) (4 119) (5 122))
0 Likes
Message 11 of 13

Kent1Cooper
Consultant
Consultant

@Kent1Cooper wrote:
.... Am I correct that you would want a routine to ask the User for the first number to be paired with 1 [in your example, 110], and the amount to adjust the value of the second number in each pair [in your example, increase by 2 every time]?

If so, this is an adjustment of the second routine in Post 4, with arguments for those options:

(defun listpairs (n start adj)
;;  (setq pairslist nil)
;; take out initial semicolons in line above to run repeatedly and start anew each time
(repeat n (setq pairslist (cons (reverse (list (+ start (* adj (setq n (1- n)))) (1+ n))) pairslist)) ) )
The 'start' and 'adj' arguments can be negative, and can even be non-integer values.

(listpairs 6 110 2)
((1 110) (2 112) (3 114) (4 116) (5 118) (6 120))

 

(listpairs 6 110 -2)
((1 110) (2 108) (3 106) (4 104) (5 102) (6 100))

 

(listpairs 4 3 -2)
((1 3) (2 1) (3 -1) (4 -3))

 

(listpairs 8 0 0.1)
((1 0.0) (2 0.1) (3 0.2) (4 0.3) (5 0.4) (6 0.5) (7 0.6) (8 0.7))

 

(listpairs 4 -1.25 -0.75)
((1 -1.25) (2 -2.0) (3 -2.75) (4 -3.5))

 

It could also be done in a way that would have the User start the routine [without arguments -- just a command name], and it would then ask them for the values.  It could also be adjusted to start the first numbers of the pairs with a value other than 1.

Kent Cooper, AIA
0 Likes
Message 12 of 13

Anonymous
Not applicable

Thanks very much cooper!.

But I have last a question.

The lisp that you make of result will be ((1 110) (2 112)...).

How to insert dot into lists such as ((1  . 110)(2  . 112)( 3 . 114)).

Because I should use ssget function with your the lisp

0 Likes
Message 13 of 13

Kent1Cooper
Consultant
Consultant
Accepted solution

@Anonymous wrote:

....

How to insert dot into lists such as ((1  . 110)(2  . 112)( 3 . 114)).

....


Here's one way:

 

(defun listpairs (n start adj)
;;  (setq pairslist nil)
;; take out initial semicolons in line above to run repeatedly and start anew each time
  (repeat n
    (setq pairslist (cons (apply 'cons (reverse (list (+ start (* adj (setq n (1- n)))) (1+ n)))) pairslist))
  )
)

 

 

(listpairs 7 22 33)

returns
((1 . 22) (2 . 55) (3 . 88) (4 . 121) (5 . 154) (6 . 187) (7 . 220))

Kent Cooper, AIA
0 Likes