Jump to content

The question about "apply" , "mapcar" .


jackyieman

Recommended Posts

Hello everyone.

I have a questionabout "apply" and "mapcar".

 

Please looks bellow code:

(setq b '((0 1 2) (3 4 5) (6 7 ))
(apply 'mapcar (cons 'list b))

it return : ((0 3 6 ) (1 4 7 ) (2 5 8 ) )

 

how it works?

 

 

and next question:

(setq a '(1 2 3 4 5 6 7 8 9 0))
(apply 'mapcar (cons 'list (list a (cdr a))))

it return : ( (1 2) (2 3) (3 4) (4 5) (5 6) (6 7) (7 8 ) (8 9) (9 0) )

 

how it works also?

Link to comment
Share on other sites

Welcome to CADTutor .

 

Hope that I could describe them as clear as you looking for :)

 

The first function apply implement the mapcar function on a list of lists and mapcar function iterate through each element of every list and with the use of cons and list functions you have them gathered in a list of lists as well .

 

The second question is also implement the mapcar function with the use of apply function to get the first and the second item of the list separately and I can offer you as alternative function in recursive manner.

 

(defun _list  (l)
;;;  Tharwat . 05.02.2015    ;;;
 (if (> (length l) 1)
   (cons (list (car l) (cadr l)) (_list (cdr l))))
 )

To test the above function :

 

(_list '(1 2 3 4 5 6 7 8 9 0))

[color=red];; Returns [/color]
((1 2) (2 3) (3 4) (4 5) (5 6) (6 7) (7  (8 9) (9 0))

Link to comment
Share on other sites

Thanks to you, Mr.Tharwat !

 

But i still confuse on some details.

First,

I know the mapcar function just like foreach. In code 1 , why the next value is "3" not "1" ?

please looks other example :

(setq lst '((3 4 5) (0 1 2) (6 7 ))
(apply 'mapcar (cons 'min lst))

the result shows (0 1 2)

why its not (3 0 6)?

 

 

Second,

Why add the list(using apostrope for not to evulate) in fornt of a list , and using apply can gather the list?

 

Thanks!

Link to comment
Share on other sites

Thanks to you, Mr.Tharwat !

But i still confuse on some details.

First,

I know the mapcar function just like foreach. In code 1 , why the next value is "3" not "1" ?

 

Because the mapcar function iterate through each first element of the target list first then the second then the third .... to the end .

 

(setq lst '((3 4 5) (0 1 2) (6 7 ))
(apply 'mapcar (cons 'min lst))

the result shows (0 1 2)

why its not (3 0 6)?

Because the minimum values for the target list is consist of three element and the outcome came in three including the minimum numbers .

 

Second,

Why add the list(using apostrope for not to evulate) in fornt of a list , and using apply can gather the list?

 

That required from the mapcar function , first apply function needs a function as a first argument then the mapcar function also requires a function as a first argument .

 

I am happy with your questions because that also refresh my memory with the fore-said functions :)

Link to comment
Share on other sites

Thanks you !

I have clear understand those function after yours explaination!

 

happy to hear that and you are most welcome .

 

Keep on practicing with functions to become familiar with them and don't be surprised or panic if you one day have noticed that a specific function doing something than you

used to see or to know :lol: an example one is the entdel function .

Link to comment
Share on other sites

Please looks bellow code:

(setq b '((0 1 2) (3 4 5) (6 7 ))
(apply 'mapcar (cons 'list b))

it return : ((0 3 6 ) (1 4 7 ) (2 5 8 ) )

 

how it works?

 

Consider that:

(apply 'mapcar (cons 'list '((0 1 2) (3 4 5) (6 7 )))

Is the same as:

(mapcar 'list '(0 1 2) '(3 4 5) '(6 7 )

Which evaluates in the same way as:

(list (list 0 3 6) (list 1 4 7) (list 2 5 )

Returning:

((0 3 6) (1 4 7) (2 5 )

Link to comment
Share on other sites

Thanks you also , Mr. Lee Mac!

 

I try other example, it really amazing.

But why using apply and mapcar , why not just using mapcar?

Is that any reasons ? much faster ?

 

Thanks!

Link to comment
Share on other sites

Thanks you also , Mr. Lee Mac!

 

You're welcome!

 

But why using apply and mapcar , why not just using mapcar?

Is that any reasons ? much faster ?

 

The function or functions required depends on the format of the input data, the operations that are to be performed, and the required format for the list that is returned - in your particular example, if each list of three elements is nested within a parent list, this list would be supplied to mapcar as a single argument and would not return the same result:

_$ (mapcar 'list '((0 1 2) (3 4 5) (6 7 ))
(((0 1 2)) ((3 4 5)) ((6 7 ))

Hence the apply function is used to construct an expression equivalent to:

(mapcar 'list '(0 1 2) '(3 4 5) '(6 7 )

Link to comment
Share on other sites

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