LISP:Symbols and packages in lisp


Published on

LISP:Symbols and packages in lisp

Published in: Technology, Business
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

LISP:Symbols and packages in lisp

  1. 1. Symbols and Packages in Lisp<br />
  2. 2. A lisp symbol is a data object that has three user-visible components:<br />The property list is the list that effectively provides each symbol with many modifiable named components.<br />The print name must be the string, which is the sequence of characters used to identify the symbol.<br />The package cell must refer to package object. A package is a data structure used to locate a symbol once given the symbols name.<br />A symbol is uniquely identified by its name only when considered relative to a package.<br />
  3. 3. overview<br />The property List<br />The Print name<br />Creating symbols<br />Packages<br />Translating strings to Symbols<br />Export and import symbols<br />Name conflicts<br />Built-in packages<br />
  4. 4. The property list<br />A property list contains two or more entries, with each entry associated with the key called the indicator.<br />A property list is very much similar to an association list in its purpose, but the difference is that a property list is an object with unique identity.<br />A property list is implemented as a memory cell containing a list with even number(possibly zero) of elements. <br />Each pair of elements in the list constitutes an entry: the first item is the indicator, the second item is the value.<br />When the symbol is created its property list is initially empty, properties are created by using get within a set form.<br />
  5. 5. get symbol indicator1 &optional default<br />Get searches the property list of the symbol for an indicator eq to indicator1. if one<br /> is found, the corresponding value is returned, else default is returned.<br />Suppose the property list of foo is ( bar t baz 3 hunoz “huh?”)<br />Then ex: (get ‘foo ‘baz)3<br /> (get ‘foo ‘hunoz)”Huh?”<br /> (get ‘foo ‘Zoo) nil<br />
  6. 6. remprop symbol indicator1<br />This removes the symbol from the property with an indicator eq to indicator1.<br />Suppose if property foo was (color blue height 6.3)<br />After using (remprop ‘foo ‘height) the property becomes (color blue)<br />Returns the list that contains the property pairs of the symbol, the contents of the property lists are extracted and returned.<br />symbol-plist symbol<br />
  7. 7. getf place indicator1 &optional default<br />getf searches for the property list stored in place for an indicator eq to indicator1, if found returns the corresponding value, else returns the default or nil.<br />remf removes from the property list stored in the place the property with an indicator eq to indicator1 . The property indicator and the corresponding value are removed by destructively splicing the property list.<br />remf place indicator1 <br />
  8. 8. get-properties place indicator-list<br />get-properties searches the property list stored in place for any of the indicators in the indicator-list untill it finds the first property in the property list.<br />Get-properties returns three values:<br /><ul><li>The first two values are indicator and the value of the first property whose indicator was in indicator list.
  9. 9. The third is the tail of the property whose tail is the indicator.</li></li></ul><li>The Print Name<br />Every symbol has an associated string called the print name used for its external representation.<br /> returns the print name of the symbol syn.<br />Ex: (symbol-name ‘xy) “XY”<br />symbol-name syn<br />
  10. 10. Creating Symbols<br />Symbols are used in two different ways:<br />An interned symbol is one that is indexed by its print name in its catalog called a package.<br />An uninterned symbol is one that is used simply as a data object with no special cataloguing.<br />An uninterned symbol is printed with #: followed by its print name.<br />make-symbol print-name creates a new uninterned symbol whose print name is the string print-name.<br />
  11. 11. copy-symbol sym &optional copy-props returns a new uninterented symbol with the same name as of the sym.<br />If copy-props is non-nil, then the initial value and function definition of the new symbol will be the same as those of the sym, and the property list of the new symbol will be a copy of sym’s.<br />gensym &optional x gensym invents a print name and creates a new symbol with that print name. it returns new uninterented symbol.<br />The invented new print name consists of a prefix( which defaults to G) , followed by decimal representation of a number.<br />The number is increased by 1 every time gensym is called.<br />
  12. 12. gentamp &optional prefix package gentemp like gensym, creates and returns new symbol. The only difference is gentemp interns the symbol in the package.<br />Symbol-package sym returns the contents of the package cell of that symbol. This will be a package object or nil.<br />Keywordp object returns true if the argument is a symbol and that symbol is a keyword.<br />
  13. 13. Packages<br />A package is a data structure that is used for mapping the print names (strings) to symbols.<br />The string-to symbol mappings available in the given package are divided into two classes: <br /><ul><li>external( to be chosen carefully and are advertised to users of the package)
  14. 14. internal( normally hidden from other packages).</li></ul>A symbol is said to be interned in a package if it is accessible in that package and also is owned.<br />
  15. 15. Consistency rules<br />In dealing with package system, it is useful to follow the consistency rules:<br /><ul><li>Read-read consistency: reading the same print name always results in the same(eq) symbol.
  16. 16. Print-read consistency: An interned symbol always as a sequence of characters that, when read back in, yields the same (eq) symbol.
  17. 17. Print-print consistancy:if two interned symbols are not eq, then their printed representations will be different sequence of characters.</li></li></ul><li>Package names<br />Each package is assigned a name when it is created, though can be changed later.<br />The function find-package translates a package name or nickname into associated package.<br />The function package-name returns the name of the package.<br />The function package-nicknames returns the list of all the nicknames for a package.<br />
  18. 18. Translating strings to symbols<br />Whatever packet object is currently the value of *package* is referred to as current-package.<br />When the lisp reader has obtained a string of characters thought to name a symbol, that name is looked up in the current package.<br />If the name is found, the corresponding symbol is returned, if the name is not found, a new symbol is created for it and is placed in the current package as an internal symbol.<br />External symbol in some other package is referred through the qualified name, consisting of the package name, then a colon, then the name of the symbol.<br />Ex: editor: buffer refers to external symbol buffer accessible in the package named editor.<br />
  19. 19. The following four symbol qualifying syntaxes are used:<br />foo: bar look up bar among the external symbols of the package named foo, printed when the symbol bar is external in its home package foo and is not accessible in the current package.<br />foo::bar interns bar as if foo is the current page, printed when symbol bar is internal in its home page foo, and is not accessible in the current page.<br />:bar interns bar as the external symbol in the keyword package and makes it evaluate to itself. Printed if the home page of the symbol bar is keyword.<br />#:bar creates a new uninterned symbol named BAR. Printed when the symbol BAR is uninterented(has no home package )<br />
  20. 20. Exporting and Importing Symbols<br />Symbols from one package may be exported to imported from other packages by the use of functions export and import respectively.<br />(import ‘editor:buffer) takes the external symbol named buffer in the editor package and adds it to the current package as an internal symbol.<br />A symbol is said to be shadowed by another symbol in some package if the first symbol would be accessible by inheritance if not for the presence of the second symbol.<br />
  21. 21. To import a symbol without the possibility of getting the error because of shadowing use the function shadowing-import.<br />Use package causes a package to inherit all of the external symbols from some other package.<br />Unlike import, use-package does not cause any of the new symbols to be present in the current package, but makes them accessible by inheritance.<br />
  22. 22. The function export takes the symbol that is accessible in some specified package, and makes it an external symbol of that package.<br />If the symbol is directly present as an internal symbol, the it is imply changed to external status.<br />If the symbol is accessible via use-package, the symbol is first imported to the package and then exported.<br />If the same name is being used more than once name-conflicts may occur, so care has to be taken to avoid any such name conflicts.<br />
  23. 23. Built-in Packages<br />Common lisp built-in packages:<br />Lisp contains the primitives of the common lisp system.<br />Its external symbols include all of the user defined functions such as car, car and *package*.<br />user package is the current package at the time a Common Lisp system starts up. <br />common-lisp package contains the primitives of the ANSI commomLisp system.<br />common-lisp-user is by default the current package at the time, an ANSI common Lisp system starts up. <br />
  24. 24. Built-in packages<br />Keyword package contains all the keywords used by built-in or user-defined Lisp functions.<br />System package name is reserved to the implementation. <br />This is used to contain names of implementation-dependent system-interface functions. This package uses lisp and has the nickname sys.<br />
  25. 25. Visit more self help tutorials<br />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<br />