Here is my collection of signatures used in newsnet postings:
(defun remove (item from) (cond ((atomp from) from) ((equal (car from) item) (remove item (cdr from))) (t (cons (car from) (remove item (cdr from))))))This is a very special one, I like very much.
Basically its a recursive lisp function to remove an item from a list, which handles recursive sublists too. (usually called trees, but lists in lisp are generally trees, plain lists without sublists are not so common in lisp).
(defun remove (item from) (cond ((atomp from) from)This stops recursion if (cdr from) is nil. (atomp) checks for the empty list nil, which is in AutoLISP!! an atom (not in other lisps) and for invalid 2nd arguments.
((equal (car from) item) (remove item (cdr from)))This checks if the first item in the list "from" is equal to "item". If so remove the "item" from the rest of the list, but dont return the head (the fount item). This is the actual remover.
(t (cons (car from) (remove item (cdr from))))))The first item in the list "from" is not equal to "item", so continue the remove process with the rest of the list, but return the head (the fount item) too. This is the usual hit.
(defun tail (l n) (cond ((zerop n) l) (t (tail (cdr l) (1- n)))))Another funky lisp recursion, this time to extract the tail of a list. for example to get (3 4 5) from (1 2 3 4 5) you call
(tail '(1 2 3 4 5) 2)
It must be called with non-negative numbers, otherwise you'll wait in an endless loop. It cdr's the list n-times. And its tail-recursive.
The corresponding (head) function has to use a helper function to be tail-recursive. See in the winston/horn examples for its definition.
(defun member (item l) (cond ((null l) nil) ((equal item (car l)) l) (t (member item (cdr l)))))This is how the (member) function could be written, for example for a fuzzy (member) use equal with a third fuzzy parameter. (this is how my gl_member is implemented)
(member) checks if the item is in the list. If so return the rest of the list including the found element, else return the empty list.
Here (null l) stops recursion instead of (atomp l), basically the same. but (null) will only check for empty lists not for invalid 2nd argument.
((equal item (car l)) l)if its found return the list (or what's left from the list)
(t (member item (cdr l)))))or if its not found, search for it in the rest of the list.
(defun sym2str (symb / f str tmp) (setq tmp "$temp.ac$" f (open tmp "w"))(princ symb f) (close f) (setq f (open tmp "r") str (read-line f) f (close f)) str)This is only understandable for AutoLISP programmers. This is the only way to convert a symbol to a string. A function CL (Common Lisp) uses daily but is named different.
You have to open a temp. file, write out the symbol with (princ) and read it back in with (read-line). There may exist shorter versions without closing the temp file, but this may exhaust the limited DOS FILES.
Note also how AutoLISP manages (symb / f str tmp) to define local variables. CL declares it with &aux instead of /
#!/bin/perl -s-- -export-a-crypto-system-sig -RSA-3-lines-PERL $m=unpack(H.$w,$m."\0"x$w),$_=^Echo "16do$w 2+4Oi0$d*-^1[d2%Sa 2/d0<X+d*La1=z\U$n%0]SX$k"[$m*]\EszlXx++p|dc,s/^.|\W//g,print pack('H*',$_)while read(STDIN,$m,($w=2*$d-1+length$n&~1)/2) -> http://www.dcs.ex.ac.uk/~aba/rsa/This is something I didn't wrote. A tiny crypt-perl which is so strong that exporting it from the states would be forbidden. Therefore I encourage using strong encryption, encryption that cannot be cracked not just by the nsa. Or do you want to hear that your state department sniffs your mail? Just surf to http://www.dcs.ex.ac.uk/~aba/rsa/ and you'll understand it.
(defun union (x y) (cond ((null x) y) ((member (car x) y) (union (cdr x) y)) (T (cons (car x) (union (cdr x) y)))))Another recursive lisp bummer!
(defun remove (x y) (apply 'append (subst nil (list x) (mapcar 'list y))))This is the winner of the great lisp puzzle contest by Tony Tanzillo on news:comp.cad.autocad 10/11/96 It deletes any expression from a list.
The most-compact solution possible involves the use of only five
AutoLISP functions, and the body of the (list-delete) function
contains only one expression.
Four of the five functions used are APPLY, MAPCAR, SUBST, and LIST.
There is one more which I will not disclose.
The first one of those listed above who emails me the most-compact
solution possible wins.
Four of the five functions used are APPLY, MAPCAR, SUBST, and LIST. There is one more which I will not disclose.
The first one of those listed above who emails me the most-compact solution possible wins.
(defun list-transpose (l) (apply 'mapcar (cons 'list l)))Douglas Wilson posted on comp.cad.autocad:
Once again, as a diversion, I pose this interesting LISP problem for those interested: write a function to 'transpose' a list e.g. For a list of 3D points, convert the list ((x1 y1 z1)(x2 y2 z2)...) to ((x1 x2 ...)(y1 y2 ...)(z1 z2 ...)) or for 2D points convert ((x1 y1)(x2 y2)(x3 y3) ...) to ((x1 x2 x3 ...)(y1 y2 y3 ...))The above sig was his solution, really cool.
It should also work for 4, 5, or more or even 1 dimensional data. No (for,while,repeat) loops, no ifs or conds. Forgive me if no one is interested, or if an answer is commonly known, but this kind of problem is one of the reasons LISP is so cool.
Though Tony Tanzillo posted:
Sheesh. Got anything harder than this?
(defun list-transpose (l)
((not l) nil)
(t (cons (mapcar 'car l)
(list-transpose (mapcar 'cdr l))