Author Topic: Recursion to foreach  (Read 2062 times)

0 Members and 1 Guest are viewing this topic.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1454
  • Marco
Recursion to foreach
« on: August 02, 2016, 03:14:50 AM »
I have been using for many years this piece of code but now I have problems in batch process with Bricscad, I think is due to the recursion (AutoCAD has no problem).
The function is complicated so I would not be wrong, can anyone help me to rewrite without recursion?

Code: [Select]
;;*************************************************************
;;   Copyright Vladimir Nesterovsky 1997, All Rights Reserved
;;
;; You may use, copy and distribute this program *unmodified*
;; for any non-commercial, non-profit purpose and without
;; charging any fee, if you keep this notice in its entirety
;;       in all copies and in all your derived works.
;;
;; This program is provided "AS IS" and has absolutely
;;  no warranty of any kind. Use it at your own risk.
;;
;; You're welcome to contact me about the possibility to use
;; this program commercially, and also with any comments and
;;        requests at vnestr@netvision.net.il,
;;              http://vnestr.tripod.com/
;;*************************************************************
;; a special encoding function for Xrecords to be used in dictionaries
(defun x-enlist ( lst )  ;; encode!
  (cond
   ((null lst) lst)
   ((atom lst)           ;; automatic code groups
     (cond
      ((= 'REAL (type lst))
        (list (cons 40 lst)))
      ((= 'INT (type lst))
       (if               ;; special handling of long integers
         (< -32768 lst 32767)
          (list (cons 70 lst))
          (list (cons 41 (float lst)))))
      ((= 'STR (type lst))
        (list (cons  1 lst)))
      (T nil)))
   ((and (cdr lst) (atom (cdr lst)))
      (list lst))      ;; pass dotted pair AS IS -- must be valid!!
   ((and (= (length lst) 3)
         (apply 'and (mapcar 'numberp lst)))
     (list (cons 10 lst)))
   ((cons '(2 . "{")
     (reverse
      (cons '(2 . "}")
       (reverse
        (apply 'append
         (mapcar 'x-enlist lst) ))))
     )
    )
  )
)

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Recursion to foreach
« Reply #1 on: August 02, 2016, 04:36:12 AM »
not tested...

Code - Auto/Visual Lisp: [Select]
  1. (defun test (l / c)
  2.   ;;(test l)
  3.   (setq c t
  4.         l (list l)
  5.   )
  6.   (while c
  7.     (setq c nil
  8.           l (apply (function append)
  9.                    (mapcar (function
  10.                              (lambda (a)
  11.                                (cond ((listp (car a)) (setq c t) (cons '(2 . "{") (reverse (cons '(2 . "}") (reverse a)))))
  12.                                      ((atom (cdr a)) (list a))
  13.                                      ((and (= (length a) 3) (apply 'and (mapcar 'numberp a))) (list (cons 10 a)))
  14.                                      ((cons '(2 . "{")
  15.                                             (reverse (cons '(2 . "}")
  16.                                                            (reverse (mapcar (function (lambda (b)
  17.                                                                                         (cond ((= 'REAL (type b)) (cons 40 b))
  18.                                                                                               ((= 'INT (type b))
  19.                                                                                                (if (< -32768 b 32767)
  20.                                                                                                  (cons 70 b)
  21.                                                                                                  (cons 41 (float b))
  22.                                                                                                )
  23.                                                                                               )
  24.                                                                                               ((= 'STR (type b)) (cons 1 b))
  25.                                                                                         )
  26.                                                                                       )
  27.                                                                             )
  28.                                                                             a
  29.                                                                     )
  30.                                                            )
  31.                                                      )
  32.                                             )
  33.                                       )
  34.                                      )
  35.                                )
  36.                              )
  37.                            )
  38.                            l
  39.                    )
  40.             )
  41.     )
  42.   )
  43.   l
  44. )

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1454
  • Marco
Re: Recursion to foreach
« Reply #2 on: August 02, 2016, 04:47:20 AM »
YES, it seems ok:
Code: [Select]
(x-enlist '(("$LAY_NORM" "1" "0") ("$LAY_TOP_NORM" "52" "0") ("$LAY_MIDI" "4" "0") ("$LAY_TOP_MIDI" "4" "0")))
=>((2 . "{") (2 . "{") (1 . "$LAY_NORM") (1 . "1") (1 . "0") (2 . "}") (2 . "{") (1 . "$LAY_TOP_NORM") (1 . "52") (1 . "0") (2 . "}") (2 . "{") (1 . "$LAY_MIDI") (1 . "4") (1 . "0") (2 . "}") (2 . "{") (1 . "$LAY_TOP_MIDI") (1 . "4") (1 . "0") (2 . "}") (2 . "}"))

(test '(("$LAY_NORM" "1" "0") ("$LAY_TOP_NORM" "52" "0") ("$LAY_MIDI" "4" "0") ("$LAY_TOP_MIDI" "4" "0")))
=>((2 . "{") (2 . "{") (1 . "$LAY_NORM") (1 . "1") (1 . "0") (2 . "}") (2 . "{") (1 . "$LAY_TOP_NORM") (1 . "52") (1 . "0") (2 . "}") (2 . "{") (1 . "$LAY_MIDI") (1 . "4") (1 . "0") (2 . "}") (2 . "{") (1 . "$LAY_TOP_MIDI") (1 . "4") (1 . "0") (2 . "}") (2 . "}"))
Thanks!

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Recursion to foreach
« Reply #3 on: August 02, 2016, 06:00:02 AM »
If your lists contain only text

Code - Auto/Visual Lisp: [Select]
  1. (defun test1 (l)
  2.  (defun f (l) (cons '(2 . "{") (reverse (cons '(2 . "}") l))))
  3.            (mapcar (function (lambda (a)
  4.                               (f (mapcar (function (lambda (b) (cons 1 b))) a))
  5.                               )
  6.                              )
  7.                    l
  8.                    )
  9.            )
  10.     )
  11.  )

Code: [Select]
(test1  '(("$LAY_NORM" "1" "0") ("$LAY_TOP_NORM" "52" "0") ("$LAY_MIDI" "4" "0") ("$LAY_TOP_MIDI" "4" "0")))


danallen

  • Guest
Re: Recursion to foreach
« Reply #5 on: August 02, 2016, 09:32:53 AM »
Have you shared this issue with Bricscad? From what I understand the Brics lisp engine shouldn't have any problem with recursion

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1454
  • Marco
Re: Recursion to foreach
« Reply #6 on: August 02, 2016, 03:01:20 PM »
Have you shared this issue with Bricscad? From what I understand the Brics lisp engine shouldn't have any problem with recursion
What you say is correct, the problem was not due to recursion I will tell you, I hope, what was the real source of the problem (I need more time).

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1454
  • Marco
Re: Recursion to foreach
« Reply #7 on: August 16, 2016, 04:45:19 AM »
Have you shared this issue with Bricscad? From what I understand the Brics lisp engine shouldn't have any problem with recursion
I apologize for the delay, I can confirm that the error is not due to recursion in Bricscad.
The Bricscad error handler signaled me the error in the recursive function and led me down the wrong path:
Code: [Select]
; ----- LISP : Call Stack -----
; [0]....
; [1]......
; [2]...........
; [3]..........
; [4]............DICTPUT <<--
My program processes many DWG files in batch and sometimes fails to write on the dictionary, perhaps the (namedobjdict) function does not give the correct result switching from one file to another...

Ciao.