Jump to content

Recommended Posts

Posted

I've seen mapcar in a few tutorials around. The way I see it, it applies a change to every element of a list.

 

For example (mapcar '1+ '(1 2 3))

would return something like (2 3 4)

 

And

(mapcar 'strcase '(a b c)) -> (A B C)

 

I understand this perfectly. But sometimes people use functions that are not that clear, for example

'cdr

 

I really don't get this, because cdr is supposed to return the 2nd element of a list. It's not a action I apply to a list, it's a return one. So how does (mapcar 'cdr ) works, and what does it do?

Posted (edited)

Hi CesarA,

 

 

You can also use foreach item (in) list do this or do that. I like to think of cdr as second and remaining. Mapcar 'cdr just means , for each element in list , or , apply the function cdr. If your list simply contains single elements this wouldn't work but if your list is like ((1 "a")(2 "b")...) then this function would return a list containing (("a") ("b") ...)

 

 

And if you use mapcar 'cadr instead of cdr you would get ("a" "b" ...)

 

 

gr.Rlx

Edited by rlx
Posted

Sorry didn't quite understand this:

 

mapcar 'cadr returns "a" "b"?

 

but cadr is the 3rd element, and there's no 3rd element in ((1 "a")(2 "b")...)

 

how come 'cdr would return (("a") ("b") ...) (this i understand)

but 'cadr would return ("a" "b" ...)??

 

There's a difference, but, i don't think it makes sense

Posted
Sorry didn't quite understand this:

 

mapcar 'cadr returns "a" "b"?

 

but cadr is the 3rd element, and there's no 3rd element in ((1 "a")(2 "b")...)

 

how come 'cdr would return (("a") ("b") ...) (this i understand)

but 'cadr would return ("a" "b" ...)??

 

There's a difference, but, i don't think it makes sense

 

 

well , try it for yourself :

 

 



(setq lst '((1 "a")(2 "b")(3 "c")))
(mapcar 'cdr lst)
(mapcar 'cadr lst)


 

 

cdr will return a list with the first element removed (second and remaining)

cadr will give the first element of the second part , equal to (car (cdr ...)

 

 

There is a big difference between a list and a 'atom' , (list 1 2 3 4) is not the same as (list (list 1 2) (list 3 4))

 

 

the first is a list of 4 atoms , the second gives you a list of 2 list's , each list containing 2 atoms

 

 

gr. Rlx

Posted

try this

 

 



(defun c:tst ( / lst1 lst2 result1 result2 result3)
 ;age of jack and jill and their dog Bruno
 (setq lst1 '(("Jack" 22)("Jill" 25)("Bruno" 5))); -> (("Jack" 22) ("Jill" 25) ("bruno" 5))
 (setq result1 (car lst1)) ; -> ("Jack" 22)
 (setq result2 (cdr lst1)) ; -> (("Jill" 25) ("bruno" 5))
 (setq result3 (cadr lst1)); -> ("Jill" 25)
 (setq lst2 (list 1 2 3 4 5)); -> ( 1 2 3 4 5)
 (setq result1 (car lst2)) ; -> 1
 (setq result2 (cdr lst2)) ; -> (2 3 4 5)
 (setq result3 (cadr lst2)); -> 2 , same as (car (cdr lst2))
)


 

 

gr. Rlx

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...