LISP - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

LISP

Description:

( quote ( 2 3)) Returns ( 2 3) Nth n: returns the nth value of ... This programme returns the value of x as it is if x 0 otherwise returns -x. List Manipulation ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 15
Provided by: ziq
Category:
Tags: lisp | quotation

less

Transcript and Presenter's Notes

Title: LISP


1
LISP
  • Symbolic Expressions (S Expressions)
  • Syntax
  • Opening and Closing parenthesis having elements
    in between.
  • List represented in LISP
  • (A B2 C3 Shahid)
  • (A B (C D E) F G)

2
Internal Representation
nil
B2
C3
Shahid
A
nil
F
G
A B
D
E
C
nil
3
Functional Programming
Defining Functions Syntax ( defunFunction
Name ( parameters procedure or body
of the function ) ) Setting Values to
variables (setq A 2) or (set A 2)
4
Arithmetic Operations
  • ( A B) means AB
  • ( 7 7) means 7749
  • (- C D) means C-D
  • (- (10 20) 7) means ((1020)- 7 23

5
Tree Diagram for Evaluation
  • ( ( 10 20) ( 2 3))


( 10 20)
( 2 3)


10
20
2
3
6
Built In Commands
  • Quote returns the list as it is.
  • (quote(a b c)) or use instead of quote.
  • Returns (a b c)
  • List load the argument as a list
  • (list(1 2 3 4))
  • Returns (1 2 3 4)
  • Eval evaluates the list or quoted list.
  • (eval ( quote ( 2 3)))
  • Returns 5
  • ( quote ( 2 3))
  • Returns (2 3)
  • Nth n returns the nth value of the list
  • (nth 0 (1 2 3 4 5 6)) returns 1
  • (nth 3 (1 2 3 4 5 6)) returns 4
  • (nth 5 (1 2 3 4 5 6)) returns 6

7
Examples
  • ( ( 5 1 ) 6 )
  • returns t
  • ( gt ( 5 6) ( 4 5))
  • returns t
  • Other List Operators
  • (length (1 2 3 4))
  • returns 4
  • (member 8 (1 4 2 8 7))
  • returns t and if not present then returns nil
  • (null ())
  • returns t

8
Assignments Statements
  • (setq a0)
  • returns 0
  • (let ((a 3) b) local assignment
  • (setq b 4) if you go outside let a goes to 0
  • ( a b))
  • 7
  • a
  • 0
  • b
  • error b not bounded at the top level

9
Sample Programme
Programme to add three numbers ( defun
ADDNUMBERS (num1 num2 num3) (setq sum 0)
initialise the sum to zero (setq sum ( sum
num1) add first number (setq sum ( sum
num2)) (setq sum ( sum num3)) )
10
Conditionals
  • Command cond
  • Syntax
  • ( cond (ltcondition 1gtltaction 1gt)
  • (ltcondition 2gtltaction 2gt)
  • ------------------------
  • ------------------------
  • (ltcondition ngtltaction ngt)
  • )

11
Example
  • ( defun absolute_values(x)
  • (cond ( ( lt x 0) (-x))
  • (t x)))
  • This programme returns the value of x as it is if
    xgt0 otherwise returns -x

12
List Manipulation
  • Command cons
  • (cons a (b c)) returns (a b c)
  • Command append does the same operation as cons
  • Command car
  • Returns the first element of the list
  • Command cdr
  • Returns the list excluding the first element

13
Examples
  • (cdr ((a b) (c d)))
  • returns ((c d))
  • (car (cdr (car list1)))
  • ca d ar (cadar x)
  • (car (car(x)) (caar x)
  • (car(car(car x))) (caaar x)
  • (cdr(car(cdr x))) (cdadr x)

14
Recursion
  • Write a programme that finds the factorial of a
    number
  • Syntax (defun factorial(x)
  • -----------------
  • (recursive call)) ?
  • recursive call ( x ( factorial ( - x 1) )
    )
Write a Comment
User Comments (0)
About PowerShow.com