Home

Lpp User`s Manual

image

Contents

1. classLS MySubType private MyType The type meta object for a MySubType is generated here genTS MySubType MyType 3 4 Accessing Type Meta Objects As mentioned previously all Lpp objects are of some type in the Lpp type hierarchy where each type has a Type meta object associated with it We call these meta objects because they contain the information about and type functionality of the ordinary Lpp objects they are associated with There is one type meta object for all ordinary Lpp object instances of that type These Type objects are first class Lpp objects and can be manipulated and accessed dynamically For example the print methods of specific types can be accessed and set dynamically First we show two functions for getting the type meta object itself typeOf object Function This function returns returns the type meta object for the type of the Lpp object object type name Macro This macro returns the type meta object named name Here are some examples Chapter 3 Subtypes 18 let typeList list type Integer type Symbol type MyType eq type0f L 23 first typelist gt t eq type0f S 23 second typeList gt t typeName object Function typeNameL object Function typeName returns a char representing the type print name of the object object For example let object S red typeName object gt Symbol The typeNameL function does the same but returns an Lpp String object A T
2. RETREAT mage es 13 LEPP pL NODEEINE neue RR 13 LPP S N DEFIME unis den XE ER REEF 13 LPP SL JNODBRINE 1 52 34 3 xe Ox XR Ry Ra teh 13 LPP True NODEEINE 3 ati ae ica de RE 13 NIL A eser pia ute QA done abe s O 27 DOUE EL nad eei murder ad doc ae Te die Tes 27 Concept Index Concept Index C circular list 4 s lio eee 49 Concept of nil 7 cies nb err d 27 cons cellsz 3 scade eR RAO bee lem eas 49 Converting between C and Lpp types 15 D Data base example 6 Debugging concepts 64 Dispatching functions 14 18 Dotted limita nt ERE s 49 E End marker list constructors 51 F File system information 61 Function objects 23 24 G Garbage collection concepts 65 H How touse ppt Caco ERE EV 6 I Implicit constructors 15 Installations firmes di nanas 4 Interning symbols 31 76 L Lisp equality concepts 29 Lpp Stream 1 O 57 N Name space clashes 11 Name space redefinitions 12 P Philosophy zia dus dt and ae OR EAR 1 Pretty printing cat tas Cp ee ele 59 proper St see dits min bee 49 Pure Lisp include file 10 R Read Print user interface
3. 48 It SASS ea C IE RUD ERA Eae Pe A9 LIL O wees 49 11 2 Last Operations 4 422 vase tis een i Gea tote eit eee 50 11 3 Using Lists as Sets ici taco en ee ie da hes 53 ll MAssociation Lb1sUs ov ebore beg er enger VR Get ers 53 12 Hash Tables cisma novas ss scsi 54 12 1 Hash Table Functions 54 12 2 Primitive Hash Function 55 l3 Stgsolsecnwe we CA See ace ee oak RES 56 13 1 String Comparison 56 13 2 String Construction and Manipulation 56 14 Input Outputcese s Er ERES 57 14 1 Printed Representation of Lpp Objects 57 142 Reading omm porch ue E Pede a 57 ASP A 58 15 File System Interface 61 T6 ETTOES E ie Ri De RS 63 i 17 Miscellaneous Features 64 17 1 Identity Function 64 172 Debugging Tools satis A RR RUP art at 64 17 3 Garbage Collection 65 18 Programming Cautions 68 18 1 Memory Leaks 68 18 2 Order of Evaluation 69 Appendix Appendix read print User Interface rpU D ico se b 70 Function l dex 26 eu ECRIRE 72 Variable Midex Liens ie se 75 Concept Index 4 b et x seat de 76 ii Chapter 1 Introduction 1
4. If the following code were allowed to loop while 1 4 x inc x y dec y then x would eventually transform from a SmallInteger to a BigInteger and y from 8 BigInteger back into a SmallInteger BigInteger objects can grow unlimited toward plus or minus infinity A Rational number can either be an Integer or a Ratio where a Ratio is nothing more than a pair of Integer objects The first of the pair being the numerator of the ratio and the second being the denominator A Ratio number can start off being read from an s expression stream or as the result of a math function let a readFromString 12 3 let w readFromString 21 22222222221111111111 24 let z divide x y As with Common Lisp in Lpp any computation or notation that produces a ratio such that the numerator and denominator can be evenly divided by an integer then the numerator and denominator are immediately converted to the divided results Or in other words the result is always immediately canonicalized So given the variables a and w in the above example print a gt 4 print w gt 7 7407407407037037037 Note that the canonicalization takes place immediately on the readFromString or divide above and not just on the print so that there never exists a non canonicalized rational number in Lpp 8 2 Predicates on Numbers These are predicate functions that test a condition of a single number argument and returns t if the condition tests true and returns nil o
5. A E 39 F PARC eet ese Ue 51 fileDirectory i v aee gp ste rS 61 finish utput i oo aii Y LPIIS ts 59 PITS Csi isk ee Reh ated whe oe eed a ne 51 O CIPIT EU 37 PLOOT CONS 5 2 5 6 0 ne nat Dr Vet ted 37 fourth odd toe his Mer batty A oles 2 51 TOMAS it De die Seek ent hein n bet iA 21 funca lia aaa la dla der 24 PUNCTLONP peta tae thes a RU es 29 G Ceci E dentur rcu NIE Puertos 65 gcConsElement 67 ECd A A TS 36 POLIST vou curd s EU Ger PIE US atenta 66 ECIree 2 oue E s tuse Sed Ras 66 EenTan usua eee dede ue dps and HU sus dox ed 17 AA i Fei dum dread Hes Y 17 Egethash ezio une Quee ed d ba ate aces au 54 getTypeEqual 3 s aca dude 19 getTypeEqualp 19 BOCTY POE RCs sip 2 pa cia ur 20 getlIypePrinl eue dun ke e dE sed 18 B tTyp PriNnC sii dal dde eed 18 pgreaterTham rss as codo eio 35 greaterThanOrEqual 39 H hashTableCount 55 hashTableCountL 55 hash Tab SP ys HS x asec Seats dete ak uU o E Eds 29 I Identity 0455 te ae ogee ta los ee ata dem dn 64 Le AS ent 15 ph ETE E 35 integerpe cu ee abi HUI ad 29 DIC ORM ED RC PES 31 L A fete n rpm dee eke es ae eh 15 Fast de ardent pme de Waa uiri 52 Length sn an Dl gu eee 44 Function Index lengthL c ad en dunque e igna 44 lessSThal ici ke peg s RN qd efe fed 35 lessThanOrEqual 39 ESB Qm
6. All Lpp objects whose prini and princ methods have not been set have default print methods which prints the object as description address where description describes the type of the object and address is the address in memory where the object is allocated defaultPriniAddress Variable The defaultPriniAddress global variable when non nil will cause the default printing of objects to print as lt description gt that is leaving out the address part This is useful for things like regression tests on program output where the same output is expected from one run to another The value of defaultPriniAddress defaults to t 14 2 Reading Lpp objects can be read and constructed from C input streams read Function read stream Function read reads one Lpp object from the C input stream stream and returns that object The syntax of such objects on stream is the same as Common Lisp except with no special dispatching characters such as ff or quote Lpp read does ignore semicolon comments to the end of line however If the stream argument is not provided cin is assumed For example suppose that we type to cin the following Chapter 14 Input Output 58 red Hello world 1 two 3 Then calling read three times will produce let symboli let stringl let listi read symbolp symbol1 gt t stringp stringi gt t listp listi gt t numberp first listi gt t list symboli stringl list1 gt
7. Constructs dolist el list Macro This macro loops through the Lpp list list with a given variable name el bound to the elements of the list The variable name el will be bound to the last element of the list on exit from the loop or else nil if the list list was empty For example the following function would print the given list1 entries in a single column void printListColumn listi 1 dolist e listi cout lt lt e lt lt endl dolist2 ell list1 el2 list2 Macro This macro loops through the Lpp lists ist and list with variable names el1 and el2 bound to the elements of the two lists The variable names el1 and el2 will be bound to the last element processed from the list on exit from the loop or else nil if the list list was empty If the lists are not the same length the shorter of the two lists terminates the loop 5 2 2 Mapping The list mapping functions all take a function argument fun as the first argument then 1 or 2 list arguments lists The function argument fun must have as many arguments as there Chapter 5 Control Structure 25 are list arguments The given list or lists are mapped over or into a new list using fun The function fun is successively applied to the list or lists and its return value except for mapc and mapl is used to construct a new list in the same order as the given lists For mapc and mapl fun is successively applied for side effect only and the original first list is just returned For mapcar ma
8. can easily be converted to the Lpp predicate with the pL macro if needed typep object type Function typep returns t if the object object type or any supertype is the Type meta object type and nil otherwise Note that typep is different from typels in that any supertype of the object satisfies the test Here are some examples of both let n L 56 pL typels n Integer gt t pL typels n Number gt nil typep n type Number gt t Chapter 6 Predicates 29 6 4 Specific Data Type Predicates symbolp object Function symbolp returns t if object is of type Symbol and nil otherwise consp object Function consp returns t if object is of type Cons and nil otherwise listp object Function listp returns t if object is of type Cons or nil and nil otherwise numberp object Function numberp returns t if object is of any type whose has a supertype of Number and nil otherwise rationalp object Function rationalp returns t if object is of any type whose has a supertype of Rational and nil otherwise integerp object Function integerp returns t if object is of type Integer and nil otherwise ratiop object Function ratiop returns t if object is of type Ratio and nil otherwise stringp object Function stringp returns t if object is of type String and nil otherwise characterp object Function stringp returns t if object is of type Character and nil otherwise functionp object Function functionp returns t if object
9. charCode returns an int that encodes the given Lpp Character character charCodeL does the same but returns an Lpp Integer object For example using the variable char1 above set to an Lpp Character object the following convert it to an Lpp Integer object a C int and then to a C char let chariInteger charCodeL char1 int charilnt charCode char1 int charilnt cL char1 char chariChar charCode char1 char chariChar cL chari Note that the results of the cL operator is exactly equivalent to the charCode function See Section 3 2 2 To C Primitive Types page 15 Chapter 9 Characters 42 9 3 Character Conversions character object Function character returns the Character object that object can be coerced to or signals an error Only Character Integer Symbol or String objects can be coerced to a Character object For example character L h gt m character L h gt h character L 104 gt h character S h gt h charUpcase character Function charDowncase characterz Function The given character must be a Character object If character is a lower case letter charUpcase returns the corresponding upper case letter Character object If chacacter is an upper case letter charDowncase returns the corresponding lower case letter Chacacter object Otherwise both just return character Chapter 10 Sequences 43 10 Sequences All Common Lisp functions that operate on sequences opera
10. determined by the define variable LPP_PRINTOSTRING_MAX If more characters are desired for some reason redefine LPP_PRINTOSTRING_MAX to a value bigger than 4096 before the include file Lpp hh in the Lpp library compile stream priniLength object Function priniLengthL object Function princLength object Function princLengthL object Function priniLength returns as an int the prin1 printed representation length of the Lpp object object priniLengthL does the same but returns the length as an Lpp Integer princLength and princLengthL are the counterparts for princ Here are some examples priniLength S hello gt 5 priniLength L hello gt 7 princLength L hello gt 5 priniLength L 1234 5 priniLength list L 1 L 2 gt 5 Chapter 15 File System Interface 61 15 File System Interface Lpp provides a small collection of file system interface functions that are somewhat similar to corresponding Common Lisp functions Since C provides a library of file stream functions these Lpp file system interface functions are not absolutely necessary They do however make it easy to bring the file system into Lisp semantics For example the fileDirectory function returns a Lisp list of file names currentDirectory Function currentDirectory returns an Lpp String that represents the environment s notion of the current directory that the Lpp program is executing in For example in Unix this is the current directory when main
11. eS bib 24 Predicates oo oa yo Se eae bee xa emi 27 GL Logical Values iso hie cherie i ee es eee ER e 27 6 2 V tables and Type Predicates 28 6 3 General Type Predicates 28 6 4 Specific Data Type Predicates 28 6 5 Equality Predicates 29 Sy Mbs cs cao nee ve deu e du ep REIN 31 El The Print Name Bu ELE AS Van 31 7 2 Creating Symbols iio nest EC IRE EE eee Es 31 8 Numbers e S 9 x9 XR Ree Y RUE ere 33 8 1 Number Types ec RR e eR RR deca bas ee 33 8 2 Predicates on Numbers 34 8 3 Comparison on Numbers 35 8 4 Arithmetic Operations 35 8 5 Component Extractions on Numbers 36 8 6 Efficient Specific Number Functions 38 O CG aracterS s serres Need 39 9 1 Predicates on Characters 40 9 2 Character Construction 41 9 3 Character Conversions 42 LO S quences ss aem rU RE UR A3 10 1 Simple Sequence Functions 44 10 2 Modifying Sequences 45 10 3 Searching Sequences 4T 10 4 Efficient Sequence Functions
12. is of type Function and nil otherwise hashTableP object Function hashTableP returns t if object is of type Hashtable and nil otherwise 6 5 Equality Predicates Lisp has taught us that there are several kinds of equality of objects In C the operator can only express one kind of equality Also without void casting it can not be applied to any two objects dynamically The operator in Lpp can be applied to any two Lpp objects dynamically and means the same as the eq function of Common Lisp However an eq function is still supplied so that an eq function object can be passed to some functions that take an optional predicate function argument Also note that eq as opposed to is a true Lpp predicate in that it returns either t or nil eq xy Function eq returns t if x is the same object as y and nil otherwise Chapter 6 Predicates 30 eql xy Function eql is the same as eq except that if x and y are Characters or Numbers of the same type their values are compared equal x y Function equal returns t if z and y are structurally similar isomorphic objects and nil other wise Roughly speaking t means that x and y print the same way equalp x y Function equalp returns t if z and y are are equal if they are characters and are eql ignoring alphabetic case if they are numbers and have the same numerical value even if they are of different types or if they have components that are all equalp Objects that have c
13. of user defined identifiers with underscores that can not possibly clash with Lpp identifiers Other characters not allowed in C identifiers are replaced by words so string becomes stringEqual In some cases this does not work in which case a reasonable alternative is chosen For example append is an identifier used in the C stream opera tions So the identifier listConcat was used instead of the Common Lisp specified append Another example is the Common Lisp specified delete which is a reserved identifier in C so nremove is used as in destructive remove In case 3 the Lpp macros reserve a small set of reserved identifiers mechanism is set up to redefine these per compilation unit by the programmer if a name clash happens to exist See Section 2 3 Redefining Predefined Names page 12 In case 4 all other reserved Lpp identifiers begin with Lpp so the user should never define a name that begins with Lpp 2 2 Returning C type names In some cases in Lpp where it might be useful and more efficient to return a primitive C type such as an int the Common Lisp borrowed function name is used since it is expected in C programs that that name will be used more frequently However a counterpart function is still needed to return the corresponding Lpp object such as an Lpp Integer object Such counterpart functions can also be coerced to a function object for use in funcall and apply In these cases if the Lpp function that returns
14. readFromString 13 4 gt 3 1 floorCons readFromString 13 4 gt 4 3 let a readFromString 5 3 let b readFromString 2 3 floor a b gt 3 floorCons a b gt 3 1 3 rem x y Function mod x y Function The rem function returns the same result that truncateCons of the same two arguments returns in its cdr The mod function returns the same result that floorCons of the same two arguments returns in its cdr For example comparing with the above examples of truncateCons and floorCons rem 13 4 gt 1 mod 13 4 gt 1 mod 13 4 gt 3 8 6 Efficient Specific Number Functions When performing operations on Lpp numbers there is a very small cost in efficiency to dispatch on the types of numbers that are passed as argments to a number function For example in the following plus x y the x and y arguments could be two SmallInteger objects a SmallInteger and a BigIn teger object a BigInteger and a SmallInteger object a SmallInteger and a Ratio object a BigInteger and a Ratio object etc See Section 8 1 Number Types page 33 This cost albeit small can be completely eliminated by casting specific types on the number arguments This can only work when the program knows absolutely what the specific types of numbers are occurring For example if in a program segment it is absolutely known that only BigInteger objects are occurring then the following plus asThe BigInteger x asTh
15. red Hello world 1 two 3 read read readFromString string Function The argument string is either a char string or an Lpp String object readFromString is the same as read but uses the string string instead of a C stream For example let symboli let stringi let listi readFromString 1 two 3 symbolp symbol1 gt t stringp stringi gt t listp listi gt t numberp first listi gt t list symboli stringi list1 gt red Hello world 1 two 3 14 3 Printing The Lpp printing functions emulate Common Lisp printing functions in that there are two ways to print Lpp objects to C streams with or without escape characters All Lpp objects automatically get two type dispatching printing functions for doing this The user can redefine these dispatching functions if need be See Section 3 4 Accessing Type Meta Objects page 17 prini prini princ princ print print object object stream object object stream object object stream readFromString red readFromString Hello world Function Function Function Function Function Function All of these functions print the Lpp object object to the C output stream stream If stream is omitted cout is assumed All of these functions return object as its value With prin1 escape characters are used as appropriate while princ prints object with no escape characters Roughly speaking the output of print is suitable for r
16. setValue S frank S age L 32 setValue S frank S siblings list S joe S mary addEntity S mary db db setValue S mary S name L Mary Ann Smith setValue S mary S age L 28 setValue S mary S brothers list S joe S frank cout Report on Smith family endl dolist person db getValue S smith family S people cout lt lt Person lt lt db getValue person S name lt lt endl lt lt is lt lt db getValue person S age years old endl dolist relation list S siblings S brothers let relatives db getValue person relation if relatives 4 cout lt lt With lt lt relation lt lt dolist relative relatives cout lt lt lt lt relative cout lt lt endl cout lt lt endl Notice there are several occurrences of S such as S joe This is the Lpp idiom for introducing symbols Symbols are useful since they are very efficient and have universal identity For example S frank refers to the same symbol frank every place that it is used in any program Also notice the occurrences of LO This is the Lpp idiom for introducing C primitive types into the Lpp world of objects L stands for Lpp conversion because in essence we are Chapter 1 Introduction 10 converting a C primitive value into an Lpp one For example L 42 generates an Lpp Integer whose value is 42 Finally notice that Lpp object
17. structure is an intrinsic part of Lisp Thus much of the extra data structure for doing garbage collection in Lisp exists for free and is more cogent than C as a starting point for garbage collection algorithms Since Lpp does not have this structure available under C and since there are many reasonably good C garbage collection algorithms available Lpp does not commit to any of these in principle nor provides any kind of automatic garbage collection Lpp does provide a facility for doing manual garbage collection much the same way delete works in standard C But it makes the garbage collection of complex Lpp data structures easier All Lpp garbage collection functions only serve to deallocate the storage of some data and then just return nil gc object Function gc garbage collects the Lpp object object Here is a trivial example let a L 1 let b L 2 let c plus a b assume c is needed from here on gc a gc b and a and b are not In general for predefined Lpp object types the object and all of its components are collected except for some exceptions listed in the next paragraph For user defined Lpp objects gc calls delete of the specific subtype class for object Since Lpp symbols have global import gc will do nothing if object is a Symbol Usually you do not want to remove symbols However in cases where you absolutely must the proper way to remove symbols and have them collected is to use unintern which re
18. the primitive C type is named Chapter 2 Name Space Issues 12 function the the counterpart that returns the corresponding Lpp type is named functionL The capital L is meant to connote that an Lpp object is the result For example the Common Lisp borrowed name length that returns the length of a sequence is used in Lpp to return an int instead of an Lpp Integer object int i length list and the counterpart function name is lengthL let i lengthL list let i funcall L lengthL list The length function is more efficient and does not generate an Lpp Integer object Whereas the lengthL does generate an Lpp Integer object if needed and as seen above can also be used as a function object whereas the primitive length can not See Chapter 4 Functions page 23 This is an unfortunate situation since it would be better to have the function such as length be overloaded on both the int returning function and the let returning function But some C compilers issue an ambiguous function name error in this case For efficiency concerns this is consistently done in all cases where the Common Lisp borrowed name of a function returns an integer In such cases where the Common Lisp function would return either a integer or nil then a 1 is returned for the int returning version For example the Common Lisp function list length returns the length of a given list as an interger but returns nil if the list is circular int i listLength li
19. was executed The returned String has a terminating directory identifier if such exists such that if the String were concatenated with a plain file name the result would be a complete file specification For example suppose on a Unix system the current directory is home me 1pp 1ib then stringConcat currentDirectoryO L filex gt home me lpp lib filex fileDirectory file Function Given a file specification file fileDirectory will return a list of names of the files in the directory if the given specification names a directory in the file system or a file specification relative to the current directory An error is signaled if the the given file can not be opened as a directory The file argument can be an Lpp String or a char string The returned list of names are Lpp Strings Here are some examples let dirList fileDirectory currentDirectory int dirLength length fileDirectory home me lpp lib test let filtered mapcan L filterFunction fileDirectory test Note that fileDirectory is similar to the directory function in Common Lisp But while the argument to directory is a file pattern string in Common Lisp the argument to fileDirectory is an exact directory name probeFile file pred Function Given a file specification file and a predicate symbol pred returns t if what pred indicates is true of the given file and nil otherwise The argument file is a file specification in the file system or a specification rel
20. we use Lpp symbols to designate entities We set relations in the data base with a typical entity attribute value tuple So the data base access member functions are as follows e getSize Returns the total number of entities in the data base e addEntity entity Add entity any Lpp object to the data base e setValue entity attribute value Add a relation for entity e getValue entity attribute Returns the attribute value for entity Here is the implementation of the data base class Chapter 1 Introduction 7 MI MB I TTT TTT TTT TTT TTT TTT TTT main cc Introduction Simple Data Base example include lt Lpp hh gt Data Base class class DataBase int size let contents public DataBase int getSize return size void addEntity let void setValue let let let let getValue let let Data Base constructor DataBase DataBase size 0 contents makeHashTable Add an entity to the Data Base void DataBase addEntity let entity if gethash entity contents 1 puthash entity contents 0 size Set the value of an attribute for given entity void DataBase setValue let entity let attribute let value let attributes gethash entity contents let old assoc attribute attributes if old rplacd old value else push cons attribute value attributes puthash entity contents attributes Return the value of an attribute for given e
21. what it was originally setTypePrinc type function Function getTypePrinc type Function setTypePrini type function Function getTypePrini type Function All Lpp objects have princ and print methods used by the Lpp printing family of functions and C stream IO of Lpp objects See Chapter 14 Input Output page 57 These functions allow the user to set or get the print method of a given type where type is a Type meta object The functions setTypePrinc and setTypePrin1 sets the Lpp function Chapter 3 Subtypes 19 object for the princ and print methods respectively The function argument is a function taking two arguments first an object of type let and second a stream of type ostream amp The function should return the first argument object as both princ and prini do The functions getTypePrinc and getTypePrin1 returns the last function set For example suppose that we had defined an Lpp class called MyClass that has two slots with accessors getSlot1 and getSlot2 Then the following code would set up a princ type printer for MyClass let princMyClass let obj ostream amp s s lt lt MyClass object sloti lt lt getSloti obj lt lt slot2 lt lt getSlot2 obj return obj let mci makeInstance MyClass 76 trombones cout mci endl Would produce lt Lpp Testclass gt setTypePrinc type MyClass L princMyClass Then cout lt lt mci lt lt endl Would produce instead MyClass object s
22. x and denominator returns the denominator Each function returns an Lpp Integer If an integer n is given as the argument then it is treated as if it was n 1 so that numerator returns n and denominator returns 1 For example let ratio divide 23 198 numerator ratio gt 23 denominator ratio gt 198 numerator numerator ratio gt 23 denominator numerator ratio gt 1 Chapter 8 Numbers 37 numeratorOf x Function denominatorOf x Function Since Lpp maintains Ratio objects as pairs of Lpp Integer objects it allows the ac cess of the actual numerator and denominator objects The functions numerator0f and denominatorOf are exactly the same as numerator and denominator respectively except for the fact that they do not make a copy but instead return the actual Integer object This is not necessary in Lisp but in C this allows operations on the parts of a Ratio without having to worry about deallocating the copies This is a useful efficiency however the programmer has the responsibility to not inadvertently change parts of a Ratio object which could break the rational canonicalization rule and even break the code For example let ratio readFromString 23 198 times numerator ratio denominator ratio Oops memory leak times numerator0f ratio denominatorOf ratio No leak dec numerator ratio OK doesn t change ratio dec numeratorOf ratio Oops does change ratio and breaks canonical
23. 1 Introduction 1 1 Overview Lpp Lisp Plus Plus is a library of Lisp like functions and macros usable in C programs The philosophy behind Lpp is to provide as close as possible the semantics and style of Lisp rather than try to force it to fit a static style of programming Lpp tries to emulate Common Lisp as much as possible in this regard By doing things this way part of the true power and flexibility of Lisp can coexist and mix with the static typing features of C even within functions and objects The hope is that Lpp will be useful for the following e Porting Lisp programs to and from C e Implementation of embedded AI sub systems in C environments e An alternative for Lisp programmers who need to program in C e When a C program needs dynamically typed objects e When a C program needs an efficient unified list implementation e When a C program needs rational numbers in the range of minus to plus infinity e When a C program needs to do symbolic processing such as manipulating s expressions symbolic math etc One of Lisp s advertised benefits is that of dynamically typed objects Standard C does not offer this capability Instead the programmer is expected to created virtual functions whose objects dynamically dispatch through the use of indirect pointers to v tables and then a v table has a pointer to the virtual function for that object While the basic idea of virtual functions is good relying only
24. 70 T LA CRI NN Ce ete 49 Type checking 21 Type hierarchy secs ido egre meh RD 17 Type meta object 14 17 U Uninterning symbols 32 W What ISSEPD gie e ERR RE HG de 1
25. C strings The names of such things as L the conversion macro True and S the symbol macro can be redefined per compilation unit by the programmer without affecting the operation of Lpp Lpp provides the basic Common Lisp I O functions like read print prini princ pprint etc but also provides that any Lpp object can appear in stream operators So if obj is the Lpp symbol object car then cout lt lt obj lt lt obj would print as obj car All Lpp objects have C stream print methods that inherit from the basic princ method of an object All new Lpp objects defined automatically get default dispatcher functions defined for princ and prin1 New princ or prini methods can be set dynamically by the programmer for Lpp type meta objects Lpp provides mathematically correct rational numbers ie ratios whose numerator and denominator and integers are in the range minus to plus infinity Lpp numbers automati cally expand or shrink to any size when operated on by Lpp math functions Overflow or underflow are impossible Chapter 1 Introduction 3 Lpp provides two debugger functions pdb and pdc for examining Lpp objects in a de bugger pdb uses prin1 and pdc uses princ So while in a debugger the user can type for example p pdc list1 where list1 is some variable in a program that contains an Lpp list the list and all of it s subcomponents will be printed exactly as you would expect in a lisp interpreter In addition the function p
26. EN MIRE oat actus ett e ios Bink Le 51 TEST CONCAT 2 eI puse eed d 52 listEM LR bru di ded E 51 ITSELF th aria Mere ne bags 52 listL ngthLb 3 24135 4e edo didus 52 VETS CD ia none on du at send dad Ts age ro x Uere 29 USE OEMs tr crs Bees enc Rte se Doth ae ie a 51 LowerCaseP nanas ut teurs urn 40 M makeHashTable 54 make Instance mica es ut tes des ns its 17 makeString ti se ee exa 56 MAP as nee nt tn dede Meur 25 MAP ei id ns 25 E A O sado en gee egy 25 MAPCON rta ues a O a eae eh 25 Maphashy cet rueda tato oA 55 MAP REUS RAT EE 25 MapStats AL eR US CIA ud 25 penbe eea see er meats once s voile Dada ete 53 MINUS nodu aient RA IR RU ol IUE ERO ep 35 MID SP Secta rente OPER p A 34 MOG RSA E A AE One LEE 38 N e te Mies ala see Ale ss 52 negate is vei es ER aede ca d deseas er d 36 DYDtho nos isganeeecoeR g E RUE PME wa eS 51 nremove n idc t din s ER CONES een 45 preverse aa uae pepe am ges Rege get 45 TCH PPP 50 D C eee ten edel t beer ee edes 51 Dio td ERU Ae ERR IR 28 numb6erp 5e xL nea 29 numeratOr uci esate Ne GERE eee ree en 36 numerator f ole epe EE en 37 O Ji Sih e bosses erdt pro inae 59 oddpuivlemes voee SN UNIO 34 P A eue xau deed es 64 A ubt t Len epe 64 plie denses o air porn eee Dee 16 PLUS A EE ea Re ue whee ee 35 PLUS athens E d we Ne sen 34 POP ie eds a es be ad aes acai 52 position E eM odeurs 47 positione ieii Se eR ERA EE 47 73 positionT
27. Lpp User s Manual Copyright 1997 2003 William Paul Vrotney Table of Contents 1 Introducir ee v4 iX 39 1 LL as eoncueIiebtehenA etc d e Uu eue 1 12 Installing pps tata tac iaa 4 1 3 Usine Epp cu ue er tod RENE E Ma Tee RE ors 6 1 4 A Simple Data Base Example 6 1 5 Pure Lisp Include File 10 Name Space Issues 11 2 1 Naming Conventions 11 2 2 Returning C type names 11 2 3 Redefining Predefined Names 12 Subtypes sass ose ease eae awed wae cerea a lae 14 3 1 Predefined Subtypes 14 3 2 CONVETSIONS A Debut mea es 15 3 2 1 From C Primitive Types 15 3 2 2 To C Primitive Types 15 3 3 Defining New Subtypes 16 3 4 Accessing Type Meta Objects 17 3 5 Accessing Subtypes 20 3 6 Type Checking reiso reri e EU ERI UE RE 21 PUNCHIONS 1522050302 neesbenan XE beue 23 Control Structure si ses sembler ouest 24 5 1 Function Invocation 24 5 2 J eratiofie i o acte t Pan aed aes ete E a a 24 5 2 1 Iteration Constructs 24 5 2 2 Mapping 3 mti RP ea da Bae
28. Lpp hh all the time it is always perfectly safe Chapter 2 Name Space Issues 11 2 Name Space Issues 2 1 Naming Conventions The Lpp library tries to prevent name space clashes with other libraries so that it can be included with such other libraries All the identifiers reserved for Lpp fall into one of the four following categories 1 The Lpp class and Lpp subtype class names 2 Lpp member function identifiers 3 Lpp macros 4 The rest of Lpp reserved identifiers that begin with Lpp_ In case 1 Lpp class and subtype class names are few and correspond directly with names in the Common Lisp type hierarchy except that the Lpp class names begin with a capital letter For example Number for the Lpp number object Integer for the Lpp Integer object String for the Lpp string object etc In case 2 the Lpp library creates its own name space of functions that dispatch on the let type For example list x y where x and y are of type let Generally this prevents clashes with other library names Lpp strives to provide the Common Lisp specified semantics and wherever possible func tions are named for their Common Lisp counterparts and replacing dashes by capitalizing the first character after the dash For example the Common Lisp function make string becomes makeString This was decided over replacing dashes with underscores since such identifiers with capitals would more likely be unique and except for the prefix Lpp allows a whole set
29. Lpp hh is included Renames True define LPP True NODEFINE 1 define XX Lpp True Renames Nil define LPP Nil NODEFINE 1 define XX Lpp Nil Renames 0 define LPP 0 NODEFINE 1 define XX Lpp_0 Renames S define LPP S NODEFINE 1 define XX Lpp_S Renames L define LPP_L_NODEFINE 1 define XX Lpp_L Renames cL define LPP cL NODEFINE 1 define XX Lpp cL Renames iL define LPP iL NODEFINE 1 define XX Lpp iL Renames pL define LPP pL NODEFINE 1 define XX Lpp pL Renames sL define LPP sL NODEFINE 1 define XX Lpp_sL Renames EM define LPP EM NODEFINE 1 define XX Lpp EM Chapter 3 Subtypes 14 3 Subtypes Lpp achieves dynamic typing capability by having all Lpp objects be of type let The type let is actually an Let where Let refers to the base class for all specific Lpp types like Symbol Cons Integer etc The actual base class of Lpp objects is abstracted away by the let and Let type definitions Thus while working with Lpp objects all objects are considered to be one cell pointers However since the programmer never uses Lpp or Lpp the concept of pointers is abstracted away with the use of let So it is also correct to refer to Lpp objects as let objects objects of type let Furthermore all computational aspects of pointers is encapsulated in the Lpp classes This means that in essence the programmer never has to deal with pointers while working specifically with let objects Th
30. MyClass then let myClassEqual let oi let o2 return equal the MyClass o1 sloti the MyClass 02 slot1 let mci makeInstance Myclass foo let mc2 makeInstance Myclass foo equal mci mc2 gt nil setTypeEqual type Myclass L myClassEqual equal mci mc2 gt t setTypeEqualp does the same thing as setTypeEqual except that when equalp is called given two objects where the first object is of the type type then the function function is automatically dispatched on the object Given both objects the function should return t if the objects are considered to be equalp and nil otherwise getTypeEqual and getTypeEqualp will return the last equal equalp dispatcher functions respectively set for the given type If a type meta object does not have a equality dispatcher function set this way then eq will be used as the default setTypeExt type extension Function getTypeExt type Function The Lpp user can set up his own Type meta object extension to any Lpp Type meta object He first defines an Lpp class that will be the meta object extension class and then instantiates a object of that class which he then sets in the desired Type meta object by calling setTypeExt on the desired Type type and meta object extension is that newly instantiated object getTypeExt would return the last extension set in the given type meta object As an example this is useful when over some number of classes the user wants to dispat
31. P EU E uei Dit ak Eidos 15 standardCharP 151m er ee tees 40 stringConcat 56 stringEqual evene vs 56 stringEQUAL 56 Stringpocqcthiveisd een ue Ure a f Pas aed 29 S bSeqi c uua ctn BHA UR Eee deua 44 sxhash c fs DDR LIES led ie a ee 55 SkhaShli iss ose take Moat adele has Did 55 synbolName sitio eta okie Heals acts 31 SYMDOL Pied athena ade pe RR EP etus 29 Function Index T tenth me ERE E SRI NP DE Yd e bane 51 UGF DII a ROS 59 ChE ex rie ES RSEN GI WM den dis 20 theUrNil4 gone ou ELSE ND ARES 20 third cud AE supe EPI RSS Men 51 CINESE nee oa a ue e dd eg 35 trincate sii n2 enr ls Mn ar ui a a aire ROT AS 37 truncateCOnS 52 net abere 37 PEA a Sem EIU NE SEE EU E QE 17 VtYDelsS iia Pa nee CEU VD eres 28 typeName z scere ted duke iesu RET 18 74 typeNameL 18 tyDeUfiz Sais oe ap Pees AT eta he Mee E 17 CY PCP iterate ses Pan Sa eee Ds te Gs un 28 unlntern 0 octal e eal de sR ead 32 upperCaseP 2 060 y id lee Sa Ue dong 40 Z Variable Index Variable Index defaultPriniAddress 57 EM gene une po Sato sd eels e ttl tes 51 LPF EL NODEP UNE ooa s Pie ERU R ERE 13 LPP EM NODEFINE lt 5 805 weed or RR RR SR 13 LPP TL NDDEEINE cos pie hy reru Stn ed dde ORE 13 LPPCOL NODEEINE cicle lia 13 LPP Nil NODEF INE 040504 hee ars 13 75 LPEIND DYPE UHECOK uae eu nie do nes 21 LPP D NODEFIME
32. S name Macro This macro returns an Lpp Symbol with print name name The name argument is not escaped with double quotes Note that for some sequence of characters chars S chars intern chars For example let symi S red let sym2 intern red symi sym2 gt 1 eq symi sym2 gt t Chapter 7 Symbols 32 Note that creating a symbol with S in an Lpp program is not the same as quoting a symbol in a Common Lisp program In a Common Lisp program the quoted symbol causes intern to be called only on reading the program however in a C program intern will be called when the code is running So for example let symi S red let listi list symi sym1 is slightly more efficient than let listi list S red S red In both cases only one Symbol red is created but in the second case intern is called twice the second time simply returning the already interned symbol red unintern symbol Function unintern removes the symbol symbol from the system It returns nil Chapter 8 Numbers 33 8 Numbers In Lpp all specific kinds of numbers such as Integers are subtypes of the Lpp type Number Operations in general are provided for general Numbers as well as specific number types where necessary If a function only makes sense for a number argument then an error will be signaled if that argument is not a number 8 1 Number Types Lpp s type hierarchy for numbers is depicted in the following diagram Number C
33. This changes the car of x to y and returns the cons x after it has been modified For example let c cons L 1 L 2 rplaca c S a gt a 2 c list S a S b S c rplaca c L 1 gt 1 b c rplacd x y Function The argument must be a cons and y can be any Lpp object This changes the cdr of x to y and returns the cons x after it has been modified For example let c cons L 1 L 2 rplacd c S a gt 1 a rplacd c list L 2 L 3 gt 1 2 3 11 2 List operations The following are operations on lists nth n list Function This returns the nth element indicated by n of the list list where the car of list is the Oth element n may be an Lpp Integer or an int and must be positive If the length of the list is not greater than n then nil is returned For example let listi list S zero S one S two nth 0 listi gt zero nth L 2 listi gt two nth 10 listi gt nil Chapter 11 Lists 51 nthcdr n list Function This returns the result of applying the cdr function n times on list n may be an Lpp Integer or an int and must be positive If the length of the list is not greater than n then nil is returned For example let listi list S zero S one S two nthcdr 1 listi gt one two nthcdr 10 listi gt nil first list Function second list Function third list Function fourth list Function fifth list Function sixth list Function seventh list Function eight
34. ame A useful property of symbols is that they print the way they read In most program code the name of a symbol called the print name is rarely needed usually only the let type object is used as the identity of the Symbol object When the name of the symbol is needed such as for printing purposes or text manipulation the following function is used symbolName symbol Function symbolName is given an Lpp Symbol object symbol and the print name is returned as an Lpp String See Chapter 13 Strings page 56 For example let sym S red length symbolName sym gt 3 symbolName does not generate any new Lpp objects since the string object of a symbol is only generated once and used over and over again It is for this reason that even thought the name string object of a symbol is accessible that string should not be modified from its original since it would render that symbol unusable 7 2 Creating Symbols A symbol can be introduced into the system with the intern function intern name Function intern returns the interned symbol with the name name name can be either an Lpp String or a char string The name argument can be composed of any characters unless it is a char in which case the character 0 is not allowed If the symbol name has not been interned yet then it is interned first Lpp symbol are are case sensitive So for example Red and red are two different symbols The S macro provides a short hand for entering symbols
35. are built up with conses where for each cons its car contains the contents of the list element and its cdr contains a pointer to another cons cell Such a list is usually terminated with nil in the cdr of the last cons cell of the list Lists are printed with the Lpp printing functions exactly as they are in Common Lisp For example the list of the three symbols a b c is printed as a b c A list with a non nil in the last cdr is called a dotted list A one element list with a non nil in the cdr is called a dotted pair Note that a one element list is a single cons cell Dotted pairs are useful in association lists or anywhere an efficient container for associated dynamic typed variables is needed They are called dotted pairs because they are printed in Lisp and by the Lpp printing functions as x y where x is the car and y is the cdr A dotted list would be printed like abc d A list can also be circular For example in the dotted list above if instead of d in the cdr of the last cons it contained the list itself then it would be circular A list that is not circular or dotted is called a proper list As can be seen from the flexibility of cons cells as dotted pairs and lists cons cells are also useful for building trees For the purpose of this manual a tree is any Lpp object that may be an atom a non cons object or a cons whose car and or cdr may contain another cons nested to any level Lists can also be used as sets with various se
36. as a variable in C programs to denote time Also using Nil and True in Lpp programs emphasizes the fact that they are just C identifier names and not in fact symbols as they are in Lisp programs null exp Function null returns t if given expression exp evaluates to nil and returns nil otherwise 6 2 V tables and Type Predicates Data type predicates determine the type of an Lpp object or compare type hierarchies This is important for complex programs where objects must be dynamically typed and there are classes of algorithms that can not be easily implemented using v tables ie virtual function tables The Lpp user can still use v tables and mix the use of v tables with dynamic type dispatching Since these data type predicates are very efficient they should not be avoided and used wherever it seems reasonable Furthermore Lpp provides for type dispatching functions in type meta objects See Section 3 4 Accessing Type Meta Objects page 17 which are as efficient as v tables but more flexible and dynamic 6 3 General Type Predicates typels exp name Macro typels returns t if the expression exp evaluates to an object whose type is exactly of the type name name and nil otherwise Note that exactly implies that if name is a supertype of the object s type then false is returned This macro is designed to be as fast a type check as possible and thus returns a low level C predicate O for false and 1 for true The returned result
37. ates an ambiguous situation when a type can be interpreted as either int or let For example overloaded Lpp functions that are disambiguated between those that have the same function profile but differ in an int or let argument and so the following is legitimate let someFunction int i let obj i to be an int let someFunction let i let obj i to be an Lpp Integer or nil So to disambiguate you would use Nil where nil was intended someFunction 0 something Means O as an int someFunction L 0 something Means O as an Lpp Integer someFunction Nil something Menas O as Lpp nil Nil Constant True Constant Nil is intended to be used in programs where the nil symbol is needed and True is intended to be used where the t symbol is needed These are both names of constants Chapter 6 Predicates 28 that can not be assigned other values Since the Lpp Nil object prints as nil and the Lpp True object prints as t nil and t when used in this manual will refer to those objects respectively The name of the constant True and Nil can be redefined per compilation unit See Section 2 3 Redefining Predefined Names page 12 So for example an Lpp programmer could redefine these names to be nil and t to correspond to the Common Lisp constants nil and t After examining many C programs it was decided that the default should not be the names nil and t since these frequently occurred as variable names For example t is frequently used
38. ative to the current directory It can be a char string or an Lpp String The pred argument can be one of the symbols exist readable writable executable directory regular symbolicLink If given the symbol exist probeFile returns t if the given file specification exists If give readable writable or executable it will return t if the file is readable writable or executable respectively If given directory it returns true if the file specification is a directory If given regular it will return t if the file is a regular file Note that a regular file can be other than a non directory in some file systems such as a Unix file system For file systems that have symbolic links if given the symbol symbolicLink it returns t if the file is a symbolic link For example the directory symbol can be used to recursively descend directories Chapter 15 File System Interface 62 void recursiveDescend let directory dolist file fileDirectory directory Do something with this file if probeFile file S directory recursiveDescend file Note that calling probeFile with the symbol exist is similar to the probe file of Common Lisp that is it is a predicate for the existence of the file But where probe file in Common Lisp returns the true name of the file probeFile just returns t if the file exists This was done on purpose in Lpp to avoid generating a String object that would just need to be garbage collected if all that we wanted to u
39. bject is a cons true if nil and signals an error for all other values It is useful for checking for the end of a proper list listLength list Function listLengthL list Function listLength is the same as length applied to a list but is guaranteed to return 1 if the list list is a circular list listLengthL is the same as lengthL but returns nil if list is a circular list listLengthL can also be used as a function object copyList list Function copyList returns a list that is equal to list but not eq Only the top level of the list is copied that is it copies in the cdr direction and not the car direction copyAlist list Function copyAlist is meant for copying association lists See Section 11 4 Association Lists page 53 It is just like copyList except that for each element a cons is replaced in the copy by a new cons with the same car and cdr copyTree object Function copyTree is for copying trees of conses If object is not a cons it is just returned If object is a cons the whole tree of conses in its car and cdr is copied recursively and returned In this process conses are copied but non conses are placed in the new cons as is nconc list1 list2 Function nconc concatenates listi to list2 and return the concatenated list Its arguments must be lists or nil The arguments are changed rather than copied listConcat list list2 Function listConcat is just like nconc except that its arguments listi and list2 are cop
40. ce Function nremove x sequence test Function nremove x sequence test gc Function nremove returns the sequence sequence with any element eql to x removed Unlike remove nremove is a destructive operation on sequence If the optional third argument test is given it must be an Lpp Function of two arguments and it used in place of eq1 for the test If test is given but nil then again eql is assumed The fourth argument gc tells nremove how the removed elements should be garbage collected If sequence is a string then gc is ignored If gc is nil then they are not collected Chapter 10 Sequences 46 If gc is t then they are fully collected If gc is an Lpp Function of one argument then it is called on the removed element If sequence is a list then gc is applied to the removed cons and a t value is equivalent to passing in the gcConsElement function If you wanted to collect just the cons element but not its contents you would pass in the gc function as the gc argument Here are some examples for lists let listi list L 1 L 2 L 3 let list2 nremove L 2 list1 listi gt 1 3 list2 1 3 let listKeep list L 1 L 2 L 3 let listi list L 1 listKeep L 3 listi gt 1 1 2 3 3 let list2 nremove listKeep list1 0 list1 gt 1 3 list2 gt 1 3 listKeep gt 1 2 3 listi list L 1 L 2 L 3 list2 nremove L 2 list1 Nil True Strings are not eql listi gt 1 2 3
41. ch on a common action like the princ printing action above Since the extension is a first class Lpp object it can be exchanged dynamically So for example the user can cause a collection of classes to behave one way in one mode and then another way in a different mode Also all Lpp objects Type meta objects are guaranteed to have an extension even if only nil which would usually be a default case or no action 3 5 Accessing Subtypes In the majority of code written using Lpp Lpp objects appear as type let All operations within Lpp objects are done with accessors When defining new Lpp objects the user needs to get at the internals of such objects for defining his own accessors The Lpp the macro makes it easy and safe to access the internals of an Lpp object the name exp Macro theOrNil name exp Macro asThe name exp Macro Chapter 3 Subtypes 21 fromThe name exp Macro The argument name names some subtype of the Lpp type hierarchy Lpp takes the liberty to adapt the Common Lisp he construct Much like Common Lisp the Lpp the macro evaluates the given expression exp and returns the value if it is of the type named by the given name and triggers an exception if the type is not of the given name In addition the Lpp the macro returns a pointer to that object of type name name This is a specific pointer of that name type as opposed to the more general Lpp let type For example classL MyType int sloti public friend void s
42. clude LppPure hh here instead of Lpp h but Lpp hh is always safe to use See Section 1 5 Pure Lisp Include File page 10 Second you need to link the library libLpp a with the compile of your C program libLpp a gets installed on your system when Lpp is installed See Section 1 2 Installing Lpp page 4 For example on Linux if you had installed Lpp under usr local and then wrote a C program that used Lpp called main cc then the following would first compile a program main cc and then make an executable called pgm c I usr local include c main cc c o pgm main o usr local lib lpp libLpp a Then after that if you are familiar with Common Lisp you will find that writing pro grams using Lpp is similar to writing programs in Common Lisp big advantage is that you can mix quite freely the static style of C with the dynamic style of Lisp without hav ing to think about it too much In the next section we give a simple example to illustrate this 1 4 A Simple Data Base Example In this example we create a class that implements a simple data base We then create an instance of the data base and populate it with some entities namely some people And finally we generate a report on the people of a family that we used to populate the data base to show the relational aspect of the data base Lpp is useful here since we want entities to be any kind of object and hence dynamically bound objects will index the data base In the example
43. d while the function myCollector is applied to the leaf node objects myObject1 myObject2 gcList listi L myCollector gcTree tree leaf Function gcTree will garbage collect the tree tree and leaf nodes depending on the leaf argument as described above The tree argument can be a cons or a non cons Lpp object If it is a non cons object then the eaf argument is applied to the object If there are circular or multiple references in the tree in paths that have one of more leaves then gcTree will break with an error message stating such For any trees with circular or multiple references the programmer can either store a non cons in the offending cons slots and then call gcTree or write a gc function specialized for such a data structure and call gcList passing the specialized function as the leaf argument Here are some examples using association lists which are in effect small trees Chapter 17 Miscellaneous Features 67 let alisti list cons L one L 1 cons L two L 2 cons L three L 3 let alist2 copyAlist listi leaf argument is nil so alisti is grabage collected but Strings one two three are left alone and numbers 1 2 and 3 are left alone gcTree alisti Nil leaf argument is t so alist2 is grabage collected and so are Strings one two three and numbers 1 2 and 3 gcTree alist2 True alisti list cons L 1 myObjecti cons L 2 myObject2 leaf a
44. e BigInteger y would eliminate any dispatching cost This situation is rare and it is recommended that the cost is so small in proportion to the algorithms involved that this casting should routinely be avoided it is simply not worth it Asofthis writing number functions only dispatch on the specific number types SmallInteger BigInteger and Ratio Chapter 9 Characters 39 9 Characters There is only one set of Lpp Character objects where each corresponds to one C character constant So there is no overhead in generating Character objects and since there is only one object per character constant they can reliably be compared with eq The initial Character objects are lazy generated ie only generated on demand since most characters are not even used in most program applications The character functions that deal with character attributes such as alphaCharP charUpcase etc are only effective on the standard character set as defined by the Common Lisp specification Chapter 9 Characters 40 9 1 Predicates on Characters The function characterp can be used to determine if an object is a Character object See Section 6 4 Specific Data Type Predicates page 28 In the rest of these predicates the given character argument must be a Character object if it is not an error is signaled standardCharP character Function standardCharP returns t if character is a standard character and nil otherwise See the Common Lisp spec
45. e of Lpp hh define LPP NO TYPE CHECK 1 This automatically modifies the behavior of the the macro With type checking turned off the users code using Lpp is just as efficient as ordinary C code accessing statically compiler type checked references Rarely do you want to turn off type checking while developing a program and usually doesn t even hurt the efficienty too much on a delivered package The above methods turns off type checking per your compilation units that see LPP_ NO TYPE CHECK as defined above If you also want the Lpp library itself to not do type checking then you need to recompile the Lpp library in the same way as above or better yet link your program with the Lpp library where this has already been done as for example linking with the Lpp supplied library Chapter 3 Subtypes 22 libLppNC a instead of libLpp a You can optionally make and install 1ibLppNC a in the Lpp in stallation See Section 1 2 Installing Lpp page 4 Chapter 4 Functions 23 4 Functions As with Common Lisp Lpp has first class function objects That is to say that objects can be created that represent C functions and manipulated like any other Lpp object Such function objects are easily created with the L conversion macro For example let someFun let x let y 4 let functionList list L car L someFun In this example functionList contains a list of two function objects car an existing Lpp function and some other function objec
46. e that no part of the returned Lpp object shares the value of z Some actual examples are Converting a let myInteger L 5 C integer to Lpp Integer int x 123 let myInteger2 L x C integer to Lpp Integer let myCharacter L z C char to Lpp Character let myString L Some string C string to Lpp string let myFun L cFunction C function to Lpp function The Lpp object types created by the L conversion macro when applied to C types is as follows int Lpp Integer char gt Lpp Character char gt Lpp String let fun let let gt Lpp Function An Lpp Function object can be created from a function returning a type let taking 0 or more type let arguments 3 2 2 To C Primitive Types Converting an Lpp object back to primitive C types is done with the zL converter where x designates a primitive C type iL x Function cL x Function sL x Function Chapter 3 Subtypes 16 pL x Macro The function iL converts an Lpp Integer x to and int and returns cL converts an Lpp Character x to a char and returns sL converts an Lpp String x to a char string and returns pL converts an Lpp predicate nil or t to a C predicate 0 or 1 Some examples are Converting a let myInteger L 5 C integer to Lpp Integer int i1 iL myInteger Then back to int let c1 LEZ C char to Lpp character char ch cL c1 Then back to C char let myString L So
47. e will handle all trees except trees that have circular or multiple references However it will break with an explanation error message if it detects either circular or multiple references for most trees The only cases where it doesn t detect is where there are circular or multiple reference paths where there are no leaves in the path For example let self list True rplaca self self Clearly this simple tree has a circularity with no leaf nodes in the circular path This would fail with gcTree But this is not usual and is probably not even useful In such unusual trees the programmer must either create his own collection function or else store a non cons in such a problematical cons slot and then call gcTree gcList list leaf Function gcList will garbage collect the list list and leaf nodes depending on the leaf argument as described above For this purpose the cars of each cons in the list will always be treated as leaf nodes even if they contain other cons objects Here are some examples let listi list L one L two L three let list2 copyList listi leaf argument is nil so listi is grabage collected but Strings one two three are left alone gcList listi Nil leaf argument is t so list2 is grabage collected and so are Strings one two three gcList list2 True listi list myObjecti myObject2 leaf argument is the Function myCollector so listi is garbage collecte
48. ead and the Chapter 14 Input Output 59 output of princ is intended to look good to people print is just like prin1 except that the printed representation of object is preceded with a newline and followed by a space Some examples will make this clear the following code let obj L one two three prini obj print object princ obj would produce on cout one two three one two three one two three Note that the last output produced by princ when read back using read would produce the Symbol one instead of the String one two three C output stream operators for Lpp objects default to using princ so that the following code let obj L one two three cout lt lt Object lt lt obj lt lt endl would produce on cout Object one two three 0 object Macro The 0 macro allows the Lpp object argument object when using C output stream operators to print using prin1 Contrast the following code and results using the O macro with the previous example let obj L one two three cout lt lt Object lt lt O obj lt lt endl would produce on cout Object one two three Note that it only makes sense to use the O macro with C stream operators The results of using the O macro elsewhere is undefined terpri Function terpri stream Function terpri outputs a newline to the C output stream stream If the stream argument is omitted cout is assumed terpri returns nil finishOutput Function fi
49. en length the String sequence would cause no sequence dispatching to take place Or the user could define something like inline let length string let sequence return length the String sequence inline let length list let sequence 1 return length the0rNil Cons sequence and then use length string when it is expected that sequence will be a string and length list when expecting a list Since the whole idea of these specific type calls is absolute efficiency when calling a sequence function using this method and the function overloads arguments as C types or Lpp types then only the C types are allowed For example if the normal sequence function can take an int or an Lpp Integer then in the specific type function arguments only the int will work subseq asThe String sequence 3 Works subseq asThe String sequence iL L 3 Works subseq asThe String sequence L 3 Will not even compile notice that the last is not allowed by this rule and will not work or even compile Frankly unless absolute efficiency is desired the overhead on sequence dispatching is so small that this specific dispatching method is rarely worth doing Chapter 11 Lists 49 11 Lists In Lpp as with Lisp lists are built up using cons cells and a list is defined as either a cons or nil In Lpp a cons cell is an Lpp object with just two members The car is thought of as the left member and the cdr as the right member Ordinary lists
50. est ees nre P4 ne 47 positionTestL 2 tres 47 PPD id SR eae o nal 64 P DALE AIS Le Se Se ede ost DUE 59 DEL s ee An een tee nee ne ne 58 prinlLength gee a ls 60 priniLengthL 60 priniToString 2c ege din t 60 A A ME is AA ES 58 princLength 2 42 det 60 prismcLengthL i1 21l2erEeie pese oia PR 60 princToString 60 PEA a Me men seu 58 probePile 32 25 5 d i Shee ia duc de e 61 push 22212 diee B vs leh ee at 52 puthash is qur A Ne nn fat US ies d 54 R Rational p os Mise fg hes Xue es bed ened AE Sore 29 Fat TOP nine nn Paniers ae SE ERR 29 read A LN HIE bonc 57 readFromString 58 Tenis ea seas 38 TOMAS siennes ele nelle dd Seal RA TU EE 54 TOMO cov orte er I cxt dinosaure 45 reversen einga dg gu Ne M pe 45 rpDsMeni 112 du Oiled tee VO Pte Idee 70 PLACA Lu het seed ARR ee BRUM es rt 50 TPL topen uian E een see Due ee 50 TpSnMenis ejut Unio prius 70 S EE 31 Second es dre encres aei residue 51 SetELt v s uon ne eds eel eG NIS sonata 44 setTypeEqual 19 SetTypeEqualp conl bep See een ss 19 SetIypeExto 2i2l2bicleblokssdde s j 20 SetIypePrindi c ves144 li eee PES 18 SetlypePrinCu S her Gate ene ge ee ek ed 18 setTypePrins 19 Seventh eee em adene oye tee a Mia dane trig 51 SYXUtli ous dent se de duque shee tine estque 51 ET eL nE
51. etSloti let int setSloti makeInstance MyType 25 void setSloti let x int val x is input as general let type MyType mt the MyType x mt is now a specific pointer mt gt sloti val to a MyType object The the macro enforces that the given object is of type type by doing a dynamic type check Dynamic type checking can be turned off per compilation unit See Section 3 6 Type Checking page 21 The asThe macro is the same as the but never does any type checking It is useful when a function absolutely knows what the type of an Lpp object is The fromThe macro is the same as asThe but instead demands that the object is of the given type or derived from a supertype of the given type It is useful when a function absolutely knows what the type or supertype of an Lpp object is The theOrNil macro is the same as the but also allows nil to pass as the type 3 6 Type Checking When dynamically typed objects are being processed in Lpp in a type safe mode objects are dynamically checked for the expected type In Lpp this is enfored with the the macro See Section 3 5 Accessing Subtypes page 20 This usually only occurs at one place in object accessors When it is not of the expected type control is transferred to a specified handler usually to an error code handler The default is to have this dynamic type checking turned on It can be turned off per compilation unit with the following define that must precede the includ
52. fresh copy of the sequence is returned and it is guaranteed to be equalp to sequence but not eq to it For example let stringi L Hello let string2 copySeq string1 equal stringi string2 gt t eq stringi string2 gt nil Chapter 10 Sequences 45 reverse sequence Function nreverse sequence Function reverse returns a copy of the sequence sequence with all of its elements reversed nreverse does the same as reverse but a copy is not made and sequence is modified For example let listi list L 1 L 2 L 3 L 4 reverse list1 gt 4 3 2 1 listi gt 1 2 3 4 nreverse list1 gt 4 3 2 1 listi gt 4 3 2 1 let stringi L 1234 reverse stringi gt 4321 stringi gt 1234 nreverse string1 gt 4321 stringi gt 4321 10 2 Modifying Sequences remove x sequence Function remove x sequence test Function remove returns a copy of sequence sequence with any element eql to x removed If the optional third argument est is given it must be an Lpp Function of two arguments and it used in place of eq1 for the test For example let listi list L 1 L 2 L 3 let list2 remove L 2 list1 listi gt 1 2 3 list2 gt 1 3 listi list L 1 L 2 L 3 list2 remove L 2 list1 listi gt 1 2 3 list2 gt 1 2 3 list2 remove L 2 listi L stringEqual listi gt 1 2 3 list2 gt 1 3 nremove x sequen
53. h list Function ninth list Function tenth list Function These functions are provided for convenient accessing of predetermined list element po sitions instead of nth and are more efficient first returns the same as nth with a first argument of 0 second with a first argument of 1 etc list args Function list constructs and returns a list of args which can be 1 or more arguments of type let Up to 10 arguments may be given For example list L zero S one L 2 gt zero one 2 list S zero S one list L 2 L 3 gt zero one 2 3 If more than 10 is needed listEM should be used EM Constant listEM args Function listEM is like list except that args can be any number of type let arguments A global constant EM abbreviation for End Marker terminates the arguments For example listEM L zero S one L 2 EM gt zero one 2 let n L 1 listEM n n n n n n n n n n n EM gt 11111111111 listSEM args Function listSEM is like listEM except that args can be any number of type char arguments For example listSEM zero one two EM gt zero one two Chapter 11 Lists 52 last list Function last list n Function last returns the last cons of the list list With the optional integer argument n it returns the tail of the list consisting of the last n conses of list The n argument may be either an Lpp Integer or an int endp object Function endp is false if o
54. han 10 arguments needed and if you are habitually using more than 10 arguments then you are probably doing something wrong or else you should be using something like 1istEM or listSEM See Section 11 2 List operations page 50 Chapter 5 Control Structure 24 5 Control Structure Lpp tries as much as possible to capitalize on C control structure C control struc ture is augmented however with the use of Lpp first class function objects and functions funcall and apply Lpp supplies control structure macros to make code easier to read and implement For example the dolist macro just expands into a harder to read C for loop 5 1 Function Invocation As mentioned previously See Chapter 4 Functions page 23 Lpp first class function objects can be generated and manipulated as data and also applied to other data funcall fun args Function This function calls the Lpp function object fun on the args which can be 0 or more arguments of type let A value of type let that fun returns is returned from the funcall apply fun args Function This function is similar to funcall except that the arguments given to fun is taken from only one argument args which is a list of 0 or more Lpp objects Here are some examples that contrast funcall with apply let listi list L Lost L World let fun L listConcat funcall fun cadr listi car list1 gt WorldLost apply fun list1 gt LostWorld 5 2 Iteration 5 2 1 Iteration
55. hash key table Function remhash removes the entry for key in hash table table This is also a predicate that returns t if there was an entry and nil if not Chapter 12 Hash Tables 55 clrhash table Function clrhash removes all entry from the hash table table and returns table hashTableCount table Function hashTableCountL table Function hashTableCount returns the number of entries in hash table table as an int hashTableCountL does the same but returns an Lpp Integer A hash table starts out with zero entries maphash function table Function For each entry in hash table table maphash calls function function on two arguments the key of the entry and the value of the entry For example the following maphash would display the key and value for all entries in hash table ht let printKeyValue let key let value princ key prini key princ value prini value terpriO return Nil maphash L printKeyValue ht 12 2 Primitive Hash Function The primitive hash function sxhash is used internally by the Lpp hash functions but is made public so that the user can create other hashing structures sxhash object Function sxhashL object Function sxhash returns a hash code positive integer for the Lpp object object as an int The returned integer is such that for given objects obj1 and obj2 equal obj1 obj2 gt sxhash obj1 sxhash obj2 So for example two different strings abc will return the sa
56. he same time imply true Furthermore having no specific object implying true makes multi value logics in programs easy and consistent with reductions to two valued logics Lpp uses the same philosophy as Common Lisp for deciding when to return non nil or t If no better non nil value is available for indicating success the symbol t will be returned 6 1 Logical Values Lpp chose to use 0 as nil This allows Lpp code to be symmetric with usual C code that uses 0 and 1 as predicates The alternative would be to have to use the null function for every predicate occurrence For example any Lpp function f returning let or Lpp object x can be used as a predicate if x do something if O do something Note that nil serves as three important things a logical value the end of a list and it is also the symbol nil The 0 as nil concept it is to be thought of as though the symbol object nil resides at memory address 0 So that everything that can be done with a symbol can be done with 0 For example symbolName 0 gt nil When a let variable has the value 0 it prints as nil Conversely when in any s expression is read from a stream nil is translated into 0 internally Lpp provides a global variable True that has as its value the Lpp Symbol t See Chapter 7 Symbols page 31 And Nil is defined simply as 0 so it is just as efficient to use Nil as 0 in your code except that the compiler knows that Nil is an Lpp type Choosing 0 as nil cre
57. ied and the copies are concatenated and returned For the next two macros push and pop the argument place is any variable that contains a list Note that by the definition of a list nil is also considered a list push item place Macro item is any object of type let After the push macro executes place will be a list with item as the car of the list and the list that was in place as the cdr pop var place Macro var is any variable of type let After the pop macro executes var will contain the car of place and place will contain the cdr Chapter 11 Lists 53 11 3 Using Lists as Sets member item list Function member item list test Function member searches the list list for an object eq1 to the Lpp object item If none is found nil is returned otherwise the tail of list beginning with the element eq1 to item is returned list is searched at the top level only Note that member can also be used as a predicate If the optional argument test is given it must be an Lpp Function object of two argu ments that performs the test in place of eql For example member L 2 list L 1 L 2 L 3 gt 2 3 member L 2 list S one S two L three gt nil let snums listSEM 1 2 3 EM member L 2 snums gt nil member L 2 snums L stringEqual gt 2 3 11 4 Association Lists An association list is a list of pairs where each pair is an association of a key to a datum The pair is represented as a c
58. ification for the definition of standard characters For example standardCharP L a gt t standardCharP L gt t standardCharP L n gt t standardCharP L gt t standardCharP codeChar 2 gt nil alphaCharP character Function digitCharP character Function alphanumericp character Function alphaCharP returns t if character is an alphabet letter of either case and nil otherwise digitCharP returns t if character is a 0 9 digit and nil otherwise alphanumericp returns t if character is either a letter or digit and nil otherwise upperCaseP character Function lowerCaseP character Function upperCaseP returns t if character is an upper case letter and nil otherwise lowerCaseP returns t if character is a lower case letter and nil otherwise Chapter 9 Characters 41 9 2 Character Construction codeChar code Function codeChar given an integer code returns the Character object encoded as code code can be an Lpp Integer a C int or C character constant For example in let chari codeChar a chari would be set to the Character object representing the C character constant a Note that the L operator can be used to do the same thing let chari L a but it is not symmetric in the sense that the L operator applied to an int will return an Lpp Integer instead of an Lpp Character as codeChar will charCode character Function charCodeL character Function
59. is is consistent with the way Lisp works In addition to this the programmer can declare let members in conventional C classes where non abstracted pointer representations are needed The Lpp class defines a slot that contains a pointer to a type meta object of type Type This pointer is a let type and so the type meta object itself is also a an Lpp object Type objects themselves being of base type let can be stored and operated on like any other Lpp object So for example let x S foo let y list x typeUf x cout lt lt first y lt lt second y lt lt endl would print x followed by its type Type objects contain meta information about the specific objects it is a type of Some of the information it contains are a type name a type lattice member and type dispatching functions Type dispatching functions are very similar to virtual functions but are more powerful in that they themselves can be dynamically manipulated An example of type dispatching functions are the various printing functions for Lpp objects 3 1 Predefined Subtypes The predefined subtypes in Lpp are e Type Type meta objects mentioned above e Function Analogous to Lisp first class function objects e Cons Analogous to Lisp cons cells e Symbol Analogous to Lisp symbols e Number Analogous to Lisp numbers e Integer Analogous to Lisp true integers e Character Analogous to Lisp characters e String Analogous to Lisp strings e HashTable Analogous to Lis
60. ization rule should now be 1 9 instead of 22 198 truncate x Function truncate x y Function truncateCons x Function truncateCons x y Function The truncate function with a single argument x converts it to an integer by truncating it toward zero So if an integer n is given as the argument then it just returns a copy of n If given a ratio as the argument then it is truncated as described here If given two arguments x and y then truncate acts as if it is given a single ratio argument of x y The truncateCons function returns a cons whose car is what truncate would have returned and whose cdr is the remainder The arguments to truncate and truncateCons can be int values or Lpp Rational numbers Here are some examples truncate L 5 gt 5 truncate divide L 13 L 4 gt 3 truncate readFromString 13 4 gt 3 truncate 13 L 4 gt 3 truncateCons readFromString 13 4 gt 3 1 let a readFromString 5 3 let b readFromString 2 3 truncate a b gt 2 truncateCons a b gt 2 1 3 floor x Function floor x y Function floorCons x Function floorCons x y Function The floor family of functions above are the same as the truncate family above except that floor truncates toward negative infinity For example Chapter 8 Numbers 38 floor L 5 gt 5 floor divide L 13 L 4 gt 3 floor readFromString 13 4 gt 3 floor 13 L 4 gt 3 floor 13 4 gt 4 floorCons
61. list2 gt 1 2 3 list2 nremove L 2 listi L stringEqual True listi gt 1 3 list2 gt 1 3 Here are some examples of remove and nremove for strings let stringi L Hello World remove codeChar o stringi gt Hell Wrld stringi gt Hello World nremove codeChar o string1 Hell Wrld string gt Hell Wrld Chapter 10 Sequences 4T 10 3 Searching Sequences position obj seq Function position obj seq start Function position obj seq start end Function positionL obj seq Function positionL obj seq start Function positionL obj seq start end Function position returns the position of the first object found in the sequence seq that is eq1 to the given object obj If the object is not found in the sequence then a 1 is returned The positionL functions are the same but an Lpp Integer object is returned for the position and nil is returned if the object is not found The optional start argument is a positive integer as either an int or Lpp Integer object and indicates at what position to start the search The optional end argument is also a similar positive integer and it indicates at what position to end the search In this case the search is from start to one element before end The end argument can also be nil defaulting end to be the length of the sequence which would cause a search to the end of the sequence positionTest obj seq test Function positionTest obj seq test sta
62. llowing simple program illustrates the use of both kinds of menus Appendix A Appendix read print User Interface rpUI 71 include lt rpMenu hh gt include lt Lpp hh gt main let menui rpDsMenu L Enter command or for help listSEM exit Exit program fix Fix me list List Sn menu EM let menu2 rpSnMenu L Choose one of listSEM choice 0 choice 1 EM int choice int doing 1 while doing 1 switch chooseRpMenu menui 1 case 0 doing 0 cout lt lt Goodbye n break case 1 cout lt lt Thank you n break case 2 choice chooseRpMenu menu2 cout lt lt nChoice lt lt choice lt lt endl When the above program is run it will produce the following dialog Enter command or for help Choose one of exit Exit program fix Fix me list List Sn menu Enter command or for help list Choose one of 0 choice 0 1 choice 1 Enter 0 1 1 Choice 1 Enter command or for help exit Goodbye Function Index Function Index A characterp iia In eb bene e de et Ch rGode sosie san yp Oe dee ns ant Pete char Code en on de eda CharDownCase is sad Ihm oy nei charUpease te ha ie e ne chooseRpMenu ER NEEDED digitCharP ioco 05219 9st ve be WI divide o v LEARN ao eon we eR 2 dolia ASE ea dure dur ET dan 72 equals hod aut alerts ia DER uen 30 OTTO ea oU mn ne ted aute Rc 63 VENIR os otii
63. loti 76 slot2 trombones The default princ and prin1 type dispatching functions for any Lpp type will print an object as lt Lpp xxxx gt where xxxx will be the type name of the object The princ type dispatching function is the one used by the princ function and also by the lt lt operator as seen above setTypePrins type function Function setTypePrins sets both the princ and prn1 printing function to the same function When an Lpp equality predicate of either equal or equalp is called an equality dis patcher function of the Type meta object is called to compute the predicate The two arguments of the equality predicate are passed to the dispatcher function and the returned result is the result of the equality predicate The equality dispatcher functions can be accessed with the following setTypeEqual type function Function getTypeEqual type Function setTypeEqualp type function Function getTypeEqualp type Function The setTypeEqual function sets the equal dispatcher function of the Type meta object type to the given function function which is an Lpp function object of two arguments When equal is called given two objects where the first object is of this type then the function function is automatically dispatched on the object Given both objects the function should Chapter 3 Subtypes 20 return t if the objects are considered to be equal and nil otherwise As an example suppose that we had defined an Lpp class called
64. me string C string to Lpp string char si sL myString Then back to a char 3 3 Defining New Subtypes The user of Lpp can create new Lpp subtypes with the classL and classLS macros Note that all examples given in this section assume that a type meta object is generated some where see genT and genTS macros below When a class is defined as an Lpp subtype a new type meta object named by that class name will exist in the Lpp type hierarchy Instances of that class then can be used anywhere an Lpp object of type let can be used For example such an instance can then occur in an Lpp list classL name Macro classLS name Macro The classL macro is used for defining new Lpp subtypes of type let The classLS macro is used for defining new Lpp subtypes of Lpp supertypes The name argument specifies the name of the new class and subtype The syntax of both is exactly the same as the ordinary C class syntax except that instead of using the class identifier either classL or classLS is used For example classL MyType let mi makeInstance MyType let listi list L 1 m1 L 2 cout lt lt second listi We could do a similar thing with a subtype of MyType classLS MySubType private MyType let msi makelnstance MySubType let listi list L 1 msi L 2 cout lt lt second listi Should print t for both of these cout lt lt typep second listi type MyType cout lt lt typep second lis
65. me sxhash integer The function sxhashL is exactly the same except that it returns an Lpp Integer object and it is guaranteed that for a given object obj sxhash obj iL sxhashL obj Chapter 13 Strings 56 13 Strings Lpp Strings are considerably more efficient than ordinary C strings in algorithms that require the length of the string since the length is held as a slot in the String object Ordinary C strings must be searched for the 0 end character Also null characters 0 character code are allowed inside Lpp Strings and not inside ordinary C strings When converting from Lpp Strings to C strings the programmer must be aware of this If there is a 0 in an Lpp String then the converted ordinary C string will look like it s been truncated Rarely is this a problem since either the programmer knows that in an application 0 s may be stored in Strings in which case he only uses Lpp Strings in such a case something like Lpp Strings must be used anyway or the programmer s application only needs to convert ordinary C strings to Lpp Strings but not vice verse 13 1 String Comparison stringEqual sl s2 Function stringEqual compares Lpp String s1 to Lpp String s2 and returns t if they are the same length and all characters of s are the same characters of s2 Otherwise nil is returned For example stringEqual L foo L Foo gt nil stringEqual L foo L foo gt t stringEQUAL s1 s2 Function StringEQUAL is the
66. moves the symbol from the system and garbage collects all of the associated parts See Section 7 2 Creating Symbols page 31 If gc is called on a cons it collects the cons object only and not its car or cdr For garbage collecting cons structures gcList should be used for garbage collecting top level lists and gcTree for collecting tree structures In these functions a leaf argument says what to do with the leaf nodes of a list or a tree Leaf nodes are a car or cdr of a cons that is something other than a list nil or another cons However the main difference between gcList and gcTree is that with gcList the car of every cons in the top level list is also treated as a leaf node for the listed meanings below of the leaf argument Note that because of this using gcList with a special user collection function as the leaf argument the user can effectively create his own special version of gcTree This also implies that if the programmer wants to use gcTree on a top level list that has a cons in some of its cars he should use gcTree instead or else supply his own leaf function argument to collect such cars or otherwise take care of them The leaf argument can be one of three things with the following implied meanings nil Do not garbage collect leaf nodes t gt Apply gc to leaf nodes Function f gt Apply Function f to leaf nodes Chapter 17 Miscellaneous Features 66 gcList will handle all lists including circular and dotted lists gcTre
67. n a different way Appendix A Appendix read print User Interface rpUI 70 Appendix Appendix read print User Interface rpUI Lpp comes bundled with rpUI a read print User Interface that is implemented using the Lpp library By providing a selection of simple menus rpUI makes it easy to create complete applications using Lpp Read print interfaces are also good for creating debugging interfaces to applications and regression testing procedures Two kinds of menus are available First Static Numerical menus present the same fix list of choices for the user with sequential integers prefixing each choice T he user types in an integer to make a selection Second Dynamic Symbolic menus just prompts for a symbol and a choice is made based on the symbol that the user enters rpSnMenu spec Function This function returns an Lpp object representing a Static Numerical menu based on the specification spec provided The menu object returned can be used in subsequent calls to chooseRpMenu where the user needs to make a menu selection spec is a list of Lpp Strings The first entry in the list will be the menu header The rest of the list will specify sequential menu selections When chooseRpMenu is called the user will be presented with the header followed by the choices prefixed with integers and then a prompt for an integer If the user enters anything other than one of the integers corresponding to a choice the user is notified of a bad en
68. nal No Tyoe Check version of the Lpp library with the following make nc make installnc and this using our above example will install IDIR lib lpp libLppNC a The Lpp User s Manual will explain when you might want to use libLppNC See Sec tion 3 6 Type Checking page 21 It doesn t hurt to do this and you may as well at this point This make will also create an Lpp regression test If you want to see if it passes this test on your system you can do the following Change to the directory UDIR 1pp 1ib test and type check It runs the regression test and then does a diff against the expected results If you see Chapter 1 Introduction 5 Lpp test suite 1 difference between now and expected is and no following differences then it has passed the test on your system After the make install referring to the above install directory example you can then read the Lpp User s Manual using Emacs Info with the file 1pp info or as HTML using the URL file IDIR doc lpp lpp html or file IDIR doc lpp lpp html index html In particular you can read how to use Lpp See Section 1 3 Using Lpp page 6 If you want a hard copy of the Lpp User s Manual then change to the directory UDIR 1pp doc and type make ps which will make the PostScript version 1pp ps Chapter 1 Introduction 6 1 3 Using Lpp Using the Lpp library is easy First you need to include in your C file include lt Lpp hh gt In some fussy cases you may want to in
69. nishOutput stream Function finishOutput attempts to ensure that all output sent to the C output stream stream has reached its destination and only then finally returns nil If the stream argument is omitted cout is assumed pprint object Function pprint object stream Function pprint object stream start end Function pprint is just like print except that the space is omitted after object is printed to the C output stream stream and the output is pretty If the stream argument is omitted cout Chapter 14 Input Output 60 is assumed If the start and end arguments are included the output will be pretty printed between columns start and end start and end must be ints All of these functions return object as its value Pretty means that the object is printed using extra white space to make it easily human readable For example nested lists are indented to make the tree structure apparent priniToString object Function princToString object Function Both of these functions return an Lpp String with the printed representation of ob ject priniToString returns the prin1 printed representation princToString returns the princ printed representation For example let obj priniToString list L 1 L 2 L 3 prini obj gt 1 2 3 Typically priniToString and princToString are used to print relatively short expres sions or values to strings The maximum length of such a string allowed by default is 4096 characters which is
70. ntity let DataBase getValue let entity let attribute return cdr assoc attribute gethash entity contents First notice the include command for Lpp hh To use Lpp a compilation unit needs to include the header file Lpp hh when using let declarations Notice in the class definition the let type declarations This is how members containing Lpp objects are always declared Also notice that let type members can be easily combined with other types in this case int In the data base accessor functions and in the following code that uses them the occur rences of Common Lisp function names such as car cdr cons assoc push puthash Chapter 1 Introduction 8 dolist etc do exactly what you would expect them to do if you are familiar with Common Lisp Now we create a main program that populates the data base and prints a report on a family Chapter 1 Introduction 9 LUN II NM M I TT M M TA M M TL A NN A A LA NN A I M A NN A M AN NL P gl gl Hl gl lll Create and test a DataBase main DataBase db DataBase db db db db db db db addEntity S smith family db setValue S smith family S people list S joe S frank S mary addEntity S joe db db setValue S joe S name L Joseph Clyde Smith setValue S joe S age L 42 setValue S joe S siblings list S frank S mary addEntity S frank setValue S frank S name L Frank Bob Smith db db
71. ommon Lisp function names and semantics for the operations on Lisp like objects Lpp chose address 0 as nil This is so that it is easy to check for the end of lists or nil returned in predicate arguments in C functions operators and statements like if while for So nil and non nil is consistent with C predicate semantics on 0 and non 0 But even though nil is 0 it is exactly equivalent to the symbol nil It is as if the symbol object nil was an object at address 0 Symbols are introduced into C programs easily with the SO macro For example S foo S Foo S my foo S foo bar S S amp are all legitimate symbols in Lpp Symbols after being interned are just as efficient but far more powerful than enums The default for symbols in Lpp is case sensitive so that foo and Foo are two distinct symbols Primitive C objects can easily be converted to their analog Lpp objects using the L constructor For example L abc will produce an Lpp String object and L cdr will produce an Lpp first class function object Conversions back are also easy for example 22 iL L 22 where iL conversion is back to C integers The programmer can use dynamic Lpp objects when needed and use simple C objects when absolute efficiency is needed Efficiency is an elusive property though For example Lpp strings seem less efficient on the surface but since they have a length they provide for faster string comparisons than ordinary 0 terminated
72. omplex Float Rational Ratio Integer BigInteger SmallInteger MinusBigInteger PlusBigInteger All of the number types fall into a more general type above them in the hierarchy For example a Rational is also a Number type and an Integer is also a Rational type and a Number type and so on and so forth In Lpp this can be demonstrated by the code let x L 18 typep x type Number gt t typep x type Integer gt t typep x type SmallInteger gt t typep x type BigInteger gt nil typep x type Ratio gt nil The Integer type is by far the most important for doing symbolic mathematics where overflow or underflow can not be tolerated such as systems that manipulate polynomials symbolically Thus integers can fall into the range of minus infinity to plus infinity Lpp implements this with the BigInteger type When an integer starts out in Lpp it can be a SmallInteger or BigInteger let x L 21 let y readFromString 2222222222222222221111111111111111111 l As of this writing the Complex and Float types have not been implemented Chapter 8 Numbers 34 In this example x starts off as a SmallInteger and y starts off as a BigInteger More specifically y is a PlusBigInteger print typeOf x gt lt Lpp Type for SmallInteger gt print typeO0f y gt lt Lpp Type for PlusBigInteger gt As Lpp math functions are applied to an integer it can expand from a SmallInteger into a BigInteger and vice verse
73. omponents are equalp if they are of the same type and corresponding components are equalp This test is implemented in a recursive manner and may fail to terminate for circular structures For conses equalp is defined recursively as the two car s being equalp and the two cdr s being equalp Here are some example of all these equality predicates eq L 23 L 23 gt nil eql L 23 L 23 gt t equal L 23 L 23 gt t eq L a LCa gt t eql L a LCa gt t equal L a LC A gt nil equalp L a LEA gt t eq S foo S foo gt t eql S foo S foo gt t equal S foo S foo gt t equal S foo S Foo gt nil equalp S foo S Foo gt nil eq1 L foo L foo gt nil equal L foo L foo gt t equal L foo L Foo gt nil equalp L foo L Foo gt t equalp S foo L foo gt nil Chapter 7 Symbols 31 7 Symbols Symbols are Lpp objects of type Symbol that have universal identity and usually stand for what they connote For example in a program the symbol red will be the same symbol as red in another program and would most likely connote the color red C enums do not have this property of universal identity Furthermore symbols are a lot more efficient than strings for this purpose since there is only one symbol object in a system per symbol name such as red When a symbol is introduced into the system it is said to be interned 7 1 The Print N
74. on it for real world complex problems presents difficulties One such problem is that it is impossible to write true generic code using virtual functions since all possible types must be accounted for The default virtual function of a base class can only serve the declared sub classes that do not define their own virtual functions Contrast this with Lisp functions that usually do not have to account for the types of the objects that it operates on As a very simple example Lisp can compute the length of a list irrespective of the types of the objects in the list Furthermore a provider could supply an object dynamically to a consumer of such a list that is not required to be seen by the compiler of the program operating on the list Lpp strives to provide the full power of Lisp in terms of dynamic typing The way that Lpp achieves this is that all Lpp objects contain a type header that is a pointer to a type meta object In some sense this is similar to the v table concept mentioned above for virtual function tables in that type meta objects can and do contain type dispatcher functions that are similar to virtual functions But the type meta objects are automatic in Lpp as well as much of their standard behavior And type meta objects are first class Lpp objects so that they can be manipulated dynamically as ordinary objects can For example a dispatcher function can be dynamically added to a type meta object and then operate on subsequent ordinary objec
75. ons where the car is the key of the pair and the cdr is the datum assoc key list Function assoc koy list test Function assoc searches for a cons in list whose car is eql to key If found the cons is returned otherwise nil is returned If the optional third argument test is given it must be an Lpp Function of two arguments and assoc will use it to do the test in place of eq1 For example let listi list cons L 1 L 2 cons L 2 L 4 cons L 3 L 6 assoc L 2 listi gt 2 4 assoc L 4 list1 gt nil let listi list list L 1 L 2 list L 2 L 4 gt list L 3 L 6 assoc L 2 list1 gt nil assoc L 2 list1 L stringEqual gt 2 4 Chapter 12 Hash Tables 54 12 Hash Tables A hash table is an Lpp object that can efficiently map any Lpp object to any other Lpp object Each hash table has a set of entries each which associates a particular key with a value Entries can be created removed modified and found given the key Since an optimal hash function and algorithm is used finding the value is very fast even if there are many entries 12 1 Hash Table Functions makeHashTable Function makeHashTable test Function makeHashTable test size Function makeHashTable returns a new Lpp hash table object If the optional argument test is given it should be a predicate function object of two arguments that returns nil unless the two arguments representing hash table keys are conside
76. ort for easy typing in a debugger In some debuggers such as gdb they can be evoked from a user defined macro which gives even more brevity pdb uses prini pdc uses princ and ppdb uses pprint For example assume we are debugging the following code in gdb let listi list L One L 2 S Three findJunk first listi lt Assume breakpoint here and assume that we had placed a breakpoint at the findJunk call Then after running and catching the breakpoint in gdb gdb p pdb listi One 2 Three gdb p pdb car list1 One gdb p pdc car list1 One Note the difference between the pdb and pdc printing of the String One the pdb version is quoted and the pdc version is not This reflects the standard Common Lisp difference between the way that Lisp objects such as strings print using princ versus print1 However the Lpp user is free to capitalize on this for his own objects for example by having one print method for standard C stream output princ and another for debugging inspection purposes And since print methods can be set dynamically in the object s type meta object the user can have any number of ways to print an object depending on the setting See Section 3 4 Accessing Type Meta Objects page 17 Chapter 17 Miscellaneous Features 65 17 3 Garbage Collection Lisp implementations have the advantage that all objects are accessible through some path originating in the symbol tables and the whole accessible
77. ory and neglects to deallocate the memory when it is no longer needed then a potential memory leak exists If a program has a memory leak in some code and then loops on that code then eventually the program will use up available memory and break with a memory allocation error In C one must either link with one of the many automatic garbage collector libraries or else each function must make sure which data it owns and manually deallocate the datas memory when it is no longer referenced Lpp provides some manual garbage collection functions which help See Section 17 3 Garbage Collection page 65 But the programmer must still be aware of how potential memory leaks can occur All Lpp data is allocated from the heap and not the stack So no Lpp data is automat ically deallocated by virtue of exiting a function Symbols and character objects though are not problematical There is only one set of character objects that are shared by all programs And symbols once interned are also shared by all programs So for example in the following code listi list S sym1 S sym1 S sym1 the symbol sym1 will only get allocated on its first internment Or in other words in the case of list1 the first S sym1 will intern the symbol sym1 and allocate it assuming it has not been interned previously and then return the symbol The second and third S sym1 then will only return the already interned symbol sym1 Usually the programmer will never have a need
78. ot truncate its value in any way use the truncate function for that See Section 8 5 Component Extractions on Numbers page 36 If divide is given two number that do not divide evenly it will return an Lpp Ratio of the form x y after it has been rationally canonicalized See Section 8 1 Number Types page 33 For example divide L 12 L 4 gt 3 evenly divides divide L 13 L 4 gt 13 4 doesn t so returns ratio divide 4 L 12 gt 1 3 returns canonicalized ratio truncate L 13 L 4 gt 3 Use truncate function to truncate inc x Function inc x delta Function dec x Function dec x delta Function These functions take an Lpp Number x and increments it in the case of inc and decre ments it in the case of dec The argument z is returned with the new value Normally the increment or decrement is by 1 however if an optional second argument delta is given it must be an Lpp Number or int and the increment or decrement is by delta instead of 1 Chapter 8 Numbers 36 Both inc and dec modify the object x so that most of the time a new object is not created However there are cases where the a new object is created This is when there is an object transition for example from a SmallInteger to a BigInteger See Section 8 1 Number Types page 33 So in the following inc x 1024 OK but x inc x 1024 this hadles object transition cases if there is no transition expected then the first is fine If a t
79. p hash tables In addition to these Lpp subtypes the user can define his own Lpp subtypes which can then be manipulated as let objects This is done with the classL macro that hides away the details of declaring such a class See Section 3 3 Defining New Subtypes page 16 Chapter 3 Subtypes 15 3 2 Conversions Conversions from primitive C types to Lpp objects is done with the L constructor and back to primitive C types is done with the zL converter where x designates a primitive C type Implicit constructors were avoided in Lpp for good reasons One major reason is that it eliminated overloaded name conflicts with other C libraries Another reason is that 0 as nil and 0 as integer is problematical This may seem like a real inelegancy For example the programmer has to specify list L 55 L string L fun instead of simply list 55 string fun but as it turns out this is not much of a problem in actual programming practice Also provided functions like listSEM one two three EM for long homogeneous lists of like objects helps The name of the L constructor can be redefined per compilation unit See Section 2 3 Redefining Predefined Names page 12 3 2 1 From C Primitive Types In Lpp the L macro is used for converting any basic C type to an Lpp object Lx Macro This macro returns an Lpp object that is analogous to the type of the argument z For example if x is a int then an Lpp Integer object is returned Not
80. pdb is provided for pretty printing such objects in a debugger Chapter 1 Introduction 4 1 2 Installing Lpp Lpp comes in a Gnu Tar Zipped file 1pp VER tgz where VER represents the version of Lpp See your system documentation on how to unpack it If we represent the directory in which you unpacked as UDIR then after unpacking there will be a directory UDIR lpp To make the Lpp library and accompaniments first go to the UDIR 1pp directory and type configure followed by make If both complete with no errors about missing components on your system then you are ready to install Lpp If you are installing in the default or a protected area you probably need to log in as a super user Then while in UDIR 1pp type make install If we refer to the default system installation directory as IDIR on Linux for example IDIR will default to usr local then the following will get installed in Linux subdirectories as follows IDIR doc lpp lpp html IDIR include lpp Lpp hh IDIR include lpp LppPure hh IDIR include lpp rpMenu hh IDIR info lpp info IDIR lib lpp libLpp a IDIR lib lpp librpMenu a The file UDIR 1pp doc INSTALL has a more general description of configure and make For example if you want to install in other than the default system installation directory then you can specify to install under some other directory IDIR as follows configure prefix IDIR make At this point while still in the UDIR directory you can install the additio
81. plist mapcan and mapcon the newly mapped list is returned If more that one list is given then the shortest list terminates the mapping mapcar fun lists Function maplist fun lists Function The lists lists are mapped over by fun and the mapped list is returned mapcar maps over successive elements of the lists which are passed to fun maplist maps over successive cdrs of the lists which are passed to fun For example let listi list L one L two L three mapcar L lengthL listi gt 3 3 5 let list2 list L cat L dogs L bears L lions mapcar L stringConcat listi list2 gt one cat two dogs three bears maplist L lengthL list1 gt 3 2 1 maplist L identity list1 gt one two three two three three mapc fun lists Function mapl fun lists Function The lists lists are mapped over by fun and the first list ie the second argument is returned mapc maps over successive elements of the lists which are passed to fun mapl maps over successive cdrs of the lists which are passed to fun For example let listi list L One L Two L Three let princOne let amp princ mapc L princOne list1 Prints on cout OneTwoThree The mapping functions mapcan and mapcon are the same as mapcar and maplist re spectively except that it is as if nconc were applied to the resulting values returned by fun It is important to notice that becau
82. r if integer index n is outside the boundary of sequence n can be either an int or Lpp Integer setElt sequence n value Function setElt sequence n value gc Function setElt sets the element at index n of sequence sequence to value value and value is returned It is an error if integer index n is outside the boundary of sequence n can be either an int or Lpp Integer If an optional fourth argument gc is given and is a function of one argument it is applied to the element replaced If argument gc is t then it defaults to the function gc If gc is nil then nothing is applied The optional fourth argument gc is ignored if sequence is a String subseq sequence start Function subseq sequence start end Function subseq returns a copy of the given sequence sequence from given start position index to the end of the sequence where 0 is the beginning of the sequence If the optional argument end is given then that is used instead of the end of the sequence The end position is not included in the returned sub sequence start and end can be either int or Lpp Integer objects If end is an Lpp object and is received by subseq as nil then that is also interpreted as the end of the sequence Some examples are subseq L abcdefg 3 gt defg subseq L abcdefg 3 Nil defg subseq L abcdefg 3 5 gt de subseq list L 1 L 2 L 3 0 1 gt 12 copySeq sequence Function copySeq returns a copy of the sequence sequence A
83. ransition is possible then the second must be used and is the recommended form most of the time When there is an object transition Lpp automatically garbage collects the defunct object and returns the new incremented or decremented object So in this case if the second code line above were not used then the new object would be lost and becomes a potential memory leak and x would point to deallocated memory negate x Function The negate function simply negates the Lpp number object that x points to While this is similar to inc and dec in that it modified the object x the object x is never transitioned to another type of object and hence does not have the transitioning concern In the following negate num Always OK num negate num Does the same thing but unnecessary other negate num the first is always safe to do and recommended however the second works also but in unnecessary The third would both negate num and set other to the negated num gcd xy Function The gcd function returns the greatest common divisor of x and y each of which must be an Lpp Integer or int type The result is always a positive Lpp Integer Here are some examples gcd 108 L 117 gt 9 gcd L 123 48 gt 3 gcd gcd a b c Returns the gcd of a b and c 8 5 Component Extractions on Numbers numerator x Function denominator x Function Given z an Lpp Rational number or int the numerator function returns the numerator of
84. red equal by the definition of the function Or test may be one of the symbols eq eql or equal which would correspond to the function of that symbol name If test is not given or nil it is assumed to be the eql function The optional argument size if given should be an positive integer argument specifying the size of the hash table size may be of type int or Lpp Integer If size is not given or nil then a default size of approximately 500 is used The user is encouraged here to either use the default size or study how to compute the optimal size of hash tables See Knuth Art of Computer Programming Although all sizes will work under Lpp some sizes will result in better distributions puthash key table value Function puthash creates an entry for key with value value in the hash table table If the key entry already exists in table then its value is replaced with value value is returned gethash key table Function gethash key table default Function gethash finds the entry for key in the hash table table and returns the entry value If not found it returns nil If the optional third argument default is specified and the key is not found default is returned instead of nil Here is an example let table makeHashTable let max L 100 let min L 0 puthash S red table list max min min puthash S green table list min max min gethash S blue table gt nil gethash S red table gt 100 O O rem
85. rgument is the Function myCollector so alisti is garbage collected while the function myCollector is applied to the leaf node objects 1 2 my bjecti myObject2 gcTree alisti L myCollector It is easy to garbage collect raw cons objects and their parts For example assume the variable var contains a cons then gcTree cdr var Collects just the whole cdr tree of var gc var Collects just the cons var and not the car or cdr There are many combinations of ways to collect cons structures and their parts But the following function fulfills a common case gcConsElement cons Function Given a cons in the cons argument gcConsElement will garbage collect the whole car tree of cons using gcTree and then garbage collect the cons itself It is useful for completely collecting ordinary list elements that are represented by cons structures and is used by some Lpp functions that optionally perform a garbage collection on destructive list operations such as nremove See Section 10 2 Modifying Sequences page 45 Chapter 18 Programming Cautions 68 18 Programming Cautions This chapter is more for programmers that are used to Lisp implementations and need to be aware of some idiosyncrasies of C as it relates to Lpp 18 1 Memory Leaks Infamous to C and C are those annoying memory leaks In Lisp a programmer does not have to worry about memory allocation but it is a different story in C When a program allocates mem
86. rt Function positionTest obj seq test start end Function positionTestL obj seq test Function positionTestL obj seq test start Function positionTestL obj seq test start end Function The positionTest and positionTestL functions are exactly the same as the position and positionL functions except that instead of testing for the object obj in the sequence using eq1 the function test is used test must be a function of two arguments the first being the object obj and the second an element of the sequence seq and must return non nil for when an object satisfies the test Here are some examples let stringi L abcabc position L a string1 gt position L c string1 position L a stringi 1 positionL L a string1 1 V I N gt 3 3 gt nil let listi list L 1 L 2 L three L 4 position L 4 listi gt 3 position L three list1 gt 1 positionL L three listi gt nil positionTest L three listi L equal gt 2 Chapter 10 Sequences 48 10 4 Efficient Sequence Functions All sequence functions must dispatch dynamically on the specific type of sequence passed into the sequence function call T his is a very small overhead especially in Lpp However for absolute efficiency any sequence function can be typed for a specific sequence type and this dispatching overhead will not be incurred For example in length sequence if we only expect sequence to be an Lpp String th
87. s can be input and output to standard C streams For example cout lt lt lt lt relative outputs the value of relative to the C standard output stream cout The variable relative is dynamically bound to an Lpp object in the dolist macro Compiling the whole data base program above and running produces the following out put Report on Smith family Person Joseph Clyde Smith is 42 years old With siblings frank mary Person Frank Bob Smith is 32 years old With siblings joe mary Person Mary Ann Smith is 28 years old With brothers joe frank 1 5 Pure Lisp Include File If you do not plan to use any C stream 1 0 operators or C stream type arguments in any given C file then you can use include lt LppPure hh gt instead if Lpp hh This will result in slightly faster compiles and slightly smaller binaries LppPure hh is the Pure Lisp include file and Lpp hh includes it LppPure hh does not include any files it is totally self contained If in a given compilation unit you are using no I O or are only using Lisp style I O such as read print print etc then LppPure hh is sufficient The Lpp library itself for example uses LppPure hh in most of its compilation units In this document whenever we talk about setting up optional compilation parame ters before including the Lpp hh file this also applies to wherever you are only including LppPure hh If none of this matters to you you can just use
88. same as stringEqual except that character case is ignored For example stringEQUAL L foo L Foo gt t 13 2 String Construction and Manipulation makeString size Function makeString size char Function makeString returns a new string of given size which can be an int or Lpp Integer The string characters are not initialized unless the optional argument char is given in which case the whole string is initialized to characters of char which must be an Lpp Character object stringConcat sl s2 Function stringConcat returns a new Lpp String which is a concatenation of Lpp Strings s1 and s2 For example stringConcat L one L two gt one two Chapter 14 Input Output 57 14 Input Output Lpp provides typical Common Lisp functions for doing input output of Lpp objects such as print read etc In addition to these Lpp objects can also be output to C streams automatically with no extra work 14 1 Printed Representation of Lpp Objects All predefined Lpp objects have print and princ methods that print the object as you would expect in Common Lisp For example the following code for printing some Lpp objects prini L Hello world terpriO princ L Hello world print cons S a S b would produce on cout Hello world Hello world a b As described in this manual See Section 3 4 Accessing Type Meta Objects page 17 the programmer can dynamically set the prini and princ methods of his objects
89. se of this the values returned by fun are concatenated together by destructively modifying them Usually the function fun creates and returns new lists each time being aware that they will be modified by being concatenated together mapcan fun lists Function mapcon fun lists Function The lists lists are mapped over by fun and the values returned by fun are concatenated together as if by using nconc and the concatenation is returned mapcan maps over successive elements of the lists which are passed to fun mapcon maps over successive cdrs of the lists which are passed to fun mapcan and mapcon can be used for combining or filtering For example Chapter 5 Control Structure 26 Example of filtering let passNums let x fif numberp x return list x return Nil let listi list L 1 S a L 2 S b L 3 S c mapcan L passNums list1 gt 1 2 3 Example of combining listi readFromString 1 2 3 4 5 6 7 mapcan L copyList listi gt 123456 9 10 8 7 8 9 10 Chapter 6 Predicates 27 6 Predicates An Lpp predicate is a function that returns either nil or a non nil value based on a test of its given arguments The return of a non nil value implies true and the return of nil implies false Things are done this way since a check for nil or non nil is always efficient and uniform Any other Lpp type let value other than nil is considered non nil This allows useful values to be returned that at t
90. se probeFile for was as a predicate Chapter 16 Errors 63 16 Errors error string args Function error takes a string argument string and 0 or more optional arguments args and reports an Lpp error message using string and enters an error handler The string argument can be either an Lpp String or char string and it may contain printf style substitution specifiers that correspond to the optional args just as in printf For example if type0f object type MyType 1 char objectID sL priniToString object char expectType typeName type MyTupe error Object s is not of type s objectID expectType Chapter 17 Miscellaneous Features 64 17 Miscellaneous Features 17 1 Identity Function identity object Function This function simply returns the object object This function is useful for debugging or when a Function object is needed by some other function and no operation more than identity is needed See the maplist example using identity See Section 5 2 2 Mapping page 24 17 2 Debugging Tools Lpp provides debugging tools primarily to make it easier to debug programs using Lpp in run time debuggers like gdb Gnu Debugger pdb object Function pdc object Function ppdb object Function These functions can be used in a debugger to print and inspect Lpp objects Each takes one argument object which is any Lpp object or 0 note that 0 prints as nil The names of these functions have been kept sh
91. st i 1 if list is circular let i listLengthL list i nil if list is circular We mention the unfortunate situation above but from another perspective the functionL function name comments the fact that an Lpp object will be generated on the heap To illustrate this following is bad if listLengthL list then do something Although it would work the listLengthL returned value is not referenced anywhere and hence would cause a memory leak It would be better done with let len listLengthL list if len then do something or if the Lpp Integer object is not needed if listLength list 1 then do something 2 3 Redefining Predefined Names Lpp defines a few identifiers that are only one or two letters for ease of typing since they are so frequently used in Lpp programs By using capital letters as the first or second letter of these identifiers it is unlikely that there will be identifier clashes but in the event that there are clashes Lpp allows all of these to be easily redefined It is not recommended to redefine these if there are no clashes but nothing will hurt except non consistency between Lpp programs The following are reserved identifiers that can be redefined per compilation unit Chapter 2 Name Space Issues 13 True Nil O0 8 L cL iL pL sL EM Assuming that XX is the user s preferred redefinition each can be renamed by putting one set of the following definitions in the compilation stream before
92. t called someFun In general any function object can be generated from any C function with an address non inline of type let with 0 or more let type arguments Note that Lpp functions that return ints can not be used but they have counterparts that can For example length can not but lengthL can Such function objects can be called using either funcall or apply See Chapter 5 Con trol Structure page 24 For example if in the previous example someFun given two argu ments returns some list then let test let a let b let functionList 1 return funcall nth 0 functionList funcall nth 1 functionList a b would return the car of that list Actually notice that test in this example could be used to apply any Lpp function of one argument to any Lpp object returned by any function of two arguments Using first class function objects as variables adds an important dimension to programming ARGSn Macro Where n is the number of function arguments For example ARGSO ARGS1 ARGS2 etc This macro makes it easy to cast function objects of functions that can have more than one argument such as list For example let fun L ARGS3 list print fun gt lt Lpp Function of 3 args funcall fun S a S b S c gt a b c Lpp only supplies up to 10 arguments for such functions but a quick look at the Lpp in clude file LppPure hh makes it apparent that any number of arguments can be added easily However very rarely are more t
93. t operations 11 1 Conses As mentioned in the preceding section conses can serve as fundamental building blocks of lists association lists dotted lists trees and sets A single cons is created with the cons function cons x y Function The cons function returns a new cons whose car is z and cdr is y For example cons S a S b gt a b cons S a list S b S c S d gt abc a The accessors of a cons are the car and cdr functions The car and cdr of a cons can be set with the rplaca replace car and rplacd replace cdr functions car list Function This returns the car of list which must be either a cons or nil The car of nil returns nil For example car cons S a S b gt a car list L 1 LO L 3 gt 1 Chapter 11 Lists 50 cdr list Function This returns the cdr of list which must be either a cons or nil The cdr of nil returns nil For example cdr cons S a S b gt b cdr list L 1 L0 L 3 gt 2 3 caar cons Function cadr cons Function cdar cons Function cddr cons Function These functions are a convenience but also are important as commonly passed function objects They perform successive car and or cdr operations of the cons argument cons and are defined by the following equivalents caar x car car x cadr x car cdr x cdar x cdr car x cddr x cdr cdr x rplaca xy Function The argument x must be a cons and y can be any Lpp object
94. te on either lists or vectors Strings are currently the only vectors in Lpp Many sequence functions take an optional test argument that is a predicate function of two arguments that tests for the action of the function to take place otherwise eq1 is used For example in remove obj sequence remove obj sequence test The first remove will remove the elements of sequence that are eq1 to the object obj But in the second remove where the function object test is given then that is used for the comparison instead of eql The rule here is that where the test function object is of the form test obj el The single object obj to compare is passed as the first argument and the second argument el being an element of the sequence to compare to is passed as the second argument This ordering is important for example if the test function was lessThan or a test function that compares two different object types by analyzing their components Chapter 10 Sequences 44 10 1 Simple Sequence Functions length sequence Function lengthL sequence Function length returns the length of the sequence sequence as an int lengthL is the same but returns an Lpp Integer object as the length For example length 0 gt 0 let listi list L one L two L three list lengthL listi gt 3 length listi 1 gt 4 length nth 2 list1 gt 5 elt sequence n Function elt returns element at index n of sequence sequence It is an erro
95. therwise zerop x Function Tests for the condition that the given number z is zero plusp x Function Tests for the condition that the given number x is non zero and positive minusp x Function Tests for the condition that the given number z is non zero and negative oddp x Function Tests for the condition that the given number x is odd Chapter 8 Numbers 35 evenp x Function Tests for the condition that the given number x is even 8 3 Comparison on Numbers Numbers can be compared with the following functions lessThan x y Function greaterThan x y Function lessThanOrEqual x y Function greaterThan0rEqual x y Function These functions take Lpp Number objects for arguments x and y and return non nil if the comparison is true and nil otherwise For each of these functions respectively non nil is returned if x is less than y x is greater than y x is less than or equal to y x is greater than or equal to y Note that the equality of Numbers can be checked using equal and more efficiently using eql See Section 6 5 Equality Predicates page 29 8 4 Arithmetic Operations Basic arithmetic can be done on Lpp Numbers with the following plus x y Function minus x y Function times x y Function divide x y Function In these functions r and y must each be Lpp Numbers or int types and the returned result is a new Lpp Number Their names describe what they do It it worth mentioning however that divide does n
96. ti type MySubType To pass the specific type of any new Lpp object down through the Lpp type lattice any constructor in a classL or classLS definition must pass in a constructor for the super type with the subtype as an argument For classL this would be the Let supertype and for Chapter 3 Subtypes 17 classLS the user defined Lpp type Recall that the Lpp type can be referenced using the type macro So for a new user defined Lpp type T the super constructor needed for classL would be Let type T Or using our example above for MyType and MySubType the class constructors might appear as follows MyTypeO Let type MyType 1 MySubTypeO MyType type MySubType makeInstance type Macro Returns a new Lpp object of type type The argument type can be a C constructor form or just the type name The constructor or type name must be of an Lpp type Since all Lpp object subtypes must have a type meta object See Section 3 4 Accessing Type Meta Objects page 17 the Type object must be generated somewhere usually in the C file that defines the accessors for the subtype This can be done easily with the genT or genTS macros genT type Macro genTS type supertype Macro The genT macro generates a type meta object of type type The genTS macro generates a type meta object of type type whose supertype is supertype For example classL MyType The type meta object for a MyType is generated here genT MyType
97. to deallocate symbols since the nature of their semantics is to be persistent within the full extent of the program Symbols can be deallocated with unintern if it is absolutely necessary See Section 7 2 Creating Symbols page 31 To be safe then all other Lpp objects should be garbage collected when known to be no longer referenced or else simulate the Symbol semantics For example if in a function we reference a commonly used string like the following let b stringConcat a L something common and that function can be called repeated times then the string should either be made static static let common string L something common let b stringConcat a common string or alternatively it can be manually garbage collected let common string L something common let b stringConcat a common string gc common string Chapter 18 Programming Cautions 69 otherwise there will be no reference to such strings allocated and when the function exits the string objects would exist in memory until the program exits The manual garbage collection in this last case is a bad choice since we know that the same string will be used over and over again The static variable containing the common string would be a better choice But in a case where there is an intermediate value whose value we can not predict then a manual garbage collection would be more appropriate The following for example would be a such a memory leak let a min
98. try and the menu is presented again If the user enters an integer corresponding to a choice that integer is returned from chooseRpMenu as in int rpDsMenu spec Function This function returns an Lpp object representing a Dynamic Symbolic menu based on the specification spec provided The menu object returned can be used in subsequent calls to chooseRpMenu where the user needs to make a menu selection spec is a list of Lpp Strings The first entry in the list will be the menu prompt The rest of the list is alternating symbol description Strings where symbol will be the expected symbol that the user is expected to enter for that choice and description is a String describing the choice When chooseRpMenu is called the user will be presented with the prompt If the user enters anything other than one of the symbols corresponding to a choice the user is notified of a bad entry and then menu of choices with symbol description pairs is presented If the user enters a symbol corresponding to a choice then an integer is returned from chooseRpMenu as an int The integer corresponds to the position in the spec list of choices where the first choice will return 0 the second 1 etc chooseRpMenu menu Function This function is given the argument menu which is either an rpSnMenu or an rpDsMenu The menu or prompt is presented as indicated above and when the user makes an entry an int of 0 to n is returned where there are n 1 choices in the menu The fo
99. ts of that type Furthermore the type meta objects maintain a run time type lattice For example at run time a program can query whether an object is a number or an integer integers being a subset of all numbers All Lpp objects as in Lisp have the same base type In Lisp the base type is type t In Lpp the base type is intentionally left unknown and abstracted with a type definition called Chapter 1 Introduction 2 let This was done so that the base type can be experimented with while preserving Lpp syntax Type let serves as the declaration type for all such Lpp objects when referring to the Lpp object as a generic object As in Lisp Lpp objects are passed around as pointers with pointer abstraction in Lpp function arguments and Lpp slots These objects can be mixed freely with other C classes and class members and can be used with other C libraries that do not use the same class names that Lpp uses Lpp provides a the macro for descending into specific Lpp objects This is similar to the the special form in Common Lisp Lpp automatically does a dynamic type check on the object wrapped in the macro which assures that specific object member references will always be correct In a debugged program compilation unit this type checking can be turned off for maximum compilation efficiency Lpp also provides other useful Lisp objects such as Cons cells lists symbols strings characters and numbers As much as possible Lpp tries to use the C
100. us d times b c where b c and d are input variables The memory leak would be where the times returns value That value is not referenced anywhere after the minus is executed So to prevent a memory leak here this should be coded instead as let temp times b c let a minus d temp gc temp The kinds of potential memory leaks mentioned above are relatively simple to find and fix But in the real world where large complex data structures are passed around memory leak bugs can be quite difficult to pin down It is a classical problem of which function owns what data when Lpp provides some assist to this by making it easier to sweep up large branches of data that are no longer referenced with functions like gcList and gcTree but the programmer must carefully design the growth and collapse of such complex structures so as to avoid memory leaks 18 2 Order of Evaluation In Lisp the order of evaluation of function arguments is strictly left to right Sometimes this is important since successive argument evaluation may depend on earlier arguments Unfortunately in C the order of evaluation is undefended Therefore the programmer must not depend on this For example in the following code let a L 5 list inc a a dec a the list function could return 10 9 or 8 4 depending on the C implementation There is no standard guideline here other than to watch out for such order of evaluation problems and if necessary re code i
101. ype meta object contains various functions that dispatch on the type of a given object dynamically These are similar to virtual functions but are more powerful in that they can be manipulated dynamically Lpp provides some built in type dispatching functions for things such as printing and equality of objects All Lpp Type meta objects when created have default dispatching functions generated The user however may access these dispatching functions or set them to new dispatching functions dynamically In general there are slots in the Type meta object that can be accessed by functions of the form SetTypeXXXX type data getTypeXXXX type where XXXX refers to the kind of data being accessed in the given type where type is an Lpp Type meta object And setTypeXXXX sets the data and getTypeXXXX returns the data data last set When setting data is given as nil it usually gets interpreted as setting back to the a default This setting and getting can be done dynamically at run time Here are some examples let original getTypePrinc type MyType setTypePrinc type MyType L MyTypePrincFunction setTypePrinc type MyType Nil setTypePrinc type MyType original The first line sets original to the current princ printer function of MyType The second line sets the princ printer function of MyType to MyTypePrincFunction The third line would set the princ printer function to the Lpp default And the last line sets the princ printer function back to

Download Pdf Manuals

image

Related Search

Related Contents

User Manual FLARM V1.00 German    Manual del Usuario para el Correo Electrónico del Recinto Metro  é è ê í ë  User Manual - ADFWeb.com  Plesk 8.3 für Linux/Unix E-Mail  INSTALLATION, MAINTENANCE AND USE  HL-EL520 取扱説明書  

Copyright © All rights reserved.
Failed to retrieve file