Author Topic: Generate Matrix List  (Read 1362 times)

0 Members and 1 Guest are viewing this topic.

Grrr1337

  • Swamp Rat
  • Posts: 812
Generate Matrix List
« on: April 27, 2017, 03:51:30 PM »
Hi guys,
Anyone bored ? Well heres some challenge that might be interesting:
How do you write a function that generates matrix list by a given string (that represents excel's cell) i.e.:
Code - Auto/Visual Lisp: [Select]
  1. _$ (GenerateMatrixList "C3")
  2. '(("A1" "A2" "A3")
  3.   ("B1" "B2" "B3")
  4.   ("C1" "C2" "C3")
  5. )

 :thinking:
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Generate Matrix List
« Reply #1 on: April 27, 2017, 06:02:13 PM »
Quickly written:
Code - Auto/Visual Lisp: [Select]
  1. (defun excelmatrix ( s / foo bar a b )
  2.     (defun foo ( c )
  3.         (if (/= c a)
  4.             (cons (bar c 1) (foo (LM:alpha++ c)))
  5.             (list (bar c 1))
  6.         )
  7.     )
  8.     (defun bar ( c r )
  9.         (if (<= r b) (cons (strcat c (itoa r)) (bar c (1+ r))))
  10.     )
  11.     (setq a (vl-string-right-trim "0123465789" (strcase s))
  12.           b (atoi (substr s (1+ (strlen a))))
  13.     )
  14.     (foo "A")
  15. )
  16.  
  17. ;; Alpha++  -  Lee Mac
  18. ;; Increments an uppercase alphabetical string by one, e.g. AZ => BA
  19. ;; a - [str] uppercase alphabetical string
  20.  
  21. (defun LM:alpha++ ( a )
  22.     (   (lambda ( f ) (vl-list->string (reverse (f (reverse (vl-string->list a))))))
  23.         (lambda ( l )
  24.             (if l
  25.                 (if (= 90 (car l))
  26.                     (cons 65 (f (cdr l)))
  27.                     (cons (1+ (car l)) (cdr l))
  28.                 )
  29.                '(65)
  30.             )
  31.         )
  32.     )
  33. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (excelmatrix "C3")
  2. (("A1" "A2" "A3") ("B1" "B2" "B3") ("C1" "C2" "C3"))

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Generate Matrix List
« Reply #2 on: April 27, 2017, 06:13:42 PM »
Another:
Code - Auto/Visual Lisp: [Select]
  1. (defun excelmatrix ( s / a b c l r )
  2.     (setq a (vl-string-right-trim "0123465789" (strcase s))
  3.           b (atoi (substr s (1+ (strlen a))))
  4.           c "A"
  5.     )
  6.     (repeat b (setq l (cons (itoa b) l) b (1- b)))
  7.     (while (/= a c)
  8.         (setq r (cons (mapcar '(lambda ( x ) (strcat c x)) l) r)
  9.               c (LM:alpha++ c)
  10.         )
  11.     )
  12.     (reverse (cons (mapcar '(lambda ( x ) (strcat c x)) l) r))
  13. )
  14.  
  15. ;; Alpha++  -  Lee Mac
  16. ;; Increments an uppercase alphabetical string by one, e.g. AZ => BA
  17. ;; a - [str] uppercase alphabetical string
  18.  
  19. (defun LM:alpha++ ( a )
  20.     (   (lambda ( f ) (vl-list->string (reverse (f (reverse (vl-string->list a))))))
  21.         (lambda ( l )
  22.             (if l
  23.                 (if (= 90 (car l))
  24.                     (cons 65 (f (cdr l)))
  25.                     (cons (1+ (car l)) (cdr l))
  26.                 )
  27.                '(65)
  28.             )
  29.         )
  30.     )
  31. )

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Generate Matrix List
« Reply #3 on: April 27, 2017, 06:28:58 PM »
Hi Lee,
I don't understand a thing in your first code * scratches head * (cause of the passing variables between the subfunctions and aswell they use recursions), but the important is that it works.
Good job, you brought me the nostalgia - back when I was lisp newbie and you still were providing overwhelming solutions.  :uglystupid2:
Atleast I understood the iterative version. :)

This function is handy for experimenting with tables (generating/manipulating).
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg