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)


Friday, September 5, 2014

Conversion Functions


For instance. We are working with an integer. And now we want to convert that integer into a string. We need a conversion function.


AutoLISP has got eleven conversion functions. Here they are:


(angtos <angle> [<mode> [<precision>]])


This function converts an angle into a string. The angle is a real number in radians. The result depends on the mode and the precision.


Mode Description


0 Degrees
1 Degrees/minutes/seconds
2 Degrees (400)
3 Radians
4 Land units


The precision argument give the number of decimals. The mode and precision argument are in the system variables AUUNITS and AUPREC.


Examples:


(setq p1 (list 500 133))
(setq p2 (list 240 133))
(setq an (angle p1 p2))


(angtos an 0 0) "180"
(angtos an 0 4) "180.0000"
(angtos an 1 4) "180d0'0"
(angtos an 3 4) "3.1416r"
(angtos an 4 2) "W"


(rtos <number> [<mode> [<precision>]])


Now a real number is converted into a text. The units and precision depend on the arguments for mode and precision.


Mode Description


1 Mathematical
2 Decimal
3 Feet and decimal inches
4 Feet and fractional inches
5 Divisions


The mode and the precision arguments can be found in the system variables LUNITS and LUPREC.


Examples:


(rtos 17.5 1 4) "1.7500E+01"
(rtos 17.5 2 2) "17.50"
(rtos 17.5 3 2) "1'-5.50"
(rtos 17.5 4 2) "1'-5 1/2"
(rtos 17.5 5 2) "17 1/2"


(itoa <integer>)


Now an integer is converted into a text.


Examples:


(itoa 33) "33"
(itoa -17) "-17"


(atoi <text>)


A text is converted into an integer.


Examples:


(atoi "97") 97
(atoi "-3") -3
(atoi "3.9") 3


(atof <text>)


A text is converted into a real number.


Examples:


(atof "97.1") 97.1
(atof "-3") -3.0


(fix <number>)


This function translates a number into an integer.


Examples:


(fix 3) 3
(fix 3.7) 3


(float <number>)


Here a number is translated into a real number.


Examples:


(float 3) 3.0
(float 3.75) 3.75


(list <expression>)


A list is created with a number of loose expressions. It is often used to create a 2D point or 3D point.


(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)


(ascii "text")


The ASCII value of the first character of the text is given.


(ascii "a") 97
(ascii "A") 66
(ascii "all") 97


(chr <number>)


The number is an ASCII value. The corresponding character is givne back.


Examples:


(chr 65) "A"
(chr 66) "B"
(chr 97) "a"


(read <text>)


This function gives back what has been read. The text cannot contain spaces. What is after the space is ignored.


(read "LISP") LISP
(read "A") A


Exercise:


Create an AutoLISP program, that creates the following parking spot. Do you see? The sizes for the length, width, and angle are given.








This is how the AutoLISP program works:


Command: PRKSP
Width/Length/Angle/<starting point>:W
Width <default>: width is entered
Width/Length/Angle/<starting point>:L
Length <default>: length is entered
Width/Length/Angle/<starting point>:A
Angle <default>: angle is entered
Number of parking spots <default>: number is entered


Take care. The AutoLISP program is going to be very big. So make separate functions for entering the width, length, and angle.


Exercise


Let's write an AutoLISP program. This is the drawing that is created by the AutoLISP program.




Two points are picked. The circle is drawn in one point. And as line goes to the other point, starting in the first point.


The user enters what text is to be drawn in the circle. And a box is drawn around the text. The TEXTBOX function is used.