Author Topic: help list  (Read 4176 times)

0 Members and 1 Guest are viewing this topic.

Q1241274614

  • Guest
help list
« on: January 21, 2013, 06:54:47 AM »

((“nr” “X” “Y” “Z”) (1.0 -55687.7 35585.1 0.0) (2.0 -54769.5 35230.2 0.0) (3.0 -54532.6 35023.2 0.0) (4.0 -55184.2 34283.8 0.0) (5.0 -55983.9 35230.2 0.0))

result: ((“nr” “X” “Y” “Z”) (“1.0”  “-55687.7”  “35585.1”  “0.0”) (“2.0”  “-54769.5”  “35230.2”  “0.0”) (“3.0”  “-54532.6”  “35023.2” “0.0" ) ("4.0"  "-55184.2" "34283.8"  "0.0") ("5.0" "-55983.9" "35230.2" "0.0"))
« Last Edit: January 21, 2013, 07:26:32 AM by Q1241274614 »

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: help list
« Reply #1 on: January 21, 2013, 07:01:36 AM »
Code - Auto/Visual Lisp: [Select]
  1.     (lambda (a)
  2.       (mapcar
  3.         (function
  4.           (lambda (b)
  5.             (vl-princ-to-string b)
  6.             )
  7.           )
  8.         a
  9.         )
  10.       )
  11.     )
  12.  '(("nr" "X" "Y" "Z") (1.0 -55687.7 35585.1 0.0) (2.0 -54769.5 35230.2 0.0) (3.0 -54532.6 35023.2 0.0) (4.0 -55184.2 34283.8 0.0) (5.0 -55983.9 35230.2 0.0))
  13.   )

Q1241274614

  • Guest
Re: help list
« Reply #2 on: January 21, 2013, 07:09:32 AM »
thank!

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: help list
« Reply #3 on: January 21, 2013, 07:36:42 AM »
Code - Auto/Visual Lisp: [Select]
  1.     (lambda (a)
  2.       (mapcar
  3.         (function
  4.           (lambda (b)
  5.             (vl-princ-to-string b)
  6.             )
  7.           )
  8.         a
  9.         )
  10.       )
  11.     )
  12.  '(("nr" "X" "Y" "Z") (1.0 -55687.7 35585.1 0.0) (2.0 -54769.5 35230.2 0.0) (3.0 -54532.6 35023.2 0.0) (4.0 -55184.2 34283.8 0.0) (5.0 -55983.9 35230.2 0.0))
  13.   )
You don't need the 2nd lambda, you could simply directly call vl-princ-to-string from the mapcar.
Code - Auto/Visual Lisp: [Select]
  1.         '(("nr" "X" "Y" "Z")
  2.           (1.0 -55687.7 35585.1 0.0)
  3.           (2.0 -54769.5 35230.2 0.0)
  4.           (3.0 -54532.6 35023.2 0.0)
  5.           (4.0 -55184.2 34283.8 0.0)
  6.           (5.0 -55983.9 35230.2 0.0)))
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: help list
« Reply #4 on: January 21, 2013, 07:46:41 AM »
Or a more generalized version allowing for multiple levels of sub-lists:
Code - Auto/Visual Lisp: [Select]
  1. (defun princ-list (source /)
  2.   (cond ((listp source) (mapcar 'princ-list source))
  3.         (t (vl-princ-to-string source))))
E.g.
Code: [Select]
_$ (princ-list
        '(("nr" "X" "Y" "Z")
          (1.0 -55687.7 35585.1 0.0)
          (2.0 -54769.5 35230.2 0.0)
          (3.0 -54532.6 35023.2 0.0)
          (4.0 -55184.2 34283.8 0.0)
          (5.0 -55983.9 35230.2 0.0)
          (6.0 (1 "test" 7))))
(("nr" "X" "Y" "Z") (1.0 -55687.7 35585.1 0.0) (2.0 -54769.5 35230.2 0.0) (3.0 -54532.6 35023.2 0.0) (4.0 -55184.2 34283.8 0.0) (5.0 -55983.9 35230.2 0.0) (6.0 (1 "test" 7)))
« Last Edit: January 21, 2013, 07:54:14 AM by irneb »
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

ribarm

  • Gator
  • Posts: 3313
  • Marko Ribar, architect
Re: help list
« Reply #5 on: January 21, 2013, 07:47:41 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun foo ( l / a b c )
  2.   (while (setq a (car l))
  3.     (setq l (cdr l))
  4.     (if (eq (type a) 'str)
  5.         (setq b (cons a b))
  6.         (cond ((eq (type a) 'int)
  7.                (setq b (cons (itoa a) b))
  8.               )
  9.               ((eq (type a) 'real)
  10.                (setq b (cons (rtos a 2 1) b))
  11.               )
  12.               ((eq (type a) 'list)
  13.                (setq b (foo a))
  14.               )
  15.         )
  16.     )
  17.     (if (eq (length b) 1) (setq c (cons (car b) c)) (setq c (cons b c)))
  18.     (setq b nil)
  19.   )
  20.   (reverse c)
  21. )
  22.  

M.R.
« Last Edit: January 22, 2013, 01:05:21 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: help list
« Reply #6 on: January 21, 2013, 07:56:37 AM »
Or my version not using vl functions:
Code - Auto/Visual Lisp: [Select]
  1. (defun princ-list (source /)
  2.   (cond ((listp source) (mapcar 'princ-list source))
  3.         ((= (type source) 'Int) (itoa source))
  4.         ((= (type source) 'Real) (rtos source))
  5.         (t source)))

The reason mine is so much shorter than yours (ribarm) is due to recursion. This is usually where recursion makes coding a lot simpler than the usual iteration with state variables.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: help list
« Reply #7 on: January 21, 2013, 08:05:33 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun f ( l )
  2.     (if (atom l)
  3.         (vl-princ-to-string l)
  4.         (cons (f (car l)) (if (cdr l) (f (cdr l))))
  5.     )
  6. )

Code: [Select]
_$ (f '((1 . 2) (1 2 3)))
(("1" . "2") ("1" "2" "3"))

Q1241274614

  • Guest
Re: help list
« Reply #8 on: January 21, 2013, 08:15:45 AM »


(apply 'max (mapcar  'strlen (mapcar 'car lst)))
(apply 'max (mapcar  'strlen (mapcar 'cadr lst)))
(apply 'max (mapcar  'strlen (mapcar 'caddr lst)))
。。。。。。。。
If there are N, how should solve。

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: help list
« Reply #9 on: January 21, 2013, 08:24:41 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun f (l)
  2.   (cond ((not l) nil)
  3.         ((atom l) (vl-princ-to-string l))
  4.         ((cons (f (car l)) (f (cdr l))))
  5.   )
  6. )
  7. (f '((1 . 2) (1 2 3) ("1" "2" "3")))

Code - Auto/Visual Lisp: [Select]
  1. (defun f1 (l)
  2.   (if (listp l)
  3.     (mapcar 'f1 l)
  4.   )
  5. )
  6. (f1 '((1 2 3) ("1" "2" "3")))

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: help list
« Reply #10 on: January 21, 2013, 08:27:53 AM »
(apply 'max (mapcar  'strlen (mapcar 'car lst)))
(apply 'max (mapcar  'strlen (mapcar 'cadr lst)))
(apply 'max (mapcar  'strlen (mapcar 'caddr lst)))
。。。。。。。。
If there are N, how should solve。

Code - Auto/Visual Lisp: [Select]
  1. (defun f ( l )
  2.     (mapcar '(lambda ( x ) (apply 'max (mapcar 'strlen x))) (apply 'mapcar (cons 'list l)))
  3. )

Code - Auto/Visual Lisp: [Select]
  1. (defun f ( l )
  2.     (if (car l)
  3.         (cons
  4.             (apply 'max (mapcar 'strlen (mapcar 'car l)))
  5.             (f (mapcar 'cdr l))
  6.         )
  7.     )
  8. )

Recursive, for entire list:
Code - Auto/Visual Lisp: [Select]
  1. (defun f ( l )
  2.     (if (= 'str (type l))
  3.         (strlen l)
  4.         (max (f (car l)) (if (cdr l) (f (cdr l)) 0))
  5.     )
  6. )

Code - Auto/Visual Lisp: [Select]
  1. _$ (f '(("a" "abc" ("b" . "def") ("ef" "ghij" ("klmn" . "o")))))
  2. 4
« Last Edit: January 21, 2013, 10:23:17 AM by Lee Mac »

Q1241274614

  • Guest
Re: help list
« Reply #11 on: January 21, 2013, 08:38:12 AM »
thank!

ribarm

  • Gator
  • Posts: 3313
  • Marko Ribar, architect
Re: help list
« Reply #12 on: January 21, 2013, 09:26:42 AM »
If you have lists within list, like Irneb thought, for entire list max strlen of string :

Code - Auto/Visual Lisp: [Select]
  1. (defun f ( l / ff b )
  2.   (defun ff ( l / a )
  3.     (while (setq a (car l))
  4.       (setq l (cdr l))
  5.       (cond ((and (eq (type a) 'str))
  6.              (setq b (append (list (strlen a)) b))
  7.             )
  8.             ((eq (type a) 'list)
  9.              (ff a)
  10.             )
  11.       )
  12.       (if (not l)
  13.         (setq b (list (apply 'max b)))
  14.       )
  15.     )
  16.     (car b)
  17.   )
  18.   (ff l)
  19. )
  20.  

Code: [Select]
(f (foo '(("nr" "X" "Y" "Z") (1.0 -55687.7 35585.1 0.0) (2.0 -54769.5 35230.2 0.0) (3.0 -54532.6 35023.2 0.0) (4.0 -55184.2 34283.8 0.0) (5.0 -55983.9 35230.2 0.0)) ))

foo is my above posted code...
« Last Edit: January 21, 2013, 11:38:10 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: help list
« Reply #13 on: January 21, 2013, 11:08:25 AM »
If you have lists within list, like Irneb thought, for entire list max strlen of string :

Code - Auto/Visual Lisp: [Select]
  1. (defun f ( l / a )...

Careful...  :wink:

Code: [Select]
_$ (f '("a" "bc" ("def")))
3
_$ (f '("a" "b"))
3

Also:
Code: [Select]
_$ (f '("a" "b "))
1

ribarm

  • Gator
  • Posts: 3313
  • Marko Ribar, architect
Re: help list
« Reply #14 on: January 21, 2013, 11:33:41 AM »
I've corrected - I don't need (snvalid), so I removed it both from (foo) and (f)...

Code: [Select]
(snvalid "a ")
nil

M.R.
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube