2. Crash Course on Lisp - fbi .2. Crash Course on Lisp ... (first (reverse '(a b c))) evaluates to

download 2. Crash Course on Lisp - fbi .2. Crash Course on Lisp ... (first (reverse '(a b c))) evaluates to

of 26

  • date post

    25-Jun-2018
  • Category

    Documents

  • view

    213
  • download

    0

Embed Size (px)

Transcript of 2. Crash Course on Lisp - fbi .2. Crash Course on Lisp ... (first (reverse '(a b c))) evaluates to

  • 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

    ::= |

    ::= |

    ::= ( )

    ::=

    |

    ::= |

    ::= 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 (

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

    ((

  • 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

  • Pr