Jump to content

Recommended Posts

Posted
Can you please explain to me the procedure of codes they are running ?

 

What specifically do you want to know?

  • Replies 22
  • Created
  • Last Reply

Top Posters In This Topic

  • Michaels

    11

  • Kerry Brown

    9

  • Lee Mac

    3

Top Posters In This Topic

Posted
Another couple of 'massoc's as an example:

(defun massoc ( x lst )
 ;; © Lee Mac 2010
 (vl-remove-if-not
   (function
     (lambda (pair) (= x (car pair)))
   )
   lst
 ))

 

(defun massoc ( x lst )
 ;; © Lee Mac 2010
 (if lst                [color="red"];<-How could IF run since lst does not have a value ?[/color]
   (if (= x (caar lst))[color="red"];<-Also this one (x)[/color]
     (cons (car lst) (massoc x (cdr lst)))[color="red"];Here is the big issue...[/color]
     (massoc x (cdr lst))[color="red"];a very blind code to me[/color]
   )  ))

 

Thanks for valued help LEE.

Appreciated.

Posted

To help your understanding, I would suggest that you read around the topic of arguments for functions and how to pass arguments to functions. 'x' and 'lst' represent arguments to be passed to this function (in the same way that Kerry uses 'alist') when the function is called, data is bound to these symbols and then, when referenced, they point to that same data, as if they were used in a 'setq' call.

 

As for the explanation of the workings of the code:

 

(defun massoc ( x lst )
 ;; © Lee Mac 2010
 (if lst [color=Purple][i][b]; If there is a list[/b][/i][/color]
   (if (= x (caar lst)) [i][color=Purple][b]; Check the first element of the first dotted pair with the desired item[/b][/color][/i]
     (cons (car lst)[color=Purple][i][b] ; retain the item[/b][/i][/color]
           (massoc x (cdr lst))) [i][b][color=Purple]; process the rest of the list recursively[/color][/b][/i]
     (massoc x (cdr lst)) [b][i][color=Purple]; else process the rest of the list recursively[/color][/i][/b]
   )
 )
)

I would also suggest you read up on recursion, here is perhaps a good starting point:

 

http://pages.cs.wisc.edu/~vernon/cs367/notes/6.RECURSION.html

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