Author Topic: Referenced Sublist Nested Sum - AutoLisp Help  (Read 1529 times)

0 Members and 1 Guest are viewing this topic.

Matthew H

  • Newt
  • Posts: 70
Referenced Sublist Nested Sum - AutoLisp Help
« on: October 24, 2022, 12:21:30 PM »
I was wondering if someone could help step through the following data structure in LISP.

The dataset is comprised of the following.
A sublist containing the following ( Reference Sublist number, Number1, Number2 )
and a list made up of multiple sublists.

The output is a generated list of sublists where each ( Number1, Number2 ) is the sum of multiple referenced sublists.
If the sublist's reference number is 0, it does not reference a sublist for its addition/subtraction.

The issue I am having is how do I handle/tackle the multi-nested leveled nature of addition.

I have posted a sample below, which hopefully explains the situation/problem.
I greatly appreciate any help one can give.
Code - Auto/Visual Lisp: [Select]
  1. ;Data Structure
  2.         ;Initial List A
  3.         ;          (REFNUM, NUM1, NUM2)
  4.         ;sublistA  1 = (  0  10  10 )
  5.         ;sublistA  2 = (  1   5   2 )
  6.         ;sublistA  3 = (  2   8  12 )
  7.         ;sublistA  4 = (  1  17   1 )
  8.         ;sublistA  5 = (  1   9   6 )
  9.         ;sublistA  6 = ( 10   7   4 )
  10.         ;sublistA  7 = (  4   3  30 )
  11.         ;sublistA  8 = (  5  -4   9 )
  12.         ;sublistA  9 = (  3 -34 -80 )
  13.         ;sublistA 10 = (  7  18 -41 )
  14.         ;sublistA 11 = (  0  20 -17 )
  15.         ;sublistA 12 = ( 11 -25  15 )
  16.        
  17.         ;Generated List B (This was manually calculated, ideally it should be calculated programmatically)
  18.         ;   MANUAL RESULT VALUE   | Attempt to solve (NOT VERIFIED TO WORK)
  19.         ;             (NUM1 NUM2)
  20.         ;sublistB  1 = (  10  10 ) | (list (cadr sublistB1) (caddr sublistB1))
  21.         ;sublistB  2 = (  15  12 ) | (list (+ (cadr sublistB2) (cadr sublistA1)) (+ (caddr sublistB2) (caddr sublistA1)))
  22.         ;sublistB  3 = (  23  24 ) | (list (+ (cadr sublistB3) (cadr sublistA2)) (+ (caddr sublistB3) (caddr sublistA2)))
  23.         ;sublistB  4 = (  27  11 ) | (list (+ (cadr sublistB4) (cadr sublistA1)) (+ (caddr sublistB4) (caddr sublistA1)))
  24.         ;sublistB  5 = (  19  16 ) | (list (+ (cadr sublistB5) (cadr sublistA1)) (+ (caddr sublistB5) (caddr sublistA1)))
  25.         ;sublistB  6 = (  55   4 ) | (list (+ (cadr sublistB6) (cadr sublistA10)) (+ (caddr sublistB6) (caddr sublistA10)))
  26.         ;sublistB  7 = (  30  41 ) | (list (+ (cadr sublistB7) (cadr sublistA4)) (+ (caddr sublistB7) (caddr sublistA4)))
  27.         ;sublistB  8 = (  15  25 ) | (list (+ (cadr sublistB8) (cadr sublistA5)) (+ (caddr sublistB8) (caddr sublistA5)))
  28.         ;sublistB  9 = ( -11 -56 ) | (list (+ (cadr sublistB9) (cadr sublistA3)) (+ (caddr sublistB9) (caddr sublistA3)))
  29.         ;sublistB 10 = (  48   0 ) | (list (+ (cadr sublistB10) (cadr sublistA7)) (+ (caddr sublistB10) (caddr sublistA7)))
  30.         ;sublistB 11 = (  20 -17 ) | (list (cadr sublistB11) (caddr sublistB11))
  31.         ;sublistB 12 = (  -5  -2 ) | (list (+ (cadr sublistB11) (cadr sublistA11)) (+ (caddr sublistB11) (caddr sublistA11)))
  32.        
  33. ;Initial List A
  34. (setq listofsublistA (list (list nil 10 10) (list 1 5 2) (list 2 8 12) (list 1 17 1) (list 1 9 6) (list 10 7 4) (list 4 3 30) (list 5 -4 9) (list 3 -34 80) (list 7 18 -25) (list 0 20 -17) (list 11 -25 15)))
  35.  
  36. ;Generated List B (This was manually combined from above)
  37. (setq listofsublistB (list (list 10 10) (list 15 12) (list 23 24) (list 27 11) (list 19 16) (list 55 4) (list 30 41) (list 15 25) (list -11 -56) (list 48 0) (list 20 -17) (list -15 -2)))
« Last Edit: October 24, 2022, 02:52:26 PM by Matthew H »

kirby

  • Newt
  • Posts: 127
Re: Referenced Sublist Nested Sum - AutoLisp Help
« Reply #1 on: October 24, 2022, 02:18:22 PM »
It's difficult to do this without knowing the pattern needed to generate list of sub-lists B.

List B item 1 = List A item 1
List B item 2 = List A (item 1 + 2)
List B item 3 = List A (item 2 + 3)

; pattern changes here
List B item 4 = List A (item 4 + 1)
List B item 5 = List A (item 5 + 1)

; pattern changes again
List B item 6 = List A (item 6 + 2)

...

; is there a pattern after every 10 items in List A?
List B item 11 = List A item 11


If there is no pattern, then it's not possible to do anything other than hard code the solution for each fixed number of List A items (e.g. one version for List A with 12 sublists, another version for List A with 13 sublists, etc.).

Matthew H

  • Newt
  • Posts: 70
Re: Referenced Sublist Nested Sum - AutoLisp Help
« Reply #2 on: October 24, 2022, 02:34:52 PM »
Kirby,

It's difficult to do this without knowing the pattern needed to generate list of sub-lists B.

The pattern is as follows.
ListofsublistsA
                  (REFNUM NUM1 NUM2)
     SublistA1 ( 0 10 10 )
     SublistA2 ( 1   5  2  )
     SublistA3 ( 2  -8 -5 )

If the index is zero, return original number. if the reference number is a number greater than 0, add the corresponding sublist to the numbers.
SublistB1 (10 10)
SublistB2 (15 12) which is (5+10) and (2+10)
SublistB3 ( 7   7) which is (-8+15) and (-5+12)

The quantity of sublists inside of listofsublists is not set to any particular number.
The reference number in each sublist has a minimum number of 0 and a maximum number which is the total number of sublists inside of listofsublists.
The reference number can be repeated in multiple sublists in listsofsublists.
The reference number is technically an index number representing a sublist inside of listofsublists. (subtract 1 from the reference number, as lists start with 0 in LISP)
The addition/subtraction of sublists can be multi-nested as in my original post. It has a technical maximum nesting level of (quantity of sublists in listofsublists minus 1)
The Reference number of a sublist cannot refer to itself, unless it is 0.
There is no particular order for the reference number to appear. sublist2 could reference sublist10. (Sublist2 (10 -8 -4))
There is also the potential of all sublists inside of listofsublists to have a reference number of 0, requiring no addition/subtraction.

Thank you,
Matthew H.
« Last Edit: October 24, 2022, 03:39:57 PM by Matthew H »

kirby

  • Newt
  • Posts: 127
Re: Referenced Sublist Nested Sum - AutoLisp Help
« Reply #3 on: October 24, 2022, 04:52:06 PM »
Here is how I'd do it...

Includes a test routine with your test data hard coded, and two custom functions.
'ProcessList' (not the best name) processes List A into List B
'ShowList' generic function to display a simple list with one item per line, to help readability.

Also note that I converted your item referencing 1,2,3 to lisp-friendly 0,1,2 indices and used nil to represent the 0 / null case where we didn't want to add (1st and 10th items, or 0th and 9th in lisp-friendly numbering).

Code - Auto/Visual Lisp: [Select]
  1. (defun C:Test ( / MyList Ans CNT)
  2. ; Test routine for 'ProcessList'
  3. ; KJM - Oct 2022
  4. ; Uses custom functions:
  5. ;       showlist
  6. ;       processlist
  7.  
  8. ; Dummy list for testing.  Note ref number follows lisp index referencing.  item 1 = 0, item 2 = 1, etc
  9. (setq MyList (list
  10.         ' (nil  10   10 )
  11.         ' (0    5    2 )
  12.         ' (1    8   12 )
  13.         ' (0   17    1 )
  14.         ' (0    9    6 )
  15.         ' (9    7    4 )
  16.         ' (3    3   30 )
  17.         ' (6   -4    9 )
  18.         ' (2  -34  -80 )
  19.         ' (6   18  -41 )
  20.         ' (nil 20  -17 )
  21.         ' (10 -25   15 )
  22. ))
  23.  
  24. ; Show input
  25. (prompt "\nInput List:")
  26. (showlist MyList nil)
  27.  
  28. ; Process list using custom function
  29. (setq Ans (processlist MyList))
  30.  
  31. ; Report output
  32. (prompt "\nOutput List:")
  33. (showlist Ans nil)
  34. )
  35. (prompt "\n type TEST to run test routine!")(princ)
  36.  
  37.  
  38.  
  39.  
  40. (defun ShowList (MyList IndentStr /
  41.                 CNT
  42.                 )
  43. ; Show a list one line at a time
  44. ; KJM
  45. ; Input:
  46. ;       MyList - (list) a list
  47. ;       IndentStr - (string) indent string e.g. "   ".  Default = "   " (3 spaces)
  48. ; Returns:
  49. ;       nothing
  50. ;       displays list to textscr
  51.  
  52. (if (not IndentStr) (setq IndentStr "   "))
  53.  
  54. (setq CNT 0)
  55. (repeat (length MyList)
  56.         (prompt "\n")(princ IndentStr)(princ CNT)(prompt "\t")(princ (nth CNT MyList))(princ)
  57.         (setq CNT (1+ CNT))
  58. ) ; close repeat
  59. )
  60.  
  61.  
  62.  
  63. (defun ProcessList (MyList /
  64.                 OutList CNT1 MyItem1 CNT2 MyItem2 Total1 Total2
  65.                 )
  66. ; Process a list to sum two accumulator values
  67. ; KJM - Oct 2022
  68. ; Input:
  69. ;       MyList - (list of lists) input list with format (0-RefIndex 1-Num1 2-Num2)
  70. ;               where RefIndex is the list index to total
  71. ; Returns:
  72. ;       Processed sublist
  73.  
  74. (setq OutList nil)
  75.  
  76. (setq CNT1 0)
  77. (repeat (length MyList)
  78.        
  79.         (setq MyItem1 (nth CNT1 MyList))
  80.         (setq CNT2 (nth 0 MyItem1))             ; get reference number to use a counter
  81.  
  82.         (setq Total1 (nth 1 MyItem1))           ; initialize first value
  83.         (setq Total2 (nth 2 MyItem1))           ; initialize second value
  84.  
  85.         (if CNT2
  86.           (progn
  87.                 ; RefIndex was not nil, get referenced sublist
  88.                 (setq MyItem2 (nth CNT2 MyList))
  89.          
  90.                 ; Add referneced values
  91.                 (setq Total1 (+ Total1 (nth 1 MyItem2)))
  92.                 (setq Total2 (+ Total2 (nth 2 MyItem2)))
  93.           )    
  94.         ) ; close if
  95.        
  96.         (setq OutList (cons (list Total1 Total2) OutList))
  97.  
  98.         (setq CNT1 (1+ CNT1))
  99. ) ; close repeat
  100.  
  101. (setq OutList (reverse OutList))
  102. OutList
  103. )
  104.  


Results differ from yours a bit, maybe there is a nuance I missed.  I'll leave it with you to check.

Code - Auto/Visual Lisp: [Select]
  1. Command: TEST
  2.  
  3. Input List:
  4.    0     (nil 10 10)
  5.    1     (0 5 2)
  6.    2     (1 8 12)
  7.    3     (0 17 1)
  8.    4     (0 9 6)
  9.    5     (9 7 4)
  10.    6     (3 3 30)
  11.    7     (6 -4 9)
  12.    8     (2 -34 -80)
  13.    9     (6 18 -41)
  14.    10      (nil 20 -17)
  15.    11      (10 -25 15)
  16. Output List:
  17.    0     (10 10)
  18.    1     (15 12)
  19.    2     (13 14)
  20.    3     (27 11)
  21.    4     (19 16)
  22.    5     (25 -37)
  23.    6     (20 31)
  24.    7     (-1 39)
  25.    8     (-26 -68)
  26.    9     (21 -11)
  27.    10      (20 -17)
  28.    11      (-5 -2)
  29.  
« Last Edit: October 24, 2022, 04:55:07 PM by kirby »

Matthew H

  • Newt
  • Posts: 70
Re: Referenced Sublist Nested Sum - AutoLisp Help
« Reply #4 on: October 24, 2022, 07:08:41 PM »
Here is how I'd do it...
Results differ from yours a bit, maybe there is a nuance I missed.  I'll leave it with you to check.

Kirby,

Good evening!
I appreciate you attempting to solve this issue.

I do see where the results are different. That is because there is the following issues.
First your initial numbers differ from the original posting initial numbers. That is okay though as is it just a sample dataset.
Second, you are only solving at one single nesting level.

If you add a number from another indices, you also have to add that indices number. This will repeat until the subsequently added indices item is a nil value, indicating no more addition/subtraction is needed.
I have updated your input and output list to help clarify the above.
The problem I face is, how to iterate through the nesting levels. The maximum number of nested indices is (total number of sublists in input list minus 1) Example, 0 through 11 sublist. The maximum nesting level would be 10.

Code - Auto/Visual Lisp: [Select]
  1. Input List:
  2.    0  (nil 10 10)
  3.    1  (0 5 2)
  4.    2  (1 8 12)
  5.    3  (0 17 1)
  6.    4  (0 9 6)
  7.    5  (9 7 4)
  8.    6  (3 3 30)
  9.    7  (6 -4 9)
  10.    8  (2 -34 -80)
  11.    9  (6 18 -41)
  12.    10 (nil 20 -17)
  13.    11 (10 -25 15)
  14.  
  15. Output List Corrected:
  16.    0  ( 10  10) = (10) (10)
  17.    1  ( 15  12) = (5+10) (2+10)
  18.    2  ( 23  24) = (8+5+10) (12+2+10)
  19.    3  ( 27  11) = (17+10) (1+10)
  20.    4  ( 19  16) = (9+10) (6+10)
  21.    5  ( 55   4) = (7+18+3+17+10) (4+-41+30+1+10)
  22.    6  ( 30  41) = (3+17+10) (30+1+10)
  23.    7  ( 26  50) = (-4+3+17+10) (9+30+1+10)
  24.    8  (-11 -56) = (-34+8+5+10) (-80+12+2+10)
  25.    9  ( 48   0) = (18+3+17+10) (-41+30+1+10)
  26.   10  ( 20 -17) = (20) (-17)
  27.   11  ( -5  -2) = (-25+20) (15+-17)


kirby

  • Newt
  • Posts: 127
Re: Referenced Sublist Nested Sum - AutoLisp Help
« Reply #5 on: October 25, 2022, 08:40:00 AM »
Ahhh, missed that part.

Need to think about that one, either iterative or recursive solution with ideally minimal passes through the dataset.  Likely also a mapcar & lambda solution, but my FORTRAN-trained brain doesn't swing in that direction.

Tied up today, may not respond until this evening.

kirby

  • Newt
  • Posts: 127
Re: Referenced Sublist Nested Sum - AutoLisp Help
« Reply #6 on: October 25, 2022, 09:40:48 AM »
Updated version that uses recursion.  The test routine processes the list one item at a time.  Change Verbose=1 in the 'processlist' function for feedback during calculations, or Verbose=0 to supress.

The test routine is inefficient since it processes the list many times, likely a better way to do it if your list is large (and your brain is larger than mine).

Code - Auto/Visual Lisp: [Select]
  1. (defun C:Test ( / MyList OutList Ans CNT)
  2. ; Test routine for 'ProcessList'
  3. ; KJM - Oct 2022
  4. ; Uses custom functions:
  5. ;       showlist
  6. ;       processlist
  7.  
  8. ; Dummy list for testing.  Note ref number follows lisp index referencing.  item 1 = 0, item 2 = 1, etc
  9. (setq MyList (list
  10.         ' (nil  10   10 )
  11.         ' (0    5    2 )
  12.         ' (1    8   12 )
  13.         ' (0   17    1 )
  14.         ' (0    9    6 )
  15.         ' (9    7    4 )
  16.         ' (3    3   30 )
  17.         ' (6   -4    9 )
  18.         ' (2  -34  -80 )
  19.         ' (6   18  -41 )
  20.         ' (nil 20  -17 )
  21.         ' (10 -25   15 )
  22. ))
  23.  
  24. ; Show input
  25. (prompt "\nInput List:")
  26. (showlist MyList nil)
  27. (prompt "\n\n")
  28.  
  29. ; Process list one at a time using custom function
  30. (setq OutList nil)
  31.  
  32. (setq CNT 0)
  33. (repeat (length MyList)
  34.         (setq Ans (processlist MyList CNT 0))
  35.         (prompt "\n  CNT = ")(princ CNT)(prompt "  Result = ")(princ Ans)
  36.         (prompt "\n ")(princ)
  37.  
  38.         (setq OutList (cons Ans OutList))
  39.  
  40.         (setq CNT (1+ CNT))
  41. ) ; close repeat       
  42. (setq OutList (reverse OutList))
  43.  
  44. ; Show output
  45. (prompt "\nOutput List:")
  46. (showlist OutList nil)
  47. (prompt "\n\n")
  48.  
  49. )
  50.  
  51. (prompt "\n type TEST to run test routine!")(princ)
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58. (defun ProcessList (MyList Index NestDepth / Verbose OutList MyItem1 RefIndex Total1 Total2 Ans)
  59. ; Process a list to sum two accumulator values (recursive)
  60. ; KJM - Oct 2022
  61. ; Input:
  62. ;       MyList - (list of lists) input list with format (0-RefIndex 1-Num1 2-Num2)
  63. ;               where RefIndex is the list index to total
  64. ;       Index - (integer) index of item to process
  65. ;       NestDepth - (integer) nesting depth, not necessary and only used for feedback display
  66. ; Returns:
  67. ;       Processed sublist of two totals (0-Total1 1-Total2)
  68. ; Uses custom functions:
  69. ;       StringDupe - used for feedback display
  70. ;       ProcessList (recursive)
  71.  
  72.  
  73. (setq Verbose 1)        ; 0 = supress feedback, 1 = show feedback
  74.  
  75. (setq OutList nil)
  76.  
  77.  
  78. ; Get starting item
  79. (setq MyItem1 (nth Index MyList))
  80. (setq RefIndex (nth 0 MyItem1))         ; get reference number to use a counter
  81. (setq Total1 (nth 1 MyItem1))           ; initialize first value
  82. (setq Total2 (nth 2 MyItem1))           ; initialize second value
  83.  
  84. (if (eq Verbose 1)
  85.   (progn
  86.         (prompt "\n  ")(princ (stringdupe " " NestDepth))(prompt "Item = ")(princ MyItem1)(princ)
  87.   )
  88. )
  89.  
  90. ; Get referenced item and recursively process
  91. (if RefIndex
  92.   (progn
  93.         (setq Ans (processlist MyList RefIndex (1+ NestDepth)))         ; recursion
  94.        
  95.         (setq Total1 (+ Total1 (nth 0 Ans)))    ; accumulate first value
  96.         (setq Total2 (+ Total2 (nth 1 Ans)))    ; accumulate second value
  97.   )    
  98. ) ; close if
  99.  
  100. (if (eq Verbose 1)
  101.   (progn
  102.         (prompt "\n  ")(princ (stringdupe " " NestDepth))(prompt "Total1 = ")(princ Total1)(prompt "  Total2 = ")(princ Total2)(princ)
  103.   )
  104. )
  105.  
  106. (setq OutList (list Total1 Total2))
  107. OutList
  108. )
  109.  
  110.  
  111. ; ========================================================= Helper functions
  112.  
  113. (defun ShowList (MyList IndentStr / CNT)
  114. ; Show a list one line at a time
  115. ; KJM
  116. ; Input:
  117. ;       MyList - (list) a list
  118. ;       IndentStr - (string) indent string e.g. "   ",  Default = "  " (2 spaces)
  119. ; Returns:
  120. ;       nothing
  121. ;       displays list to textscr
  122.  
  123. (if (not IndentStr) (setq IndentStr "  "))
  124.  
  125. (setq CNT 0)
  126. (repeat (length MyList)
  127.         (prompt "\n")(princ IndentStr)(princ CNT)(prompt "\t")(princ (nth CNT MyList))(princ)
  128.         (setq CNT (1+ CNT))
  129. ) ; close repeat
  130. )
  131.  
  132.  
  133. (defun StringDupe (Str Num / OutVal)
  134. ; Duplicate a string
  135. ; KJM - Oct 2022
  136. ; Input:
  137. ;       Str - (string) string to duplicate.  Default nil = " "
  138. ;       Num - (integer) number of times to duplicate. 0 = return null string ""
  139. ; Returns:
  140. ;       string
  141.  
  142. (if (not Str) (setq Str " "))
  143.  
  144. (setq OutVal "")
  145.  
  146. (if (> Num 0)
  147.         (repeat Num
  148.                 (setq OutVal (strcat OutVal Str))
  149.         )
  150. ) ; close if
  151.  
  152. OutVal
  153. )
  154.  

Results:
Code - Auto/Visual Lisp: [Select]
  1.   0    (10 10)
  2.   1    (15 12)
  3.   2    (23 24)
  4.   3    (27 11)
  5.   4    (19 16)
  6.   5    (55 4)
  7.   6    (30 41)
  8.   7    (26 50)
  9.   8    (-11 -56)
  10.   9    (48 0)
  11.   10     (20 -17)
  12.   11     (-5 -2)
  13.  

Matthew H

  • Newt
  • Posts: 70
Re: Referenced Sublist Nested Sum - AutoLisp Help
« Reply #7 on: October 25, 2022, 12:32:42 PM »
Kirby,

Thank you very much for your help!
I was actually working through the problem when you posted. I was making good progress, so I decided not to look at your function until I mine worked.
Unfortunately, it is also fairly inefficient as well. In order to handle the nesting levels, I repeated the function for the maximum possible nesting level.
I also separated the problem out, solving for (nth 0 and nth 1) of the input list, and then solving (nth 0 and nth 2) of the input list.
I did have to reference a "remove_nth" script from another forum. You can see that reference in my code below.
I also need to comment my code, so it is understandable for future references.
Thank you again by the way!

Code - Auto/Visual Lisp: [Select]
  1. ;; BEGIN Reference
  2. ;; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/removing-elements-from-a-nested-list/td-p/10510631
  3. ;; Miljenko Hatlak
  4. (defun C:removenth_nth (lst ntlst / a i ret)
  5.         (foreach e lst
  6.            (setq i -1)
  7.            (while (< (setq i (1+ i)) (length e))
  8.                         (if (not(member i ntlst)) (setq a (cons (nth i e) a)))
  9.            )
  10.            (setq ret (cons (reverse a) ret) a nil)
  11.         )
  12.         (reverse ret)
  13. )
  14. ;; END References
  15.  
  16.  
  17. (defun C:AddIndiceNumber ( sublist listofsublists / indices valueslist valuetoadd outputlist)
  18.                 (repeat (- (length listofsublists) 1)
  19.                         (setq indices (nth 0 sublist))
  20.                         (cond
  21.                                 ((/= indices nil)
  22.                                         (setq valueslist (cons (nth 1 sublist) valueslist))
  23.                                         (setq sublist (nth (nth 0 sublist) listofsublists))
  24.                                 )
  25.                                 (t (setq outputlist (apply '+ (cons (nth 1 sublist) valueslist))))
  26.                         )
  27.                 )
  28. )
  29.  
  30. (defun C:IterateEachSublist ( listofsublists / sublist outputlist )
  31.         (foreach sublist listofsublists
  32.                 (reverse (setq outputlist (cons (C:AddIndiceNumber sublist listofsublists) outputlist)))
  33.         )
  34. )
  35.  
  36. (defun C:OutputList ( InputList / )
  37.         (apply 'mapcar (cons 'list (list (C:IterateEachSublist (C:removenth_nth InputList (list 2))) (C:IterateEachSublist (C:removenth_nth InputList (list 1))))))
  38. )
  39.  
  40. (C:OutputList (list
  41.         ' (nil  10   10 )
  42.         ' (0    5    2 )
  43.         ' (1    8   12 )
  44.         ' (0   17    1 )
  45.         ' (0    9    6 )
  46.         ' (9    7    4 )
  47.         ' (3    3   30 )
  48.         ' (6   -4    9 )
  49.         ' (2  -34  -80 )
  50.         ' (6   18  -41 )
  51.         ' (nil 20  -17 )
  52.         ' (10 -25   15 )
  53. ))

kirby

  • Newt
  • Posts: 127
Re: Referenced Sublist Nested Sum - AutoLisp Help
« Reply #8 on: October 25, 2022, 01:32:01 PM »
Excellent, always better to understand by doing it yourself.

Comments (like meaningful function and variable names) are gold.

One comment on your code:
Normal convention is for command type functions (callable from command line) begin with 'C:' prefix and do not have arguments.  Internal functions can be used like build in AutoLisp functions from the command line, enclose within parentheses and including arguments e.g. (removenth lst ntlst)

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Referenced Sublist Nested Sum - AutoLisp Help
« Reply #9 on: October 30, 2022, 12:49:35 PM »
Here's another possible method -
Code - Auto/Visual Lisp: [Select]
  1. (defun foo ( l )
  2.     (mapcar '(lambda ( x ) (apply 'mapcar (cons '+ (bar x l)))) l)
  3. )
  4. (defun bar ( l m )
  5.     (if (car l)
  6.         (cons (cdr l) (bar (nth (car l) m) m))
  7.         (list (cdr l))
  8.     )
  9. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (foo
  2.    '(
  3.         (nil  10   10)
  4.         (0     5    2)
  5.         (1     8   12)
  6.         (0    17    1)
  7.         (0     9    6)
  8.         (9     7    4)
  9.         (3     3   30)
  10.         (6    -4    9)
  11.         (2   -34  -80)
  12.         (6    18  -41)
  13.         (nil  20  -17)
  14.         (10  -25   15)
  15.     )
  16. )
  17. ((10 10) (15 12) (23 24) (27 11) (19 16) (55 4) (30 41) (26 50) (-11 -56) (48 0) (20 -17) (-5 -2))

Beware of loops and self-referencing sublists though!