
Introduction to newLISP en.wikibooks.org December 29, 2013 On the 28th of April 2012 the contents of the English as well as German Wikibooks and Wikipedia projects were licensed under Creative Commons Attribution-ShareAlike 3.0 Unported license. A URI to this license is given in the list of figures on page 199. If this document is a derived work from the contents of one of these projects and the content was still licensed by the project under this license at the time of derivation this document has to be licensed under the same, a similar or a compatible license, as stated in section 4b of the license. The list of contributors is included in chapter Contributors on page 197. The licenses GPL, LGPL and GFDL are included in chapter Licenses on page 203, since this book and/or parts of it may or may not be licensed under one or more of these licenses, and thus require inclusion of these licenses. The licenses of the figures are given in the list of figures on page 199. This PDF was generated by the LATEX typesetting software. The LATEX source code is included as an attachment (source.7z.txt) in this PDF file. To extract the source from the PDF file, you can use the pdfdetach tool including in the poppler suite, or the http://www. pdflabs.com/tools/pdftk-the-pdf-toolkit/ utility. Some PDF viewers may also let you save the attachment to a file. After extracting it from the PDF file you have to rename it to source.7z. To uncompress the resulting archive we recommend the use of http://www.7-zip.org/. The LATEX source itself was generated by a program written by Dirk Hünniger, which is freely available under an open source license from http://de.wikibooks.org/wiki/Benutzer:Dirk_Huenniger/wb2pdf. Contents 1 Introduction to newLISP 3 1.1 Welcome to newLISP ............................... 3 1.2 Resources ..................................... 3 2 The basics 5 2.1 Downloading and installation .......................... 5 2.2 Getting started .................................. 5 2.3 The three basic rules of newLISP ........................ 7 2.4 Rule 1: a list is a sequence of elements ..................... 7 2.5 Rule 2: the first element in a list is special ................... 7 2.6 Rule 3: Quoting prevents evaluating ...................... 10 2.7 Destructive functions ............................... 13 3 Controlling the flow 15 3.1 Tests: if... ..................................... 15 3.2 Looping ...................................... 18 3.3 Blocks: groups of expressions .......................... 23 3.4 ambiguity: the amb function .......................... 25 3.5 Selection: if, cond, and case ........................... 26 3.6 Variables local to a control structure ...................... 27 3.7 Make your own functions ............................ 28 3.8 Local variables .................................. 30 4 Lists 37 4.1 Building lists ................................... 37 4.2 Working with whole lists ............................. 40 4.3 List analysis: testing and searching ....................... 44 4.4 Selecting items from lists ............................. 54 4.5 Implicit addressing ................................ 56 4.6 List surgery .................................... 57 4.7 Find and replace matching elements ...................... 61 4.8 Find and replace all matching elements: set-ref-all . 61 4.9 Working with two or more lists ......................... 63 4.10 Association lists .................................. 64 5 Strings 71 5.1 Strings in newLISP code ............................. 71 5.2 Making strings .................................. 72 5.3 String surgery ................................... 74 5.4 Substrings ..................................... 75 III Contents 5.5 Modifying strings ................................. 77 5.6 Testing and comparing strings .......................... 81 5.7 Strings to lists .................................. 84 5.8 Parsing strings .................................. 84 5.9 Other string functions .............................. 86 5.10 Formatting strings ................................ 86 5.11 Strings that make newLISP think ........................ 87 6 Apply and map: applying functions to lists 89 6.1 Making functions and data work together ................... 89 6.2 Write one in terms of the other? ........................ 91 6.3 More tricks .................................... 92 6.4 Lispiness ...................................... 93 6.5 currying ...................................... 94 7 Introducing contexts 97 7.1 What is a context? ................................ 97 7.2 Contexts: the basics ............................... 97 7.3 The default function ............................... 100 7.4 Dictionaries and tables .............................. 103 7.5 Saving and loading contexts . 105 7.6 Using newLISP modules ............................. 105 7.7 Scoping ...................................... 106 7.8 Objects ...................................... 107 8 Macros 113 8.1 Introducing macros ................................ 113 8.2 When do things get evaluated . 113 8.3 Tools for building macros ............................ 114 8.4 Symbol confusion ................................. 114 9 Working with numbers 119 9.1 Integers and floating-point numbers . 119 9.2 Integer and floating-point maths ........................ 120 9.3 Conversions: explicit and implicit ........................ 121 9.4 Invisible conversion and rounding ........................ 122 9.5 Number testing .................................. 123 9.6 Absolute signs, from floor to ceiling . 124 9.7 Number formatting ................................ 125 9.8 Number utilities ................................. 125 9.9 Sequences and series ............................... 125 9.10 Random numbers ................................. 126 9.11 Randomness .................................... 127 9.12 Trigonometry ................................... 129 9.13 Arrays ....................................... 130 9.14 Getting and setting values ............................ 132 9.15 Matrices ...................................... 133 9.16 Statistics, financial, and modelling functions . 134 IV Contents 9.17 Bayesian analysis ................................. 135 9.18 Financial functions ................................ 136 9.19 Logic programming ................................ 137 9.20 Bit operators ................................... 137 9.21 Bigger numbers .................................. 138 10 Working with dates and times 141 10.1 Date and time functions ............................. 141 10.2 The current time and date ............................ 141 10.3 Timing and timers ................................ 143 11 Working with files 145 11.1 Interacting with the file system . 145 11.2 Reading and writing data ............................ 149 12 Working with pipes, threads, and processes 155 12.1 Processes, pipes, threads, and system functions . 155 12.2 Reading and writing to threads . 158 12.3 Communicating with other processes . 159 13 Working with XML 161 13.1 Converting XML into lists ............................ 161 13.2 Outputting SXML to XML . 167 13.3 A simple practical example . 168 14 The debugger 171 15 The internet 175 15.1 HTTP and networking .............................. 175 15.2 Accessing web pages ............................... 176 15.3 A simple HTML Form .............................. 176 15.4 A simple IRC client ............................... 177 16 More examples 179 16.1 On your own terms ................................ 179 16.2 Using a SQLite database ............................. 180 16.3 Simple countdown timer ............................. 183 16.4 Editing text files in folders and hierarchies . 186 16.5 Talking to other applications (MacOS X example) . 187 17 Graphical interfaces 191 17.1 Introduction .................................... 191 17.2 A simple application ............................... 191 18 Contributors 197 List of Figures 199 19 Licenses 203 19.1 GNU GENERAL PUBLIC LICENSE . 203 V Contents 19.2 GNU Free Documentation License . 204 19.3 GNU Lesser General Public License . 205 1 1 Introduction to newLISP 1.1 Welcome to newLISP Welcome to this introduction to newLISP! You'll find newLISP easy to learn and powerful, combining some of the power and elegance of classic LISP with the facilities of a mod- ern scripting language, such as regular expressions, network functions, Unicode support, multitasking, and many others. This book is a straightforward and simple description of the basics of the language. You should be familiar with using a text editor, and ideally you'll have done a bit of scripting before, but previous programming experience or knowledge of LISP isn't required. I hope it's enough to get you started, and ready to start exploring the real power of newLISP. I'm writing this on a MacOS X system, but it shouldn't make any difference if you're using Linux, Windows, or one of the many other platforms that newLISP supports. The examples are designed to be run in newLISP version 10. This is an unofficial document - for the official and definitive description of newLISP refer to the excellent reference manual that is installed with the software. 1.2 Resources • the main newLISP web site, newlisp.org1, which provides a helpful forum, code examples and documentation, and the latest newLISP software • the newLISP on Noodles wiki, newlisp-on-noodles.org2 • John Small's excellent 21 minute introduction, newLISP in 21 minutes, at newLISP in 21 minutes3 • the fine newLISP dragonfly logo, fashioned from 11 pairs of parentheses,
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages210 Page
-
File Size-