Upload
phungduong
View
228
Download
0
Embed Size (px)
Citation preview
Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. Summer semester 2010. 25.3.20101
2. Crash Course on Lisp Language-Oriented Programming
Prof. Dr. Bernhard HummFaculty of Computer ScienceHochschule Darmstadt – University of Applied SciencesSummer semester 2010
Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. Summer semester 2010. 25.3.20102
This lecture in the context of the entire course
1. Introduction
2. Lisp crash course
3. Functional programming
4. Advanced object-oriented programming
5. Database programming
6. Logic programming
7. Workflows
8. User interfaces
9. More Domain-Specific Languages
Agenda
• S-Expressions and Data Types
• Functions
• Control Structures
• Package Handling
• Testing
• Language Conventions
• References
Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. Summer semester 2010. 25.3.20103
Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. Summer semester 2010. 25.3.20104
LISP in a nutshell
• LISP is very simple: only 2 kinds of data objects
1. atoms (identifiers/constants) green 12.5
2. lists (of atoms and sublists) (1 2 3.14)
• Functions and function calls are represented as lists (i.e., program = data)
(define-function (square x) (* x x)) ;; cl:defun
• All computation is performed by applying functions to arguments, also as lists
(+ 2 3) evaluates to 5
(square 5) evaluates to 25
(first (reverse '(a b c))) evaluates to c
Slides adopted from D. Reed, Creighton University, Omaha, US
Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. Summer semester 2010. 25.3.2010
S-expressions
• in LISP, data & programs are all of the same form: S-expressions
(Symbolic-expressions)
• an S-expression is either an atom or a list
<S-expression> ::= <atom> | <list>
<atom> ::= <number> | <identifier>
<list> ::= ( <S-expressions> )
<S-expressions > ::= <empty>
| <S-expressions > <S-expression>
<number> ::= <digit> | <number> <digit>
<identifier> ::= string of printable characters, not including
parentheses
5
Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. Summer semester 2010. 25.3.20106
Atoms
• Atoms are:
- numbers 4 3.14 1/2 #xA2 #b1001
- characters #\a #\Q #\space #\tab
- strings "foo" "Dave Reed" "@%!?#"
- Booleans true (cl:t) false (cl:nil)
- symbols Dave num123 miles->km !_^_!
• Symbols are sequences of letters, digits, and "extended alphabetic
characters"
• + - . * / < > = ! ? : $ % + & ~ ^
- can't start with a digit
Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. Summer semester 2010. 25.3.20107
Lists
• () is a list (the empty list nil)
• (L1 L2 . . . Ln) is a list, where each Li is either an atom or
a list
• Lists can store different types, not contiguous, not random access
• Examples:
- ()
- (a)
- (a b c d)
- ((a b) c (d e))
- (((((a)))))
Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. Summer semester 2010. 25.3.20108
Data types in LISP
• LISP is dynamically typed: types are associated with values rather
than variables, bound dynamically
• numbers can be described as a hierarchy of types
- number
- complex
- real
- rational
- integer
• integers and rationals are exact values, others can be inexact
• (+ 3 1/2) 7/2
• (+ 3 0.5) 3.5
Agenda
• S-Expressions and Data Types
• Functions
• Control Structures
• Package Handling
• Testing
• Language Conventions
• References
Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. Summer semester 2010. 25.3.20109
Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. Summer semester 2010. 25.3.201010
Functional expressions
• Computation in a functional language is via function calls (also S-exprs)
(FUNC ARG1 ARG2 . . . ARGn)
(+ 3 (* 4 2))
(first'(a b c))
• Evaluating a functional expression:
- function/operator name & arguments are evaluated in unspecified order
- note: if argument is a functional expression, evaluate recursively
- the resulting function is applied to the resulting values
(first '(a b c))
- so, primitive first function is called with argument (a b c)
evaluates to primitive function
evaluates to list (a b c) : ' terminates recursive evaluation
quote specifies data, not to be evaluated further
Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. Summer semester 2010. 25.3.201011
Arithmetic primitives
• Predefined functions:
- + - * /
- quotient remainder modulo
- max min abs gcd lcm expt
- floor ceiling truncate round
- = < > <= >=
• Many of these take a variable number of inputs
- (+ 3 6 8 4) 21
- (max 3 6 8 4) 8
- (= 1 (- 3 2) (* 1 1)) true ;; cl:t
- (< 1 2 3 4) true ;; cl:t
• Functions that return a true/false value are called predicate functions
- (is-even 5) ;; cl:evenp false ;; cl:nil
- (is-odd 5) ;; cl:oddp true ;; cl:t
Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. Summer semester 2010. 25.3.201012
List primitives
•Predefined functions:
-first rest add (cl:cons)
-list length member
-reverse append is-equal (cl:equalp)
•Examples:-(list 'a 'b 'c) (a b c)
-(add 1 '(2 3)) (1 2 3)
-(first '(1 2 3) 1
-(rest '(1 2 3) (2 3)
-(member 'b '(a b c)) (b c); i.e., true
-(member 'd '(a b c)) nil; i.e., false
-(equal 'a (first '(a b c)) t
Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. Summer semester 2010. 25.3.201013
Defining functions• A function is a mapping from some number of inputs to a single
output
• Define a new function using define-function (cl:defun)
(define-function NAME (INPUTS) OUTPUT_VALUE)
(define-function square (x)
(* x x))
(define-function next-to-last (arblist)
(second (reverse arblist)))
(define-function add-at-end1 (item arblist)
(reverse (add item (reverse arblist))))
(define-function add-at-end2 (item arblist)
(append arblist (list item)))
(square 5) 25
(next-to-last '(a b c d))
c
(add-at-end1 'x '(a b c))
'(a b c x)
(add-at-end2 'x '(a b c))
'(a b c x)
Agenda
• S-Expressions and Data Types
• Functions
• Control Structures
• Package Handling
• Testing
• Language Conventions
• References
Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. Summer semester 2010. 25.3.201014
Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. Summer semester 2010. 25.3.201015
Conditional evaluation
• Can select alternative expressions to evaluate
(if TEST TRUE_EXPRESSION FALSE_EXPRESSION)
• Examples:(define-function my-abs (num)
(if (< num 0)
(- 0 num)
num))
(define-function wind-chill (temp wind)
(if (<= wind 3)
temp
(+ 35.74 (* 0.6215 temp)
(* (- (* 0.4275 temp) 35.75) (expt wind 0.16)))))
• Note: an if-expression is a special form not a functional expression different evaluation rule: only relevant expression is evaluated
Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. Summer semester 2010. 25.3.201016
Conditional evaluation (cont.)
• Logical connectives and, or, not can be used
• predicates exist for selecting various types
symbolp charp booleanp stringp listp …
• Example:
(if (and (listp x) (= (length x) 1))
'singleton
'not)Boolean expressions are evaluated
left-to-right, short-circuited
Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. Summer semester 2010. 25.3.201017
Multi-way conditional• When there are more than two alternatives, can
- nest if-expressions (i.e., cascading if's)
- use the cond special form (i.e., a switch)
(cond (TEST1 EXPRESSION1)
(TEST2 EXPRESSION2)
. . .
(else EXPRESSIONn))
• Examples:(define-function compare (num1 num2)
(cond ((= num1 num2) 'equal)
((> num1 num2) 'greater)
(else 'less))))
(define-function wind-chill (temp wind)
(cond ((> temp 50) 'UNDEFINED)
((<= wind 3) temp)
(else (+ 35.74 (* 0.6215 temp)
(* (- (* 0.4275 temp) 35.75)
(expt wind 0.16))))))
evaluate tests in order
• when reach one that evaluates to
"true", evaluate corresponding
expression & return
Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. Summer semester 2010. 25.3.201018
Repetition via recursion
• pure LISP does not have loops: repetition is performed via recursive
functions
• Examples:
(define-function sum-1-to-N (N)
(if (< N 1)
0
(+ N (sum-1-to-N (- N 1)))))
(define-function my-member (item lst)
(cond ((is-empty lst) false)
((equal item (first lst)) true)
(else (my-member item (rest lst)))))
• Additionally, Common Lisp provides a powerful loop construct
Agenda
• S-Expressions and Data Types
• Functions
• Control Structures
• Package Handling
• Testing
• Language Conventions
• References
Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. Summer semester 2010. 25.3.201019
Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. Summer semester 2010. 25.3.201020
Package Handling
• Namespace concept of Common Lisp
• Define a package in a separate file using defpackage
• Use a keyword symbol (starting colon) as name, e.g., :my-package
• Start the file in which you define the package with (in-package :common-lisp-user)this will make your package be known everywhere
• Start the file in which you define your code with(in-package :my-package)or whatever your package name isThis will make your definitions be known in your package
• For executing your code in the interactive shell (REPL) evaluate(in-package :my-package)
• Non-imported symbols may be used with colon, e.g., cl:car
Agenda
• S-Expressions and Data Types
• Functions
• Control Structures
• Package Handling
• Testing
• Language Conventions
• References
Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. Summer semester 2010. 25.3.201021
Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. Summer semester 2010. 25.3.2010
Unit Testing
• Unit tests repeatedly test a unit of your code, e.g., a function
• Unit testing framework LispUnit by Christopher K. Riesbeck
http://www.cs.northwestern.edu/academics/courses/325/readings/lisp-
unit.html
• Other assert functions: assert-true, assert-false
• Use in the interactive shell (REPL):
22
(in-package :humm)
(define-test factorial-test
(assert-equal 1 (factorial 1))
(assert-equal 24 (factorial 4))
)
Name of the test case
Compares expected valuewith actual value
cl-user(14): (in-package :humm)
#<The de.h-da.lop.app.user.humm package>
humm(15): (run-tests)
factorial-test: 2 assertions passed, 0 failed.
total: 2 assertions passed, 0 failed, 0 execution errors.
Agenda
• S-Expressions and Data Types
• Functions
• Control Structures
• Package Handling
• Testing
• Language Conventions
• References
Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. Summer semester 2010. 25.3.201023
Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. Summer semester 2010. 25.3.2010
LOP Language conventions
• Naming conventions for languages and applications implemented in those
langauges:
• Language versioning:
- Every language version which is not upwards compatible with the earlier
version will get a new version number
- Old language versions are deprecated
24
functional.1.package.lisp
functional.1.lang.lisp
functional.1.lang.test.1.lisp
application.functional.1.lisp
application.test.1.lisp
Language name + version Package definition
Language definition
Subject to testing:Language definition
Test language name + version
Subject to testing:application
Test language name + version
Application name Language name + version
Agenda
• S-Expressions and Data Types
• Functions
• Control Structures
• Package Handling
• Testing
• Language Conventions
• References
Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. Summer semester 2010. 25.3.201025
Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. Summer semester 2010. 25.3.201026
References
• Books:
- Guy Steele: Common Lisp – The Language: http://www.cs.cmu.edu/Groups/AI/html/cltl/cltl2.html
- Peter Seibel: Practical Common Lisp. http://www.gigamonkeys.com/book/
• Common Lisp Sites:
- The Common Lisp Directory: http://www.cl-user.net
- Common Lisp.net: http://common-lisp.net/
- CLiki: http://www.cliki.net/index
- Common Lisp Hyper Spec: http://www.lispworks.com/documentation/HyperSpec/Front/
• News groups:
- comp.lang.lisp
- cl-user