2. Crash Course on Lisp - fbi · 2. Crash Course on Lisp Language-Oriented Programming Prof. Dr....

32
Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. 09.10.2015 1 2. Crash Course on Lisp Language-Oriented Programming Prof. Dr. Bernhard Humm Faculty of Computer Science Hochschule Darmstadt – University of Applied Sciences

Transcript of 2. Crash Course on Lisp - fbi · 2. Crash Course on Lisp Language-Oriented Programming Prof. Dr....

Page 1: 2. Crash Course on Lisp - fbi · 2. Crash Course on Lisp Language-Oriented Programming Prof. Dr. Bernhard Humm Faculty of Computer Science ... (define-function compare (x y) (cond

Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. 09.10.20151

2. Crash Course on Lisp Language-Oriented Programming

Prof. Dr. Bernhard HummFaculty of Computer ScienceHochschule Darmstadt – University of Applied Sciences

Page 2: 2. Crash Course on Lisp - fbi · 2. Crash Course on Lisp Language-Oriented Programming Prof. Dr. Bernhard Humm Faculty of Computer Science ... (define-function compare (x y) (cond

Agenda

• S-Expressions and Data Types

• Functions

• Control Structures

• Package Handling

• Testing

• IDE

• Language Conventions

• References

Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. 09.10.20152

Page 3: 2. Crash Course on Lisp - fbi · 2. Crash Course on Lisp Language-Oriented Programming Prof. Dr. Bernhard Humm Faculty of Computer Science ... (define-function compare (x y) (cond

Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. 09.10.20153

LISP in a nutshell

• LISP is very simple: only 2 kinds of data objects (called symbolic expressions or S-expressions)

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

Page 4: 2. Crash Course on Lisp - fbi · 2. Crash Course on Lisp Language-Oriented Programming Prof. Dr. Bernhard Humm Faculty of Computer Science ... (define-function compare (x y) (cond

Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. 09.10.20154

Atoms

• Atoms are:

- Numbers 4 3.14 1/2 #xA2 #b1001

- Characters #\a #\Q #\space #\tab

- Strings "foo" "Dave Reed" "@%!?#"

- Symbols Dave num123 miles->km !_^_!

- Booleans true (cl:t) false (cl:nil)

• Symbols are sequences of letters, digits, and extended alphabetic

characters (+ - . * / < > = ! ? : $ % + & ~ ^)

- cannot start with a digit

- Example: Customer

Common Lisp (namespace cl) is the most common Lisp dialect and an ANSI standard. In this lecture, we use a subset with some convenience modifications

Page 5: 2. Crash Course on Lisp - fbi · 2. Crash Course on Lisp Language-Oriented Programming Prof. Dr. Bernhard Humm Faculty of Computer Science ... (define-function compare (x y) (cond

Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. 09.10.20155

Lists

• () is a list (the empty list nil)

• (e1 e2 . . . en) is a list, where each element is either an

atom or a list

• Lists can store different types, not contiguous, no random access

• Examples:

- ()

- (a)

- (a b c d)

- ((a b) c (d e))

- (((((a)))))

Page 6: 2. Crash Course on Lisp - fbi · 2. Crash Course on Lisp Language-Oriented Programming Prof. Dr. Bernhard Humm Faculty of Computer Science ... (define-function compare (x y) (cond

Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. 09.10.20156

Data types in LISP

• LISP is dynamically typed: types are associated with values rather

than with variables, bound dynamically

• numbers can be described as a hierarchy of types

- number

- complex #c(3.5 1.2)

- real 3.14

- rational 2/3

- integer -3

• integers and rationals are exact values, others can be inexact

• (+ 3 1/2) 7/2

• (+ 3 0.5) 3.5

Page 7: 2. Crash Course on Lisp - fbi · 2. Crash Course on Lisp Language-Oriented Programming Prof. Dr. Bernhard Humm Faculty of Computer Science ... (define-function compare (x y) (cond

Agenda

• S-Expressions and Data Types

• Functions

• Control Structures

• Package Handling

• Testing

• IDE

• Language Conventions

• References

Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. 09.10.20157

Page 8: 2. Crash Course on Lisp - fbi · 2. Crash Course on Lisp Language-Oriented Programming Prof. Dr. Bernhard Humm Faculty of Computer Science ... (define-function compare (x y) (cond

Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. 09.10.20158

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 first (in unspecified order), then the function call itself

- if argument is a functional expression it is evaluated recursively

- the resulting function is applied to the resulting values

(first '(a b c))

- so, primitive function first 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

Page 9: 2. Crash Course on Lisp - fbi · 2. Crash Course on Lisp Language-Oriented Programming Prof. Dr. Bernhard Humm Faculty of Computer Science ... (define-function compare (x y) (cond

Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. 09.10.20159

Arithmetic primitives

• Predefined functions:

- + - * / expt mod rem

- = < > <= >=

- max min abs

- 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 Boolean value are called predicates

- (is-even 5) ;; cl:evenp false ;; cl:nil

- (is-odd 5) ;; cl:oddp true ;; cl:t

Page 10: 2. Crash Course on Lisp - fbi · 2. Crash Course on Lisp Language-Oriented Programming Prof. Dr. Bernhard Humm Faculty of Computer Science ... (define-function compare (x y) (cond

Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. 09.10.201510

List primitives

•Predefined functions:- first rest add (cl:cons)

- list length member

- reverse append

- equal

• 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 (non-nil)

- (member 'd '(a b c)) nil; i.e., false

- (equal 'a (first '(a b c)) t

- (append '(a b) '(c d)) (a b c d)

Page 11: 2. Crash Course on Lisp - fbi · 2. Crash Course on Lisp Language-Oriented Programming Prof. Dr. Bernhard Humm Faculty of Computer Science ... (define-function compare (x y) (cond

Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. 09.10.201511

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(param-1 … param-n) result)

(define-function square (x)

(* x x))

(define-function next-to-last (l)

(second (reverse l)))

(define-function add-at-end1 (x l)

(reverse (add x (reverse l))))

(define-function add-at-end2 (x l)

(append l (list x)))

(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)

No explicit return statement

Page 12: 2. Crash Course on Lisp - fbi · 2. Crash Course on Lisp Language-Oriented Programming Prof. Dr. Bernhard Humm Faculty of Computer Science ... (define-function compare (x y) (cond

Agenda

• S-Expressions and Data Types

• Functions

• Control Structures

• Package Handling

• Testing

• IDE

• Language Conventions

• References

Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. 09.10.201512

Page 13: 2. Crash Course on Lisp - fbi · 2. Crash Course on Lisp Language-Oriented Programming Prof. Dr. Bernhard Humm Faculty of Computer Science ... (define-function compare (x y) (cond

Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. 09.10.201513

Conditional evaluation

•Can select alternative expressions to evaluate

(if test true-expression false-expression)

• Examples:(define-function my-abs (n)

(if (< n 0)

(- n)

n))

(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: if is a special form, not a function different evaluation rule: only relevant expression is evaluated

Page 14: 2. Crash Course on Lisp - fbi · 2. Crash Course on Lisp Language-Oriented Programming Prof. Dr. Bernhard Humm Faculty of Computer Science ... (define-function compare (x y) (cond

Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. 09.10.201514

Conditional evaluation (cont.)

• Logical operators and, or, not can be used

•Predicates exist for selecting various types

is-number (cl:numberp)

is-list (cl:listp)

is-atom (cl:atom)

has-type (cl:typep)

Example:

(if (and (is-list l) (= (length l) 1))

'singleton

'no-singleton)

Boolean expressions are evaluated

left-to-right, short-circuited

Page 15: 2. Crash Course on Lisp - fbi · 2. Crash Course on Lisp Language-Oriented Programming Prof. Dr. Bernhard Humm Faculty of Computer Science ... (define-function compare (x y) (cond

Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. 09.10.201515

Multi-way conditional• When there are more than two alternatives you can...

- nest if-expressions (i.e., cascading if's)

- use the cond special form (i.e., a switch)

(cond (test1 expression1)

(test2 expression2)

. . .

(else expression))

• Examples:(define-function compare (x y)

(cond ((= x y) 0)

((> x y) 1)

(else -1))))

(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

Page 16: 2. Crash Course on Lisp - fbi · 2. Crash Course on Lisp Language-Oriented Programming Prof. Dr. Bernhard Humm Faculty of Computer Science ... (define-function compare (x y) (cond

Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. 09.10.201516

Repetition via recursion

• In pure LISP, repetition is performed via recursion

• Examples:

(define-function sum-1-to-n (n)

(if (< n 1)

0

(+ n (sum-1-to-n (- n 1)))))

(define-function my-member (x l)

(cond ((is-empty l) false)

((equal x (first l)) true)

(else (my-member x (rest l)))))

• Additionally, Common Lisp provides a powerful loop construct

See Peter Seibel: Practical Common Lisp

Page 17: 2. Crash Course on Lisp - fbi · 2. Crash Course on Lisp Language-Oriented Programming Prof. Dr. Bernhard Humm Faculty of Computer Science ... (define-function compare (x y) (cond

Agenda

• S-Expressions and Data Types

• Functions

• Control Structures

• Package Handling

• Testing

• IDE

• Language Conventions

• References

Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. 09.10.201517

Page 18: 2. Crash Course on Lisp - fbi · 2. Crash Course on Lisp Language-Oriented Programming Prof. Dr. Bernhard Humm Faculty of Computer Science ... (define-function compare (x y) (cond

Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. 09.10.201518

Package Handling

•Namespace concept of Common Lisp

•Define a package in a separate file using defpackage

•Use a keyword symbol (prefix :) 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

Page 19: 2. Crash Course on Lisp - fbi · 2. Crash Course on Lisp Language-Oriented Programming Prof. Dr. Bernhard Humm Faculty of Computer Science ... (define-function compare (x y) (cond

Agenda

• S-Expressions and Data Types

• Functions

• Control Structures

• Package Handling

• Testing

• IDE

• Language Conventions

• References

Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. 09.10.201519

Page 20: 2. Crash Course on Lisp - fbi · 2. Crash Course on Lisp Language-Oriented Programming Prof. Dr. Bernhard Humm Faculty of Computer Science ... (define-function compare (x y) (cond

Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. 09.10.2015

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):

20

(in-package :humm)

(define-test factorial-test

(assert-equal 1 (factorial 1))

(assert-equal 24 (factorial 4))

)

Name of the test case

Compares expected value with 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.

Page 21: 2. Crash Course on Lisp - fbi · 2. Crash Course on Lisp Language-Oriented Programming Prof. Dr. Bernhard Humm Faculty of Computer Science ... (define-function compare (x y) (cond

Agenda

• S-Expressions and Data Types

• Functions

• Control Structures

• Package Handling

• Testing

• IDE

• Language Conventions

• References

Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. 09.10.201521

Page 22: 2. Crash Course on Lisp - fbi · 2. Crash Course on Lisp Language-Oriented Programming Prof. Dr. Bernhard Humm Faculty of Computer Science ... (define-function compare (x y) (cond

Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. 09.10.2015

Window layout recommendation

22

Interactive Shell (REPL): Debug Window

Editors

Project Manager

Additional windows, e.g., Help

Page 23: 2. Crash Course on Lisp - fbi · 2. Crash Course on Lisp Language-Oriented Programming Prof. Dr. Bernhard Humm Faculty of Computer Science ... (define-function compare (x y) (cond

Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. 09.10.2015

The most important IDE commands

• File Open project (automtically at IDE start)

• File New (Ctrl+N)

• File Save (Ctrl+S)

• File Save all (Ctrl+Shift+S)

• Tools Compile Project (Ctrl+Alt+C)

23

Page 24: 2. Crash Course on Lisp - fbi · 2. Crash Course on Lisp Language-Oriented Programming Prof. Dr. Bernhard Humm Faculty of Computer Science ... (define-function compare (x y) (cond

Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. 09.10.2015

Interactive Shell (REPL)

24

Enter package via(in-package …)

Convenience historyfeature: select an earlier expressionand type enter

Simply evaluateexpressionsinteractively

Page 25: 2. Crash Course on Lisp - fbi · 2. Crash Course on Lisp Language-Oriented Programming Prof. Dr. Bernhard Humm Faculty of Computer Science ... (define-function compare (x y) (cond

Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. 09.10.2015

Auto completion: Ctrl+.

• After completion:

25

Signature specification

Page 26: 2. Crash Course on Lisp - fbi · 2. Crash Course on Lisp Language-Oriented Programming Prof. Dr. Bernhard Humm Faculty of Computer Science ... (define-function compare (x y) (cond

Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. 09.10.2015

Context Help:F1

For Common Lisp functions: For own functions:

26

Page 27: 2. Crash Course on Lisp - fbi · 2. Crash Course on Lisp Language-Oriented Programming Prof. Dr. Bernhard Humm Faculty of Computer Science ... (define-function compare (x y) (cond

Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. 09.10.2015

Debugger

27

Debugger can bestarted if erroroccurs

Runtime stack isdisplayed, variables can beinspected (and modified !)

Page 28: 2. Crash Course on Lisp - fbi · 2. Crash Course on Lisp Language-Oriented Programming Prof. Dr. Bernhard Humm Faculty of Computer Science ... (define-function compare (x y) (cond

Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. 09.10.2015

Tracing

Tracing via context menu Displays trace information at execution

Untrace via contect menu

Also helpful: Quick Find Definition

28

Page 29: 2. Crash Course on Lisp - fbi · 2. Crash Course on Lisp Language-Oriented Programming Prof. Dr. Bernhard Humm Faculty of Computer Science ... (define-function compare (x y) (cond

Agenda

• S-Expressions and Data Types

• Functions

• Control Structures

• Package Handling

• Testing

• IDE

• Language Conventions

• References

Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. 09.10.201529

Page 30: 2. Crash Course on Lisp - fbi · 2. Crash Course on Lisp Language-Oriented Programming Prof. Dr. Bernhard Humm Faculty of Computer Science ... (define-function compare (x y) (cond

Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. 09.10.2015

LOP Language conventions

• Naming conventions for languages and applications implemented in those

languages:

• 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

30

Language Implementationfunctional.1.package.lisp

functional.1.lang.lisp

Language Testfunctional.1.lang.test.1.lisp

Application Implementationapplication.functional.1.lisp

Application Testapplication.test.1.lisp

Language name + version

Package def.

Language definition

Subject to testing:Language definition

Test language name + version

Subject to testing:application

Test language name + version

Application name Language name + version

import

Page 31: 2. Crash Course on Lisp - fbi · 2. Crash Course on Lisp Language-Oriented Programming Prof. Dr. Bernhard Humm Faculty of Computer Science ... (define-function compare (x y) (cond

Agenda

• S-Expressions and Data Types

• Functions

• Control Structures

• Package Handling

• Testing

• IDE

• Language Conventions

• References

Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. 09.10.201531

Page 32: 2. Crash Course on Lisp - fbi · 2. Crash Course on Lisp Language-Oriented Programming Prof. Dr. Bernhard Humm Faculty of Computer Science ... (define-function compare (x y) (cond

Prof. Dr. Bernhard Humm, Darmstadt University of Applied Sciences. www.fbi.h-da.de/~b.humm. 09.10.201532

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/

• Newsgroups:

- comp.lang.lisp

- cl-user