Docsity

An Introduction to LISP: Understanding Expressions, Control, Functions, and Recursion, Slides of Robotics

Toggle sidebar
bg1
An Introduction to LISP
•Introduction
•LISP: A Brief Overview
–Expressions, the Syntactic Basis of LISP
–Control of LISP Evaluation: quote and eval
–Programming in LISP: Creating New Functions
–Program Control in LISP: Conditionals and Predicates
–Functions, Lists, and Symbolic Computing
–Lists as Recursive Structures
–Nested Lists, Structure, and car/cdr Recursion
–Binding Variables Using set
–Defining Local Variables Using let
–Data Types in Common LISP
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download An Introduction to LISP: Understanding Expressions, Control, Functions, and Recursion and more Slides Robotics in PDF only on Docsity!

An Introduction to LISP

  • Introduction
  • LISP: A Brief Overview
    • Expressions, the Syntactic Basis of LISP
    • Control of LISP Evaluation: quote and eval
    • Programming in LISP: Creating New Functions
    • Program Control in LISP: Conditionals and Predicates
    • Functions, Lists, and Symbolic Computing
    • Lists as Recursive Structures
    • Nested Lists, Structure, and car/cdr Recursion
    • Binding Variables Using set
    • Defining Local Variables Using let
    • Data Types in Common LISP

Symbolic

  • Why do we care about symbols?
    • U nderstand human cognition with a language like

symbolic processing

Characteristics of LISP

1. Language for artificial intelligence programming

a. Originally designed for symbolic computing

2. Imperative language

a. Describe how to perform an algorithm b. Contrasts with declarative languages such as PROLOG

3. Functional programming

a. Syntax and semantics are derived from the mathematical theory of recursive functions. b. Combined with a rich set of high-level tools for building symbolic data structures such as predicates, frames, networks, and objects

4. Popularity in the AI community

a. Widely used as a language for implementing AI tools and models b. High-level functionality and rich development environment make it an ideal language for building and testing prototype systems.

LISP: A Brief Overview

  • Syntactic elements of LISP
    • Symbolic expressions : S-expressions
      • Atom : basic syntactic units
      • List
    • Both programs and data are represented as s-expressions
  • Atom
    • Letters (upper, lower cases)
    • Numbers
    • Characters : * + - / @ $ % ^ & _ < > ~.
    • Example
      • 3.
      • Hyphenated-name
      • some-global
      • nil
      • x

OTHER DATA TYPES

  1. Strings: (length "abc") --> 3
  2. Many number types.

LISP EVALUATION RULE

  1. Every expression is either a list or an atom.
  2. Every list to be evaluated is either a special form or a function

application.

  1. A special form expression is a list whose first element is a special form operator and is evaluated according to the special rule of that operator.
  2. A function application is evaluated by first evaluating the arguments (the rest of the list) and then finding the function named by the first element of the list and applying it to the list of evaluated arguments.
  3. Every atom is either a symbol or a non-symbol.
  4. A symbol evaluates to the most recent value assigned to the variable.
  5. A non-symbol atom evaluates to itself.

Control of LISP Evaluation:

quote & eval

  • quote : ā€˜
    • prevent the evaluation of arguments
      • (quote a) ==> a
      • (quote (+ 1 3)) ==> (+ 1 3)
      • ā€˜a ==> a
      • ā€˜(+ 4 6) ==> (+ 4 6)
  • eval
    • allows programmers to evaluate s-expressions at will
      • (eval (quote (+ 2 3))) ==> 5
      • (list ā€˜ 2 5) ==> ( 2 5)**
      • (eval (list ā€˜ 2 5)) ==> 10*

WHAT MAKES LISP DIFFERENT

  1. Built-in support for Lists.
  2. Automatic memory management.
  3. Dynamic typing.
  4. First-class functions.
  5. Uniform syntax.
  6. Extensibility

Interpreted & interactive

  • Interpreted
  • Interactive (^) USER(1): 12
USER(2): (+ 12 3)
USER(3): (setf Almost-age 31)
USER(4): Almost-age
USER(5): 'Almost-age
ALMOST-AGE
USER(6): Docsity.com

LISTS

  1. Primitive aggregating type.
  2. Primitive functions: first, second, ..., length,

last. append, cons, list.

Everything's a List!

  • Data
  • Functions
  • Simple syntax:

( function-name arg1 arg2 …)

(a b c)

(defun plus (x y)
(+ x y))

List as recursive structures

  • Cons cell: data structure to hold a list in LISP
    • car - holds the first element.
    • cdr - holds the rest in the list.
  • Accessing a list
    • (car ā€˜(a b c)) ==> a
    • (cdr ā€˜(a b c)) ==> (b c)
    • (first ā€˜(a b c)) ==> a
    • (second ā€˜(a b c)) ==> b
    • (nth 1 ā€˜(a b c)) ==> b
  • Constructing a list
    • (cons ā€˜a ā€˜(b c)) ==> (a b c)
    • (cons ā€˜a nil) ==> (a)
    • (cons ā€˜(a b) ā€˜(c d)) ==> ((a b) c d)

Artificial Intelligence 17

Dynamic

  • Functions are first-class objects
    • Pass functions as arguments to other functions
USER(2): (apply #'+ '(1 2 3))
USER(1): (+ 1 2 3)

One Function, and a Side of Fries, To Go

  • Create functions on the fly
  • Lisp contains itself: eval

USER(1): (funcall #'(lambda (x y) (+ x y)) 17 14) 31