Upcoming SlideShare
×

# LISP: Data types in lisp

7,132 views
6,813 views

Published on

LISP: Data types in lisp

Published in: Technology, Education
3 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
7,132
On SlideShare
0
From Embeds
0
Number of Embeds
39
Actions
Shares
0
0
0
Likes
3
Embeds 0
No embeds

No notes for slide

### LISP: Data types in lisp

1. 1. Data Types in Lisp<br />
2. 2. Overview<br />Numbers<br />Characters<br />Symbols<br />Lists and conses<br />Arrays<br />Hash tables<br />Functions<br />Data structures<br />
3. 3. In Lisp, data type is possibly a set of lisp objects.<br />The set of all objects is defined by the symbol t.<br />The empty data type which contains no data objects, is defined by nil.<br />A type called common encompasses all the data objects required by the Common Lisp Objects.<br />The following categories of Common Lisp Objects are of particular interest: Numbers, characters, Symbols, Lists, arrays, structures, and functions.<br />Lisp data types<br />
4. 4. Numbers<br />Number data type includes all kinds of data numbers.<br />Integers and ratios are of type Rational.<br />Rational numbers and floating point numbers are of type Real.<br />Real numbers and complex numbers are of type Number.<br />Numbers of type Float may be used to approximate real numbers both rational and irrational.<br />Integer data type is used to represent common mathematical integers.<br />In Common Lisp there is a range of integers that are represented more efficiently than others; each integer is called a Fixnum. ( range: -2^n to 2^n-1, inclusive)<br />An integer that is not a fixnum is called bignum.<br />Integers may be noted in radices other than 10. The notation <br />#nnrddddd or #nnRddddd means the integer in radix-nn notation denoted by the digits dddd.<br />
5. 5. Numbers<br />Numbers<br />A ratio is the number representing the mathematical ratio of two integers.<br />Integers and ratios collectively constitute the Rational.<br />Rule of rational canonicalization: If any computation produces a result that is a ratio of two integers such that the denominator evenly divides the numerator, the result is immediately converted into the equivalent integer.<br />Complex numbers( type Complex) are represented in Cartesian form, with a real part and an imaginary part, each of which is a non-complex number. <br />Complex numbers are denoted by #c followed by a list of real and imaginary parts.<br />Ex: #c(5 -4), #c(0 1) etc.<br />
6. 6. Numbers<br />Numbers<br />A floating-point number is a rationale number of the form s.f.b^(e-p) where s is the sign(+1 or -1), b is the base(integer greater than 1), p is the precision, f is the significand ( positive integer between b^(p-1) and b^p -1 inclusive), e is the exponent.<br /> Recommended minimum Floating-point precision and Exponent Size.<br />
7. 7. Characters<br />Characters are of data objects of type Character. There are 2 subtypes of interest :standard-char and string-char.<br />A character object is denoted by writing # followed by the character itself.<br />Ex: #g<br />The Common Lisp character set consists of a space character#Space and a new line character#Newline, and the following 94 non-blank printing characters.(standard characters)<br />! “ # \$ % & ‘ ( ) + * , - . / 0 1 2 3 4 5 6 7 8 9 : ; < > ?<br />@ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ ] ^ _<br />‘ a b c d e f g h i j k l m n o p q r s t u v w x y z { } ~<br />
8. 8. Characters<br />Characters<br />The following characters are called semi-standard charecters:<br />#Backspace, #Tab, #Linefeed, #Page, #Return, #Rubout<br />Every object of type character has three attributes: code( intended to distinguish between character printed glyphs and formatting functions for characters), bits( allows extra flags to be associated with a character.) and font(specifies the specification of the style of the glyphs)<br />Any character whose bits and fonts are zero may be contained in strings.<br />All such characters together constitute a subtype of characters called string-char.<br />
9. 9. Symbols<br />Every object of type symbol has a name called its print-name.<br />Symbols have a component called the property-list or plist.<br />A symbol is notated simply by writing its name, if its name is not empty.<br />Ex: frobbboz, +\$, /user/games/slider<br />Other notating symbols are: + - * / @ \$ % ^ & _ < > ~ .<br />Writing an escape character() before any character causes the character to be treated itself as an ordinary character.<br />Ex: ( denotes character (<br /> +1 denotes +1<br />
10. 10. Lists and Cons<br />A cons is a record structure containing two components called the car and the cdr.<br />Conses are used primarily to represent Lists.<br />A list is a chain of conses linked by their cdr components and terminated by nil.<br />The car components of the conses are called the elements of the lists.<br />A list is notated by writing the elements of the list in order, separated by blank spaces and sorrounded by parenthesis.<br />Ex: ( a b c).<br />A dotted-list is one whose last cons doesn't have a nil for its cdr., after the last element and before the parenthesis is written a dot and the cdr of the last cons.<br />Ex: ( a b c. 4), ( a . d) etc..<br />In Lisp ( a b . ( c d)) means same as the (a b c d)<br />
11. 11. Arrays<br />An array is an object with the components arranged according to a Cartesian co-ordinate system.<br />The number of dimensions of an array is called its rank.<br />An array element is specified by a sequence of elements.The length of the sequence must be equal to the rank of the array.<br />One may refer to array elements using the function aref.<br />Ex: (aref foo 2 1) refers to element (2,1) of the array.<br />
12. 12. Arrays<br />Arrays<br />Multi-dimensional arrays store their components in row-major order<br />Internally the multidimensional array is stored as a one-dimensional array, with the multidimensional index sets ordered lexicographically, last index varying fastest. This is important in 2 situations:<br /><ul><li>When arrays with different dimensions share their contents, and
13. 13. When accessing very large arrays in virtual-memory implementation.</li></ul>A simple-array is one which is not displaced to another array, has no fill pointer, and is not to have its size adjusted dynamically after creation.<br />
14. 14. Arrays<br />Arrays<br />To create an array use the syntax:<br />Make-array dimensions &key :element-type<br /> :initial-element :initial-contents: adjustable:fill-pointer: displaced to: displaced-index-offset.<br />Ex: (make array ‘(4 2 3)<br />: initial contents<br /> ‘(((a b c) (1 2 3))<br /> ((d e f) (3 1 2))<br /> ((g h i) (2 3 1))<br /> ((j k l) (0 0 0))))<br />
15. 15. Vectors<br />One dimensional array is called vectors in lisp and constitute the type Lisp.<br />Vectors and Lisps are collectively considered to be sequences.<br />A general vectors is notated by notating the components in order sorrounded by #( and )<br />Ex: #( a b c d ), #( ), #( 1 2 3 ) etc.<br />
16. 16. Structures<br />Structures are instances of user-defined data types that have a fixed number of named components.<br />Structures are declared using the defstruct construct.<br />The default notation for a structure is:<br />#s(structure-name <br /> slot-name-1 slot-value-1<br /> slot-name-2 slot-value-2<br /> ……….)<br />Where #s indicates structure syntax, structure name is the name(symbol) the structure type, each slot-name is the name of the component and each corresponding slot value is the representation of the lisp object in that slot.<br />
17. 17. Hash tables provide an efficient way of mapping any LIST object (a key) to an associate object.<br />To work with hash tables CL provides the constructor function (make-hash-table) and the accessor function( get-hash).<br />In order to look up a key and find the associated value, (use gethash)<br />New entries are added to hash table using the setq and gethash functions.<br />To remove an entry use remhash.<br />Hash Tables<br />
18. 18. Hash Tables<br />Ex: (setq a (make-hash-table))<br /> (setf (gethash ‘color a) ‘black)<br /> (setq (gethash ‘name a) ‘fred)<br />(Gethash ‘color a)black<br />(gethash ‘name a)fred<br />(gethash ‘pointy a)nil<br />
19. 19. Hash Tables<br />Hash table functions<br />Syntax: make-hash-table &key :test :size :rehash-size :rehash-threshold<br />:test attribute determines how the attributes are compared, it must be one of the values( #’eq, #’eql, #’eql or one of the symbols(eq, eql, equal)<br />:size attribute sets the initial size of the hash table.<br />:rehash-size argument specifies how much to increase the size of the hash table when it becomes full.<br />:rehash-threshold specifies how full the hash table can be before it must grow.<br />Ex: (make-hash-table: re-hash size 1.5<br /> :size (* number-of-widgets 43))<br />
20. 20. Functions<br />Functions are objects that can be invoked as procedures: These may take arguments and return values.<br />A compiled function is a compiled-code object.<br />A lambda-expression (a list whose car is the symbol lambda) may serve as a function.<br />
21. 21. Data structures<br />CL operators for manipulating lists as data structures are used for:<br /><ul><li>Finding the length of the list
22. 22. Accessing particular members of the list
23. 23. Appending multiple lists together to make a new list
24. 24. Extracting elements from the list to make a new list.</li></ul>Common lisp defines accessor function from first to tenth as a means of accessing the first ten elements of the list.<br />Ex: (first ‘(a b c))A<br /> (third ‘(a b c))C<br />Use the function nth to access an element from the arbitrary list.<br />Ex: (nth 0 ‘(a b c))A<br /> (nth 1 ‘(a b c))B<br />
25. 25. Data structures<br />Data structures<br />To check if a particular object is lisp or not,CL provides Listp function.It always returns either T or NIL<br />Ex: (listp ‘(pontaic cadillac chevrolet))T<br /> (listp 99)NIL<br />The function Length is used to get the number of elements in List as an integers.<br />Ex: (length ‘(pontaic cadillac chevrolet))3<br />The member function is used to determine if the particular item is a member of the particular list.<br />Ex: (member ‘dallas ‘(boston san-fransisco portland))NIL<br />(member ‘san-fransisco’(boston san-fransisco portland))SAN-FRANSISCO PORTLAND<br />
26. 26. Data structures<br />Data structures continued..<br />Subseq is a part of the list used to return a portion of the List.<br />Ex: (subseq ‘(a b c d) 1 3)1 3<br /> (subseq ‘(a b c d) 1)b<br />The function append is used to append any number of lists to an already existing list.<br />Ex: (setq my-slides ‘(DATA TYPES))<br /> (append my-slides ‘(IN LISP))<br /> my-slides DATA TYPES IN LISP<br />The function cons is used to add a single element to the List.<br />Ex: (cons ‘a ‘(b c d))(A B C D)<br />
27. 27. Data structures<br />The function remove is used to remove the item from the list.<br />Ex: (setq data ‘(1 2 3 4))<br /> (setq data(remove 3 data))<br /> data(1 2 4)<br />Use the function sort with #’< to sort the list in ascending order and sort function with #’> to sort the list in descending order.<br />Ex: (setq data (sort data #’<))(1 2 4)<br /> (setq data (sort data #’>))(4 2 1)<br /> The function union, intersection and set-difference takes two Lists and computes the corresponding set operation.<br />Ex: (union ‘(1 2 3) ‘(7 8 1))(3 2 7 8 1)<br /> (intersection ‘(1 2) ‘(7 8 1))(1)<br />
28. 28. Property lists(plists)<br />Property lists are used to handle keyword-value pairs<br />Plists is a simple list with each with even number of elements. (keyword-value pairs)<br />Ex for a plist is: <br />(:michigan “lansing” :illinois “springfield”<br /> :pennsylavania “harrisburg”)<br />Getf function is used to access the members of plist.<br />Ex: (getf ‘(:michigan “lansing”<br />:illinois “springfield”<br />:pennsylavania “harrisburg”<br />: illinois)) “springfield”<br />
29. 29. Pick a tutorial of your choice and browse through it at your own pace.<br />The tutorials section is free, self-guiding and will not involve any additional support.<br />Visit us at www.dataminingtools.net<br />Visit more self help tutorials<br />