Jump to content

'(Recursion passing-function-lists other-quirks)


SOliver

Recommended Posts

Hi folks,

 

Whilst playing around with recursive list manipulation I took a bit of time experimenting with some of the quirkier aspects of Lisp and thought some of them pave the way for an interesting discussion thread on the forum.

 

Recursion
;sample list
(setq sample '((0 5 0) (10 12 16) (44 65 97)
                            (((0 0 0) (10 20 20) (1 1 3)
                                ((0 0 2)    (2 2 2)) (4 4 4)))))
(defun c:foo( /  )
 (bar  sample )
)
(defun bar(lst  / x)
   (mapcar
       '(lambda(x)
            (if(listp x)
                (bar x)
             (someFunction x)
       lst)
)

The bar method is pretty much a bog standard recursive function call itself when x is a lst and calling a function when it is not. Which isn't particularly interesting on it's own .

 

Passing function lists
;Recursively evaluate list for multiples of three, return t or nil in place of original ;value.
(defun c:foo( / f  )
 (setq f '(if(>(rem x 3) 0) nil t))
 (bar f sample )
)
(defun bar(func lst  / x)
   (mapcar
       '(lambda(x)
            (if(listp x)
              (bar func x)
              (eval func)))
       lst)
)

The new parameter func in the bar method is an unevaluated list. The idea with this is that you can pass a partial function to be evaluated on individual items. For me this is interesting because f's definition can be doesn't have to be a static method For example.

(defun c:foo( / f)
 (if(equal  (strcase (getstring "\nReturn remainder?(y/n)")) "Y")
   (setq f  '(rem x 3))
   (setq f '(if(>(rem x 3) 0) nil t))
 )
(bar f sample)
)

Which again is where the beauty of it is. Say you wanted to add handling for strings you could always extend the definitions of x to include a stringp arguement.

 

Finally on the recusion part. Say you have a script that creates a list that is in effect an object or structure definition; the listp is only checks that items are a list. Which could then be handled by extending the definition of bar again to encompass a validation method pass.

(bar '(...) 'isCustomObject sample)

(defun bar(func valid lst  / x)
   (mapcar
       '(lambda(x)
            (if(valid x)
              (bar func x)
              (eval func)))
       lst)
)
(defun isCustomObject( item /)
 ;;Some validation method
)

As an end the below code is a fun quirk of the eval.

(setq f1 '(if(= func f1) (setq func f2)(setq func f1)))
(setq f2 '(if(= func f2) (setq func f1)(setq func f2)))
(setq f f1)
(bar f 'listp sample)

This completely useless partial function (unless you like to gamble on the end function i suppose) just swaps the function list that will be called. The quirk of this is that it changes the value of func from inside the eval statement which is evaluating func.

 

Anyway I'm not condoning the use of any of this but I as commented above I thought my endevours might be of interest to the forum.

 

Also it would be good to see any other quirks that forum users have come across.

Edited by SOliver
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...