Crash Course in Lisp - PowerPoint PPT Presentation

About This Presentation
Title:

Crash Course in Lisp

Description:

1. Crash Course in Lisp. CS 171/271. 2. Brief Intro. Lisp: List Processor ... String of characters (letters, digits, and hyphens) Examples: x Move a1 turn-right SQR ... – PowerPoint PPT presentation

Number of Views:105
Avg rating:3.0/5.0
Slides: 17
Provided by: jpver
Category:
Tags: course | crash | lisp | net

less

Transcript and Presenter's Notes

Title: Crash Course in Lisp


1
Crash Course in Lisp
  • CS 171/271

2
Brief Intro
  • Lisp List Processor
  • Designed in 1958 by McCarthy(2nd oldest
    programming language)
  • Functional programming language
  • Interpreted
  • Based on symbolic expressions, lists, functions,
    recursion

3
Symbols and Numbers
  • Symbol
  • String of characters (letters, digits, and
    hyphens)
  • Examples x Move a1 turn-right SQR
  • NOT case sensitive
  • Number
  • Examples 123 -1.234 8e99 -7.8E-23
  • Just like int or double constants in C/Java

4
Lists
  • List Sequence of symbols, numbers, or lists
  • Examples
  • (a b c d e 1 2 3)
  • (This list (contains (4 elements)) (really))
  • Expressions that arent lists are atoms
  • Examples A 1 the
  • The empty list is nil
  • nil is a special symbol
  • both a list and an atom

5
Lisp Expressionsand the Lisp Interpreter
  • The interpreter repeatedly
  • Prompts for a well-formed expression
  • Evaluates the expression
  • Returns a response
  • Examples
  • gt ( 1 5) gt (square 5)
  • 6 25
  • gt (square 5) gt (first ((a b) c (1 2) 3)
  • (square 5) (a b)

6
Built-in Functions
  • Numeric Functions
  • List Access Functions
  • List Construction Functions
  • Predicates
  • quote and setq
  • defun
  • Special Functions if, cond, loop

7
Numeric Functions
  • Example ( 5 8 3 2)
  • - /
  • sqrt
  • expt
  • min max
  • abs mod round
  • sin cos tan

8
List Access Functions
  • first or CAR
  • rest or CDR
  • last
  • length

9
List Construction Functions
  • cons
  • append
  • list

10
Predicates
  • listp numberp integerp stringp atom
  • NOTE nil is false, T is true
  • null
  • equal eq eql
  • and or not

11
quote
  • quote or prevents an expression from being
    evaluated
  • (quote exp) same as exp
  • gt a
  • Error because a is unbound/cant be evaluated
  • gta
  • a
  • gt( 3 2)
  • 5
  • gt( 3 2)
  • ( 3 2)

12
setq
  • setq stores a value for a symbol
  • gt(setq a 5)
  • 5
  • gta
  • 5
  • gt(setq b a)
  • 5
  • gt(setq c a)
  • a
  • gt(setq acts (s l r))
  • (s l r)

13
Evaluating a symbol
  • gta
  • a
  • gt(setq a 5)
  • 5
  • gta
  • 5
  • gta
  • a

14
defun
  • (defun func-name (args) body )
  • body may contain some elements in args
  • body may contain several expressions
  • Last expression is the one returned

15
Special Functions
  • (if condition then-result else-result)
  • (cond (test1 result1) (test2 result2)
    )
  • (loop )

16
load
  • Create a text file containing Lisp expressions
  • Suppose the file is named file.lisp
  • Type in the expression
  • gt (load file.lisp)
Write a Comment
User Comments (0)
About PowerShow.com