Basic lisp l.jpg
Advertisement
This presentation is the property of its rightful owner.
1 / 18

Basic Lisp PowerPoint PPT Presentation

Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn What is Lisp? A functional language includes linked lists as a built-in data type Everything in lisp is either an atom or a list (expression) Lisp is dynamically typed

Download Presentation

Basic Lisp

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Basic lisp l.jpg

Basic Lisp

CIS 479/579

Bruce R. Maxim

UM-Dearborn


What is lisp l.jpg

What is Lisp?

  • A functional language includes linked lists as a built-in data type

  • Everything in lisp is either an atom or a list (expression)

  • Lisp is dynamically typed

  • It is possible to define executable data structures because data and code are equivalent


Lists and atoms l.jpg

Lists and Atoms

  • Consider the linked list ‘(a b c)

    a is an atom

    b is an atom

    c is an atom

  • Atoms have both names and values

  • Often times in Lisp programming you will work with the name and ignore the value


Literal and numeric atoms l.jpg

Literal and Numeric Atoms

  • Numeric atoms

    • (name matches value)

      12 or –1.53

  • Literal atoms

    • begin with letter

    • value is undefined when created

      a or Sam


Pre defined literal atoms l.jpg

Pre-Defined Literal Atoms

  • T or t

    • True (logical constant)

  • Nil or nil

    • Nil = ‘( ) meaning the "empty list“

    • False (logical constant)

  • There are no reserved words in Lisp to that means t or nil can be redefined by the user


Using lisp l.jpg

Using Lisp

  • Lisp is a interactive system

  • You can files to be processed as a batch file, but more often than not the programmer is the “main program”

  • Every expression typed a the “>” prompt is “read” and “evaluated” unless it is prefixed with an apostrophe ‘

  • Typing (exit) at the “>” prompt terminates the xlisp program


Sample output l.jpg

Sample Output

>(a b c)

error: unbound function - a

if continued: try evaluating symbol again

1> [ back to top level ]

> '(a b c)

(a b c)

> 1

1

> 1.2

1.2

> a

error: unbound variable - a

if continued: try evaluating symbol again

1> [ back to top level ]


Sample output8 l.jpg

Sample Output

> nil

nil

> t

t

> T

t

> '(a (b c) d)

(a (b c) d)

> (setq sam 'abc)

abc

> sam

abc


Arithmetic functions l.jpg

Arithmetic Functions

> (/ 2 3)

2/3

> (/ 1.0 2)

0.5

> (1+ 3)

4

> (mod 2 3)

2

> (mod 5 2)

1

> (+ (* 2 2) (/ 4.0 5)

)

4.8


Car first and cdr rest l.jpg

car=first and cdr=rest

> (car '(a b c))

a

> (cdr '(a b c))

(b c)

> (car nil)

nil

> (cdr nil)

nil

> (first '(a b c))

a

> (car (cdr '(a b c)))

b

> (cadr '(a b c))

b


List functions l.jpg

List Functions

> (list 'a 2 'b)

(a 2 b)

> (list '(a b) '(c d))

((a b) (c d))

> (list sam c)

error: unbound variable - c

if continued: try evaluating symbol again

1> [ back to top level ]

> (list sam 'c)

(abc c)

> (cons 'a '(b c d))

(a b c d)

> (cons '(a b c) 'd)

((a b c) . d)


List functions12 l.jpg

List Functions

> (append '(a b) '(c d))

(a b c d)

> (reverse '(a b c d))

(d c b a)

> (length '(a (b c) d)))

3

>

> (last '(a b c d))

(d)

> (subst 'a 'b '(a b c))

(a a c)

> (subst 'a 'b '(a b c b))

(a a c a)


Eval and quote l.jpg

eval and quote

> (eval (cdr '(a + 2 3)))

5

> (setq a 'b)

b

> a

b

> b

error: unbound variable - b

if continued: try evaluating symbol again

1> [ back to top level ]

> (set 'a 'b)

b

> (eval (eval ''a))

b

> 'a

a


Eval and quote14 l.jpg

eval and quote

> (eval (eval '(quote a)))

b

> 'a

a

> (eval '(list '* 9 6))

(* 9 6)

> (eval (eval '(list * 9 6)))

error: bad function - (* 9 6)

1> [ back to top level ]

> (eval (eval '(list '* 9 6)))

54


Function definition l.jpg

Function Definition

> (defun intro (x y)

(list x 'this 'is y)

)

Intro

>; be careful not to quote the arguments when

>; defining the function

> (intro 2 3)

(2 this is 3)

> (intro 'stanley 'livingston)

(stanley this is livingston)


Predicate functions l.jpg

Predicate Functions

> (atom 2)

t

> (atom '(a b c))

nil

> (listp 2)

nil

> (listp '(a b c))

t

> (equal 2 3)

nil

> (= 2 3)

nil

> (equal 6 (* 2 3))

t


Predicate functions17 l.jpg

Predicate Functions

> (set a ‘(1 2))

(1 2)

> (equal a ‘(1 2))

t

> (eql a ‘(1 2))

nil

> (null '())

t

> (null 2)

nil

> nil

nil

> (null nil)

t


Membership functions l.jpg

Membership Functions

> (member 'c '(a b c d))

(c d)

> (member 'a '((a b) c d))

nil

> (member '(d e) '((a b) c (d e) f))

nil

> (assoc 'c '((a b) (c d) (e f)))

(c d)


  • Login