LISP Part I - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

LISP Part I

Description:

(defun hypotenuse (x y) (sqrt ( (square x) (square y)))) ? (hypotenuse 4 3) ? 25. Conditionals. Implementing conditional branches: ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 23
Provided by: ricardo125
Category:
Tags: lisp | hypotenuse | part

less

Transcript and Presenter's Notes

Title: LISP Part I


1
LISP (Part I)
  • Introduction
  • LISP An Overview
  • Control of LISP Evaluation
  • Programming in LISP
  • Summary

2
Introduction
  • LISP has about 40 years of existence
  • Originally designed for symbolic computing
  • LISP is functional (based on recursive
    functions)
  • Examples of applications
  • Search engines
  • Pattern matchers
  • Theorem provers
  • Rule-based expert systems
  • Semantic networks

3
LISP (Part I)
  • Introduction
  • LISP An Overview
  • Control of LISP Evaluation
  • Programming in LISP
  • Summary

4
Overview
Syntax Basic elements are symbolic expressions
(s-expressions)
Letters Numbers Non-alphanumeric characters
Atoms Lists
s-expressions
Sequence of atoms or lists
5
Atoms and Lists
Examples of atoms 3.1416 100 x nil Examples of
lists (1 2 3 4) (tom mary john joyce) (a (b c)
(d (e f))) ()
6
Lists
LISP uses lists and atoms to represent both
programs and data.
  • ? ( 7 9)
  • ? 63
  • When given a list
  • LISP evaluates the first element as the name of a
    function
  • Evaluates the remaining elements as arguments
  • LISP returns the result of applying the function
    to the
  • arguments

7
Examples
? ( ( 2 3) 5) ? t ? (a b c) Error invalid
function a
  • ? ( 14 5)
  • ? 19
  • ? ( 1 2 3 4)
  • ? 10
  • ? (- ( 3 4) 7)
  • ? 0
  • ? ( ( 2 5) (- 7(/ 21 7)))
  • ? 28

8
Figure 15.1 Tree diagram of the evaluation of a
simple LISP function.
9
Predefined Functions in LISP
? (list 1 2 3 4 5) ? (1 2 3 4 5) ? (nth 0
(a b c d)) a ? (nth 2 (list 1 2 3 4 5)) ?
3 ? (nth 2 ((a 1)(b 2)(c 3)(d 4))) ? (c 3)
? (length (a b c d)) ? 4 ? (member 7 (1 2
3)) ? nil ? (null ()) ? t
10
LISP (Part I)
  • Introduction
  • LISP An Overview
  • Control of LISP Evaluation
  • Programming in LISP
  • Summary

11
Quote
To avoid evaluating an argument we use the symbol
or the function quote. ? (quote (a b c))
? (a b c) ? (a b c)
? (a b c) ? (quote ( 1
3)) ? ( 1 3) ? ( 1 3)
? ( 1 3)
12
Examples
? (list ( 1 2) ( 3 4)) ? (3 7) ? (list
( 1 2) ( 3 4)) ? (( 1 2) ( 3 4))
13
Eval
The opposite of quote is eval. It evaluates the
argument twice. ? (eval (quote ( 2 3))) ?
5 ? (eval (list 2 5 )) ? 10
14
LISP (Part I)
  • Introduction
  • LISP An Overview
  • Control of LISP Evaluation
  • Programming in LISP
  • Summary

15
Creating New Functions
New functions are built using the word
defun. (defun square (x) ( x x)) 1st
argument name of the function 2nd argument
list of arguments The rest constitute the body of
the new function ? (square 5) ? 25
16
Another Example
(defun hypotenuse (x y) (sqrt ( (square
x) (square y))))
? (hypotenuse 4 3) ? 25
17
Conditionals
Implementing conditional branches (cond
(ltcondition 1gt ltaction 1gt) (ltcondition
2gt ltaction 2gt)
(ltcondition ngt ltaction ngt)) (defun
absolute-value (x) (cond ((lt x 0) (-
x)) ((gt x 0) x )))
18
Arithmetics
? (gt 17 4) ? t ? (lt 8 ( 4 2)) ? nil ?
(oddp 3) checks whether argument is odd or
not ? t ? (minusp 6) checks if argument
is less than zero ? nil
19
Arithmetics
? (numberp 17) checks whether argument is
numeric ? t ? (zerop 6) true
if argument is zero ? nil ? (plusp 10)
true if argument is strictly greater than
zero ? t
20
Logical Connectives
Logical connectives and, or, not. Evaluation is
left to right. ? (and (oddp 2) (print
hello)) ? nil ? (and (oddp 3) (print
hello)) ? hello ? (or (oddp 2) (print
hello)) ? hello
21
LISP (Part I)
  • Introduction
  • LISP An Overview
  • Control of LISP Evaluation
  • Programming in LISP
  • Summary

22
Summary
  • LISP works on symbolic expressions (atoms and
    lists).
  • Lists can be used to represent programs and data.
  • Function quote avoids evaluating the arguments
    of
  • the function.
  • Function eval works contrary to quote it
    evaluates
  • the argument twice.
  • New functions are built using the word defun.
Write a Comment
User Comments (0)
About PowerShow.com