Introduction to Lisp 1 - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Introduction to Lisp 1

Description:

LISP means 'LISt Processor', is a functional language ... One of the strongest features of LISP is that it treats programs as same as data. ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 13
Provided by: cseBu
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Lisp 1


1
Introduction to Lisp 1
  • Carlos Lollett

2
Introduction to Lisp. Contents
  • What is Lisp
  • Expressions
  • Functions
  • Lists
  • Examples

3
What is Lisp
  • LISP means "LISt Processor", is a functional
    language
  • LISP was designed as an interpreted language. One
    of the strongest features of LISP is that it
    treats programs as same as data. An intelligent
    LISP program can evolve and change its code!

4
Expressions
  • Evaluation Loop
  • loop
  • read in an expression from the console
  • evaluate the expression
  • print the result of evaluation to the console
  • end loop.

5
Expressions
  • Atoms
  • An atom is the unit in LISP.
  • Any combination of characters, except " ", "("
    and ")", can be an atom.
  • Types of Atom
  • Symbols
  • Not case sensitive.
  • Numbers
  • Integers
  • Rational
  • Complex C(i j)
  • Constants (self-evaluating)
  • Symbols that have special meaning like NIL, T

6
Expressions
  • Forms
  • Functions f(x) gt (f x)
  • Prefix notation gt ( 2 (cos 0) ( 4 6))
  • Overloading

7
Bindings
  • Variable binding
  • Variables equal to symbols
  • We can associate a value to the variable "a", by
    the function "setq"
  • (setq myval 3)
  • gt myval
  • 3

8
Bindings
  • Variable Literal value
  • symbol myval" means myval" or 3 ? We should use
    the QUOTE. QUOTE is an essential function in
    LISP, it can be used in two ways
  • gt myval
  • MYVAL
  • gt (quote myval)
  • MYVAL
  • (eval myval) eval is opposite to quote
  • 3

9
Defining Functions
  • (defun double (x) ( x 2))
  • (defun factorial (N) "Compute the factorial of
    N." (if ( N 1) 1 ( N (factorial (- N 1)))))
  • (defun fibonacci (N) "Compute the N'th Fibonacci
    number." (if (or (zerop N) ( N 1)) 1 (
    (fibonacci (- N 1)) (fibonacci (- N 2)))))

10
Coding Lisp out of the interpreter
  • Start with acl
  • (compile-file "testing.lisp")
  • (load "testing")
  • (trace factorial) to trace functions
  • (exit) exit acl environment

11
Lists
  • (cons 1 (cons 2 nil))
  • (1 2)
  • USER(24) (first '(2 4 8))
  • 2
  • USER(25) (rest '(2 4 8))
  • (4 8)
  • USER(26) (first (rest '(2 4 8)))
  • 4
  • USER(27) (rest (rest '(2 4 8)))
  • (8)
  • USER(28) (rest (rest (rest '(8))))
  • NIL
  • USER(29) (null nil)
  • T
  • USER(30) (null '(1 2 3))
  • NIL
  • USER(31) (consp nil)
  • NIL
  • USER(32) (consp '(1 2 3))

12
List Recursive Function
  • (defun recursive-list-length (L) "A recursive
    implementation of list-length." (if (null L) 0 (
    1 (recursive-list-length (rest L)))))
Write a Comment
User Comments (0)
About PowerShow.com