Author Topic: [lesson] mapcar-lambda construct  (Read 1585 times)

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
[lesson] mapcar-lambda construct
« on: December 07, 2021, 10:57:45 AM »
Mapcar-lambda is easy. Here, watch:

Mapcar: Do <something> to every item in a list.

Lambda: Same as defun but no `name' ...A Lambda is list of process' just like the formal (named) procedure defun.

Demonstrations:

I will demonstrate the process of printing each string item in a list.

Code - Auto/Visual Lisp: [Select]
  1. (defun print-it (lst)
  2.    (if (null lst)
  3.    ;; if the list is `empty' or `nil'
  4.      nil
  5.    ;; return `nil'
  6.      (progn
  7.    ;; otherwise
  8.         (princ (car lst))
  9.    ;; print the first item in the list
  10.         (print-it (cdr lst))
  11.    ;; strip off the first item (because we just printed it) and send the rest of the list back through this function again.
  12.         )
  13.      )
  14.    )

Code: [Select]
(print-it '("one" "two" "three" "four" "five" "six" "se7en"))
Lambda is even easier to unerstand. Since it is nothing more then a formal procedure without a name it can be defined on the fly (so to speak) or at the point of use/need.

Code: [Select]
( (lambda () (1+ 2)) )
Will return what? ...3. Good.

So, a Mapcar-Lambda construct is saying nothing more then: "Preform <this> function on each and every item in a list."

How's that? Is the process a bit more clear?


Extra Credit:
using the print-it function above we can build our own version of mapcar.


Here is a redefinition of mapcar.
Code - Auto/Visual Lisp: [Select]
  1. (defun map (proc items)
  2.   (if (null items)
  3.     nil
  4.     (cons (proc (car items))
  5.           (map proc (cdr items))
  6.       )
  7.     )
  8.   (princ)
  9.   )

Code: [Select]
(map princ '("one" "two" "three" "four" "five" "six" "se7en"))
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

mhupp

  • Bull Frog
  • Posts: 250
Re: [lesson] mapcar-lambda construct
« Reply #1 on: December 07, 2021, 05:47:09 PM »
Thanks buddy, hope this wasn't just for me. I do under stand mapcar-lambda for the most part. What gets me is that if I cant follow step by step process in Bricscad's version of VLIDE. when it gets to mapcar-lambda it will run through everything in one go. So its hard for me to see what i need to change to get the output I want. for instance banged my head on the table a few nights to get this to sort a ssget entity's left to right & top to bottom.


Code - Auto/Visual Lisp: [Select]
  1. (setq tlst (vl-sort tlst '(lambda (a b) (if (equal (car (cdr (assoc 10 (entget a)))) (car (cdr (assoc 10 (entget b)))) 1e-6)
  2.                                     (> (cadr (cdr (assoc 10 (entget a)))) (cadr (cdr (assoc 10 (entget b)))))
  3.                                     (< (car (cdr (assoc 10 (entget a)))) (car (cdr (assoc 10 (entget b)))))
  4.                                   )
  5.                           )
  6.            )
  7. )

one or two operations are fine but when it gets to lee Mac style of coding where its mapcar lambda wrapped in another mapcar lambda it gets confusing.

ronjonp

  • Needs a day job
  • Posts: 7527
Re: [lesson] mapcar-lambda construct
« Reply #2 on: December 07, 2021, 10:05:10 PM »
Thanks buddy, hope this wasn't just for me. I do under stand mapcar-lambda for the most part. What gets me is that if I cant follow step by step process in Bricscad's version of VLIDE. when it gets to mapcar-lambda it will run through everything in one go. So its hard for me to see what i need to change to get the output I want. for instance banged my head on the table a few nights to get this to sort a ssget entity's left to right & top to bottom.


Code - Auto/Visual Lisp: [Select]
  1. (setq tlst (vl-sort tlst '(lambda (a b) (if (equal (car (cdr (assoc 10 (entget a)))) (car (cdr (assoc 10 (entget b)))) 1e-6)
  2.                                     (> (cadr (cdr (assoc 10 (entget a)))) (cadr (cdr (assoc 10 (entget b)))))
  3.                                     (< (car (cdr (assoc 10 (entget a)))) (car (cdr (assoc 10 (entget b)))))
  4.                                   )
  5.                           )
  6.            )
  7. )

one or two operations are fine but when it gets to lee Mac style of coding where its mapcar lambda wrapped in another mapcar lambda it gets confusing.
@mhupp .. give it a test:
Code - Auto/Visual Lisp: [Select]
  1. (setq tlst (mapcar 'cadr (ssnamex (ssget "_X" '((0 . "INSERT"))))))
  2. (setq tlst2 (mapcar '(lambda (x) (list (cdr (assoc 10 (entget x))) x)) tlst))
  3. (benchmark
  4.   '((vl-sort
  5.      tlst
  6.      '(lambda (a b)
  7.         (if (equal (car (cdr (assoc 10 (entget a)))) (car (cdr (assoc 10 (entget b)))) 1e-6)
  8.           (> (cadr (cdr (assoc 10 (entget a)))) (cadr (cdr (assoc 10 (entget b)))))
  9.           (< (car (cdr (assoc 10 (entget a)))) (car (cdr (assoc 10 (entget b)))))
  10.         )
  11.       )
  12.     )
  13.     (mapcar
  14.      'cadr
  15.      (vl-sort
  16.       tlst2
  17.       '(lambda (a b)
  18.          (if (equal (caar a) (caar b) 1e-6)
  19.            (> (cadr (car a)) (cadr (car b)))
  20.            (< (car (car a)) (car (car b)))
  21.          )
  22.        )
  23.      )
  24.     )
  25.    )
  26. )

100 block test:
Quote
    (MAPCAR (QUOTE CDR) (VL-SORT TLST2 (...)......1797 / 12.33 <fastest>
    (VL-SORT TLST (QUOTE (LAMBDA (A B) (...).....22156 / 1.00 <slowest>

---- Benchmark Utility: In memory of Michael Puckett ----
 
« Last Edit: December 08, 2021, 04:58:35 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: [lesson] mapcar-lambda construct
« Reply #3 on: December 08, 2021, 01:09:15 PM »
Thanks buddy, hope this wasn't just for me. I do under stand mapcar-lambda for the most part. What gets me is that if I cant follow step by step process in Bricscad's version of VLIDE. when it gets to mapcar-lambda it will run through everything in one go. So its hard for me to see what i need to change to get the output I want. for instance banged my head on the table a few nights to get this to sort a ssget entity's left to right & top to bottom.

Code - Auto/Visual Lisp: [Select]
  1. (setq tlst (vl-sort tlst '(lambda (a b) (if (equal (car (cdr (assoc 10 (entget a)))) (car (cdr (assoc 10 (entget b)))) 1e-6)
  2.                                     (> (cadr (cdr (assoc 10 (entget a)))) (cadr (cdr (assoc 10 (entget b)))))
  3.                                     (< (car (cdr (assoc 10 (entget a)))) (car (cdr (assoc 10 (entget b)))))
  4.                                   )
  5.                           )
  6.            )
  7. )

one or two operations are fine but when it gets to lee Mac style of coding where its mapcar lambda wrapped in another mapcar lambda it gets confusing.

I'm sorry, but that is some horrible code styling! Not only does it need to pick a style but also learn to wrap at a somewhat decent horizontal position (I typically wrap my code at the maximum of 78th column).

A good rule of thumb I have is when I start creating variables in a lambda statement, I consider creating a separate function instead (but I would create a separate function for that mess--create a list and call a separate sorting function on that list).

ASIDE:
1. In programming, there are typically very few reasons to actually sort a list (it takes up unnecessary processor time--a program shouldn't care and/or be able to deal with a messy list). Why are you sorting a list?

2. I like ronjonp's version better.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

mhupp

  • Bull Frog
  • Posts: 250
Re: [lesson] mapcar-lambda construct
« Reply #4 on: December 08, 2021, 06:04:47 PM »
ASIDE:
1. In programming, there are typically very few reasons to actually sort a list (it takes up unnecessary processor time--a program shouldn't care and/or be able to deal with a messy list). Why are you sorting a list?

2. I like ronjonp's version better.

Didn't say it was good code i need to get some books or something.

1. Sorting a selection set of tables. So i can either look/edit them one by one in order. Or split up my viewport into 8 sections and view 8 at a time in order. Allowing me to easily transfer data between them.  (usually only have to compare 15 or so tables)
2. Who wouldn't! not hurting my feelings one bit I have already replaced that code. I'm self taught in lisp so If I get stumped ill try and google it first. usually I can find code that works or can edit to my needs.
« Last Edit: December 08, 2021, 06:12:43 PM by mhupp »

ronjonp

  • Needs a day job
  • Posts: 7527
Re: [lesson] mapcar-lambda construct
« Reply #5 on: December 08, 2021, 06:10:44 PM »
ASIDE:
1. In programming, there are typically very few reasons to actually sort a list (it takes up unnecessary processor time--a program shouldn't care and/or be able to deal with a messy list). Why are you sorting a list?

2. I like ronjonp's version better.

Didn't say it was good code i need to get some books or something.

1. Sorting a selection set of tables. So i can either look/edit them one by one in order. Or split up my viewport into 8 sections and view 8 at a time in order. Allowing me to easily transfer data between them.
2. Who wouldn't! not hurting my feelings one bit I have already replaced that code. I'm self taught in lisp so If I get stumped ill try and google it first. usually I can find code that works or can edit to my needs.
We're all self taught ( with the help of many others :) ). Keep practicing and asking questions and you'll just get better! *beers*

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: [lesson] mapcar-lambda construct
« Reply #6 on: December 08, 2021, 06:22:01 PM »
haha ...noted.

In the beginning I fumbled around with AutoLisp for a few months but I soon took a course and I learned from the Structure and Interpretation of Computer Programs course. You can safely read the first two chapters and make almost a simple translation to AutoLisp. When you get into chapter 4 you start building a compiler and you will not be able to port any of that stuff to AutoLisp easily so it would be up to you if you'd want to keep going. At any rate, I'm just getting back into AutoLisp and I'm rusty (and I've forgotten most of that book too) so, I'll read the first few chapters with you if you're interested.

[ https://mitpress.mit.edu/sites/default/files/sicp/full-text/book/book-Z-H-4.html#%_toc_start ]
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org