Friday, September 19, 2014

List Functions


In most programming languages they work with arrays. In AutoLISP there are no arrays. But you have lists in AutoLISP.


In fact. Lists can perform the same function as arrays. To learn more about it, take a look at my blog. I have written about it.


But let's talk about lists. There are lists functions. There are 18 list functions. Here is a list of them.


- List
- Append
- Cons
- Atom
- Listp
- Length
- Car
- Cdr
- Caar, Cadr, Cadar, Etc.
- Last
- Member
- Nth
- Reverse
- Assoc
- Subst
- Foreach
- Apply
- Mapcar


LIST


(list <expression>...)


This function makes a list of a loose element or loose elements.


Examples:


Function
Gives back




(list 'a 'b 'c)
(A B C)
(list 'a '(b c) “text”)
(A (B C) “text”)
(list 3.9 6.7)
(3.9 6.7)


APPEND


(append <expression>...)


This function creates a new list out of two or more lists.


Function
Gives back




(append '(a b) '(c d))
(A B C D)
(append '((a) (b)) '(c))
((A) (B) C)


CONS


(cons <new> <list>)


A new element is added to the list. The new element can be an atom or a list. It comes at the first place of the list. Examples are:


Function
Gives back




(cons 'a '(b c d))
(A B C D)
(cons '(a) '(b c d))
((A) B C D)


Instead of a new element and a list, this function can also be used with two atoms. Then you will get a dotted pair. Examples:


Function
Gives back




(cons 2 'a)
(2 . A)
(cons 8 “0”)
(8 . “0”)


ATOM


(atom <item>)


This function gives back nil if the item is a list. If not it gives back T or true.


Examples:


(setq a '(x y z))
(setq b 'a)


Function
Give back




(atom 'a)
T
(atom a)
nil
(atom 'b)
T
(atom b)
T
(atom '(a b c))
nil


LISTP


(listp <item>)


This function is the same as the prevision function. Except it gives back T if the item is a list and nil if it is not a list.


Function
Gives back




(listp '(a b c))
T
(listp 'a)
nil
(listp 3.434)
nil


LENGTH


(length <list>)


This function gives the number of element of a list. Or the length of a list.


Function
Gives back




(length '(a b c d))
4
(length '(a b (c d))
3
(length '())
0


CAR


(car <list>)


This function gives back the first element of a list. What is given back can be an atom or a list. If the list is empty, nil is given back.


Examples:


Function
Gives back




(car '(a b c))
3
(car '((a b) c)
2
(car '())
nil


CDR


(cdr <list)


This function gives back the list without the first element. The first element can be an atom or a list.


Examples:


Function
Gives back




(cdr '(a b c))
(B C)
(cdr '((a b) c)
(C)
(cdr '())
nil


CAAR, CADR, CADAR, etc.


These functions are a combination of the previous functions. You can combine the functions until four levels.


LAST


(last <list>)


The function gives back the last element of a list. Examples:


Function
Gives back




(last '(a b c d))
D
(last '(a b (c d))
(C D)


MEMBER


(member <expression> <list>)


This function checks if the expression is in the list. If so the list is given back starting with the expression.


Function
Gives back




(member 'c '(a b c d e))
(C D E)
(member 'z '(a b c d e))
nil


NTH


(nth <list>)


This function gives back the nth number minus 1 of the list. The elements of the list are numbered., The number of the first element is zero.


Function
Gives back




(nth 3 '(a b c d e))
D
(nth 0 '(a b c d e))
A
(nth 9 '(a b c d e))
nil


REVERSE


(reverse <list>)


This function turns the order of a list around. Example:


Function
Give back




(reverse '(a b c))
(C B A)


ASSOC


(assoc <item> <list)


This function gives back a part list of a list depending on whether the item has been found in the list.


Examples:


(setq ls '((name box) (width 100) (height 200))


Function
Gives back




(assoc 'name a)
(NAME BOX)
(assoc 'width a)
(WIDTH 100)
(assoc 'length a)
nil


SUBST


(subst <new item> <old item> <list>)


This function looks in a list for an old item and replaces it with the new item. If the anew item can be found.


If the old item is not found, then the original list is given back.


Examples:


(setq dm '(a b (c d) b))


Function
Gives back




(subst 'qq 'b dm)
(A QQ (C D) QQ)
(subst 'qq 'z dm)
(A B (C D) B)
(subst 'qq (c d) dm)
(A B QQ B)
(subst '(qq rr) 'c d) dm)
(A B (QQ RR) B)


FOREACH


(foreach <name> <list> <expression>)


This function goes through a list. Every item is given a name and the name is evaluated according to the expression.


The result of the last item and expression is what is given back by the fucntion.


Example:


Function
Gives back




(foreach nm '(1 2 3) (* nm 2))
6


In the example the following calculations have been made:


(* 1 2)
(* 2 2)
(* 2 3)


APPLY


(apply <function> <list>)


With this function elements of a list can be evaluated. Examples:


Function
Gives back




(apply '+ '(1 2 3))
6
(apply 'strcat '(“Auto” “LISP”)
AutoLISP


MAPCAR


(mapcar <function> <list1>...<listn>)


This function evaluates the function specified with all the lists as arguments. In the lists must be the number of needed arguments.


Example:


(setq a 10 b 20 c 30)
(mapcar '1+ (list a b c))


gives back:


(11 21 31)


That is:


(1+ a)
(1+ b)
(1+ c)


(mapcar '+ (10 20 30) (4 3 2))


gives back:


(14 23 32)


That is:


(+ 10 4)
(+ 20 3)
(+ 30 2)


1 comment:

  1. Play Slots at Sands Casino
    The Sands Casino 샌즈카지노 is home to some of the biggest names in Las Vegas. From slot machines and table games febcasino to video poker, the Sands is 메리트 카지노 home to some of

    ReplyDelete