practical common lisp

14
PRACTICAL COMMON LISP Peter Seibel http://www.gigamonkeys.com/book/ 1

Upload: rainer

Post on 11-Jan-2016

42 views

Category:

Documents


2 download

DESCRIPTION

PRACTICAL COMMON LISP. Peter Seibel http://www.gigamonkeys.com/book/. OUTLINE. CHAPTER 1 Introduction: Why Lisp? CHAPTER 2 Lather, Rinse, Repeat: A Tour of the REPL CHAPTER 3 Practical: A Simple Database CHAPTER 4 Syntax and Semantics CHAPTER 5 Functions CHAPTER 6 Variables - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: PRACTICAL COMMON LISP

PRACTICAL COMMON LISP

Peter Seibelhttp://www.gigamonkeys.com/book/

1

Page 2: PRACTICAL COMMON LISP

OUTLINE

CHAPTER 1 Introduction

CHAPTER 2 Lather, Rinse, Repeat: A Tour of the REPL

CHAPTER 3 Practical: A Simple Database

CHAPTER 4 Syntax and Semantics

CHAPTER 5 Functions

CHAPTER 6 Variables

CHAPTER 7 Macros: Standard Control Constructs

CHAPTER 8 Macros: Defining Your Own

CHAPTER 9 Practical: Building a Unit Test Framework

CHAPTER 10 Numbers, Characters, and Strings

CHAPTER 11 Collections

CHAPTER 12 They Called It LISP for a Reason: List Processing

CHAPTER 13 Beyond Lists: Other Uses for Cons Cells

CHAPTER 14 Files and File I/O

2

Page 3: PRACTICAL COMMON LISP

GNU CLISP

Common Lisp is a high-level, general-purpose,

object-oriented, dynamic, functional programming

language.

CLISP is a Common Lisp implementation by Bruno

Haible, then of Karlsruhe University, and Michael Stoll,

then of Munich University, both in Germany.

CLISP implements the language described in the ANSI

Common Lisp standard with many extensions.

http://clisp.sourceforge.net/ http://www.clisp.org/ 3

Page 4: PRACTICAL COMMON LISP

CHAPTER 1 INTRODUCTION: WHY LISP

4

Page 5: PRACTICAL COMMON LISP

WHY LISP? Common Lisp is the programmable programming language.

Common Lisp follows the philosophy that what's good for the language's designer is good for the language's users.

A Common Lisp program tends to provide a much clearer mapping between your ideas about how the program works and the code you actually write. You will develop code more quickly. There's less code to write. You do not waste time thrashing around trying to find a clean

way to express yourself within the limitations of the language.

Common Lisp is an excellent language for exploratory( 探究 ) programming. Common Lisp provides several features to help you develop

your code incrementally and interactively. 5

Page 6: PRACTICAL COMMON LISP

WHERE IT BEGAN? LISP: LISt Processing Common Lisp is the modern descendant of the Lisp language

first conceived by John McCarthy in 1956.

Who this book is for ? This book is for you if you're curious(渴望知道 ) about Common Lisp. After you finish this book,

you'll be familiar with all the most important features of the language and how they fit together,

you'll have used Common Lisp to write several nontrivial programs, and

you'll be well prepared to continue exploring the language on your own.

While everyone's road to Lisp is different, I hope this book will help smooth the way for you. 6

Page 7: PRACTICAL COMMON LISP

CHAPTER 2 LATHER, RINSE, REPEAT: A TOUR OF THE REPL

7

Page 8: PRACTICAL COMMON LISP

INSTALL COMMON LISP Download GNU Clisp and install it

http://clisp.sourceforge.net/

Our official distribution sites

http/SF (sources and win32)

Experimenting in the REPL REPL: read-eval-print loop

CL-USER> This is the Lisp prompt (提示符號 ). Lisp reads Lisp expressions, evaluates them according to the rules of

Lisp, and prints the result. Then it does it again with the next expression you type. That endless cycle of reading, evaluating, and printing is why it's called

the read-eval-print loop (REPL).8

Page 9: PRACTICAL COMMON LISP

EXPERIMENTING IN THE REPL

9

Page 10: PRACTICAL COMMON LISP

“HELLO, WORLD,” LISP STYLE CL-USER> "hello, world" "hello, world"

Lisp reads the double-quoted string and instantiates a string object in memory that, when evaluated, evaluates to itself and is then printed in the same literal syntax.

CL-USER> (format t "hello, world") hello, world NIL

The FORMAT function takes a variable number of arguments, but the only two required arguments are the place to send the output and a string.

The first argument to FORMAT should be the symbol T when we want to write to the display (*STANDARD-OUTPUT*).

The second argument must be a string, called the format control string.

http://www.gigamonkeys.com/book/a-few-format-recipes.html10

Page 11: PRACTICAL COMMON LISP

“HELLO, WORLD,” LISP STYLE CL-USER>(write-line "hello, world")

Break 1 [7]> (write-line "hello, world")hello, world"hello, world"

CL-USER>(print "hello, world")Break 1 [7]> (print "hello, world")"hello, world""hello, world"

11

Page 12: PRACTICAL COMMON LISP

“HELLO, WORLD,” LISP STYLE CL-USER> (defun hello-world () (format t "hello, world"))

HELLO-WORLD DEFUN is a special kind of function, called a macro

function. DEFUN is used to define other functions. The first input to DEFUN is the name of the function being

defined. The second input is the argument list: It specifies the

names the function will use to refer to its arguments. The remaining inputs to DEFUN define the body of the

function: what goes on ''inside the box.''

CL-USER> (hello-world)

hello, world

NIL12

Page 13: PRACTICAL COMMON LISP

“HELLO, WORLD,” LISP STYLE Examples:

CL-USER>(defun average (x y) (/ (+ x y) 2.0)) CL-USER>(average 4 6)

CL-USER>(defun square (n) (* n n)) CL-USER>(square 6)

13

Page 14: PRACTICAL COMMON LISP

SAVING YOUR WORK If you exit Lisp and restart, the function definition will be gone. Having written such a fine function, you'll want to save your

work. Loading a text file:

CL-USER>(load "C:/CLISP/filename.lisp") The output T means everything loaded correctly.

CL-USER>(load (compile-file "ch1.lisp") A way to load a file's worth of definitions is to compile the

file first with COMPILE-FILE and then LOAD the resulting compiled file, called a FASL file.

;; Compiling file C:\CLISP\ch1.lisp ... ;; Wrote file C:\CLISP\ch1.fas 0 errors, 0 warnings ;; Loading file C:\CLISP\ch1.fas ... ;; Loaded file C:\CLISP\ch1.fas T

CL-USER>(exit) ;;close the CLISP

14