Jump to content

Recommended Posts

Posted

About 2 weeks into my use of AutoLISP. I understand that mapcar evaluates each item seperately, and lamda is used as an undefined function, but:

 

In what situation would you need to use mapcar and lamda? Why would you need an output of a string of values other than presenting the output to the user?

Posted

Basically, 'mapcar' allows you to process each member of a list and return a value for each, grouped equally in another list.
How does 'mapcar' process that list? You can do so using a simple language function (for example, you could apply  '-  to a list of integers to change the sign of each one and obtain the resulting list), or you could apply a more elaborate function that could be implemented using 'lambda', which is nothing more than a temporary substitute for 'defun' that allows you to execute your code without it remaining in memory after execution.
It would be conceptually similar to implementing a local defun within a public defun: the local defun only exists during the execution of the public defun.

Posted

I like mapcar - LISP is a list processing language and mapcar is great for lists. A couple of examples

 

(setq Answer (mapcar 'Modifier' 'List to process 1' 'List to process 2' )) format

 

(setq Answer (mapcar '* '(1 1 0) '(15 20 25) )) ;;modifier is multiply list 1 by list 2 - list 2 is a coordinate, here set z to 0... flatten in
---> Answer: (15 20 0)


(setq Answer (mapcar '+ '(15 0 0) '(15 20 25) )) ;;modifier is multiply list 1 by list 2 - list 2 is a coordinate, offset X direction
---> Answer: (30 20 25)

(setq Answer (mapcar 'strcase '("adam" "ben" "claire" "david"))) ;; Modifier is a function, strcase applied to all in the list
---> Answer: ("ADAM" "BEN" "CLAIRE" "DAVID")

 

That last example was shamelessly taken from Lee Macs tutorial: https://lee-mac.com/mapcarlambda.html

  • Like 1
Posted

Although it may complicate the explanation a bit, as StevenP shows in his examples, it is also possible to apply the modifier code to multiple lists, but with two important considerations in mind:
- all lists should have the same number of elements because the result of the operations will stop at the last element of the shortest list
- the result of the operations will be performed, each time, on the same ordinal elements of each list, returning a result for each operation and adding them to a result list that will be returned at the end of all operations

  • Like 1
  • Agree 1
Posted
(mapcar '(lambda (x y) (/ (float x)(float y))) (list 2 4 6 8) (list 1 3 7 9))
(mapcar (function (lambda (x y) (/ (float x)(float y)))) (list 2 4 6 8) (list 1 3 7 9))

(defun my-funk (x y)(/ (float x)(float y)))
(mapcar 'my-funk (list 2 4 6 8) (list 1 3 7 9))

 ;;; -> (2.0 1.33333 0.857143 0.888889)

 

  • Like 1
Posted

my-funk ?

  • Funny 1
Posted
Quote
3 hours ago, Steven P said:
(setq Answer (mapcar '* '(1 1 0) '(15 20 25) )) ;;modifier is multiply list 1 by list 2 - list 2 is a coordinate, here set z to 0... flatten in
---> Answer: (15 20 0)


(setq Answer (mapcar '+ '(15 0 0) '(15 20 25) )) ;;modifier is multiply list 1 by list 2 - list 2 is a coordinate, offset X direction
---> Answer: (30 20 25)

(setq Answer (mapcar 'strcase '("adam" "ben" "claire" "david"))) ;; Modifier is a function, strcase applied to all in the list
---> Answer: ("ADAM" "BEN" "CLAIRE" "DAVID")

 

From the response I've seen, it seems like mapcar is only used to give an output of multiple separate values. I see how lamda would be used in a greater LISP to locally define throw-away functions. However, I still don't fully understand how the return values of mapcar can/would be used in a greater LISP routine. 

 

Say I want to calculate values based on set integers, and a user input integer. Can I use mapcar to:

1. Calculate using the set and user input value, and

2. Use those values in an if or cond function?

 

I may be misunderstanding when this function is most commonly used, so if what I am asking is not making sense, my lack of knowledge would be the reason.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...