# TheSwamp

## Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Jeroen on April 15, 2019, 07:31:17 AM

Title: Combine lists for desk layout and placing blocks
Post by: Jeroen on April 15, 2019, 07:31:17 AM
I have a Lisp that creates two lists throug user-input. These lists are measurements of  two different parts.
The lists can change in length. For example (top one in screenshot):

A (2 1 2) -> white-rectangles with "100+" in it
B (3 2) -> red dashed with value)

What I need is a list with points to put in 2 different blocks. (see screenshot, block A and block B)

The start and end-block I got covered.

List A only contains values of 1or 2
List B contains values of 1, 2, 3or 4

It should work in every combination where the total of the sum of the values in the lists are equal. (two more examples in screenshot)

I have been breaking my mind for hours and I cannot figure it out...

Kind regards,
Jeroen Berkers
Title: Re: Combine lists for desk layout and placing blocks
Post by: roy_043 on April 15, 2019, 08:35:07 AM
Your problem is hard to understand.
If both list A and B are based on user input what should the Lisp in fact do?
If only list B is given and you accept a list A with only 1's (as per your image) the algorithm is rather simple.

Code: [Select]
`(defun B_To_A (lstB)  (apply    'append    (mapcar      '(lambda (itm / ret) (repeat itm (setq ret (cons 1 ret))))      lstB    )  ))`
Title: Re: Combine lists for desk layout and placing blocks
Post by: ribarm on April 15, 2019, 09:16:11 AM
Here you are :

Code - Auto/Visual Lisp: [Select]
1. (defun c:const-st-a-b-en-lst ( / lst1 lst2 lst3 l l1 l2 lst )
2.   (initget 1)
3.   (setq lst1 (read (strcat "(" (getstring t "\nSpecify first list with numbers - integers : ") ")")))
4.   (initget 1)
5.   (setq lst2 (read (strcat "(" (getstring t "\nSpecify second list with numbers - integers : ") ")")))
6.   (if (and lst1 lst2)
7.       (repeat (apply '+ lst1)
8.         (setq lst3 (cons 1 lst3))
9.       )
10.       (setq lst (cons "st" lst))
11.       (setq l 0 l1 0 l2 0)
12.       (foreach x lst3
13.         (setq l (+ l x))
14.         (if (= l (apply '+ lst3))
15.           (setq lst (append lst (list "en")))
16.           (if (= l (+ (car lst1) l1))
17.             (if (= l (+ (car lst2) l2))
18.                 (setq lst (append lst (list "b")))
19.                 (setq l2 (+ l2 (car lst2)))
20.                 (setq l1 (+ l1 (car lst1)))
21.                 (setq lst2 (cdr lst2))
22.                 (setq lst1 (cdr lst1))
23.               )
24.                 (setq lst (append lst (list "a")))
25.                 (setq l1 (+ l1 (car lst1)))
26.                 (setq lst1 (cdr lst1))
27.               )
28.             )
29.           )
30.         )
31.       )
32.     )
33.   )
34.   (princ lst)
35.   (princ)
36. )
37.
38. ;|
39. Command: CONST-ST-A-B-EN-LST
40. Specify first list with numbers - integers : 2 1 2
41. Specify second list with numbers - integers : 3 2
42. (st a b en)
43. Command:
44. Command: CONST-ST-A-B-EN-LST
45. Specify first list with numbers - integers : 1 1 1 1 1 1
46. Specify second list with numbers - integers : 2 2 2
47. (st a b a b a en)
48. Command:
49. Command: CONST-ST-A-B-EN-LST
50. Specify first list with numbers - integers : 1 1 1 1 1
51. Specify second list with numbers - integers : 2 3
52. (st a b a a en)
53. |;
54.

HTH., M.R.
Title: Re: Combine lists for desk layout and placing blocks
Post by: Jeroen on April 15, 2019, 09:35:53 AM
Thanks Marko, it does exactly what I was looking for.

Jeroen