Lecture 21 - November 6, 2007

 

In this lecture, different things were covered in the two sections. The 12:30 section spent a lot of time on Snobol and not much time on Lisp.  The 2:00 section spent most of its time on Lisp.  Below you will find material from both sections.  You should make sure you can read and understand and use all of it, regardless of which section you are in.

 

     DEFINE('COMB(STR,N,HEAD)CH')           :(COMB_END)

COMB OUTPUT =  EQ(N,0) HEAD                 :S(RETURN)

C2   STR  LE(N,SIZE(STR)) LEN(1) . CH =     :F(RETURN)

     COMB(STR,N - 1, HEAD CH)               :(C2)

COMB_END    

    COMB('ABCD',3)

END

1

2

3

4

5

6

7

 

Start of hand trace of  function COMB

Variables

STR

ABCD

BCD

CD

 

 

N

3

2

 

 

 

HEAD

NULL

A

 

 

 

CH

A

B

 

 

 

 

 

     DEFINE('REVERE(STRING)','R1')   :(R_END)

R1   ONECH         = LEN(1) . CH

R2   STRING ONECH  =                 :F(RETURN)

     REVERE        = CH REVERE       :(R2)

R_END

     OUTPUT        = REVERE('miami')

END

 

Complete hand trace of REVERE

VARIABLES

STRING

miami

iami

ami

mi

I

null

 

ONECH

M

 

 

 

 

 

 

CH

M

i

 

a

M

i

 

REVERE

M

 

im

aim

maim

Imaim

 

 

FUNCTION NAME

REVERE

 

LABELS

R1

R2

R_END

 

PATTERN

ONECH

A possible key for the Snobol assignment

    DEFINE('LLIST(N)')                  :(LLIST_END)

LLIST   OUTPUT = DIFFER(N)  VALUE(N)    :F(RETURN)

        LLIST(LSON(N))

      LLIST(RSON(N))                  :(RETURN)

LLIST_END

    DATA('BNODE(VALUE,LSON,RSON)')

    N =  BNODE('+',BNODE('X'), BNODE('Y'))

    LLIST(N)

END

 

Slides about LISP produced by former students

LISP syntax summary produced by former students - see how helpful you find it

A Word document discussing some features of LISP

NOTE: Although they exist, we will not use first, second, nth, loop, do

 

Below you will find a giant screen shot showing what the results are of typing various "things" into the LISP interpreter.  Notice that things which caused errors have been left in so you can see what doesn't work as well as what works.  Think about why they didn't work.

 

U:\Web\CS430_~1\Lisp>xlispsml

XLISP-PLUS version 2.1h

Portions Copyright (c) 1988, by David Betz.

Modified by Thomas Almy and others.

> (2 4 6)

error: bad function - 2

> (+ 2 5)

7

> '(2 5 6)

(2 5 6)

> (quote (2 5 7))

(2 5 7)

> quote(2 6 7)

error: unbound variable - QUOTE

> (car '(2 5 7))

2

> (cdr '(2 5 7))

(5 7)

> (car '( (2 3) a (9 2 1))

)

(2 3)

> (cdr '( (2 3) a (9 2 1))

)

(A (9 2 1))

> (FIRST '(2 5 7))

2

> (CADR '(2 5 7))

5

> (CDDR '(2 5 7))

(7)

> (CADDR '(2 5 7))

7

> (CDDDR '(2 5 7))

NIL

>  (READ)

APPLE

APPLE

> (SETQ A (READ))

APPLE

APPLE

> A

APPLE

> (setq mylist '(1 2 3 4))

(1 2 3 4)

> (SETQ YOURLIST '(10 11 12 13))

(10 11 12 13)

> yourlist

(10 11 12 13)

> mylist

(1 2 3 4)

> (cons (car mylist) yourlist)

(1 10 11 12 13)

> (cons (cdr mylist) (cdr yourlist))

((2 3 4) 11 12 13)

> (append (car mylist) (cdr yourlist))

error: bad argument type - 1

> (APPEND (CDR MYLIST) (CDR YOURLIST))

(2 3 4 11 12 13)

> (APPEND 3 '(2 5 66))

error: bad argument type - 3

> (APPEND APPLE '(2 5 66))

error: unbound variable - APPLE

> APPLE

error: unbound variable - APPLE

> (APpend a '(2 5 66))

error: bad argument type - APPLE

> (append 'a '(2 5 7 7))

error: bad argument type - A

> (append (list a) (mylist))

error: unbound function - MYLIST

> (append (list a) mylist)

(APPLE 1 2 3 4)

> (defun square (a)

     (* a a)

)

SQUARE

> (square 356)

126736

> (defun sum_square (a b)

    (+  (* a a) (* b b) )

)

SUM_SQUARE

> (sum_square 2 4)

20

> (trace sum_square)

(SUM_SQUARE)

> (sum_square 2 4)

Entering: SUM_SQUARE, Argument list: (2 4)

Exiting: SUM_SQUARE, Value: 20

20

> (trace +)

(+ SUM_SQUARE)

> (sum_square 2 4)

Entering: SUM_SQUARE, Argument list: (2 4)

 Entering: +, Argument list: (4 16)

 Exiting: +, Value: 20

Exiting: SUM_SQUARE, Value: 20

20

> (trace *)

(* + SUM_SQUARE)

> (sum_square 2 4)

Entering: SUM_SQUARE, Argument list: (2 4)

 Entering: *, Argument list: (2 2)

 Exiting: *, Value: 4

 Entering: *, Argument list: (4 4)

 Exiting: *, Value: 16

 Entering: +, Argument list: (4 16)

 Exiting: +, Value: 20

Exiting: SUM_SQUARE, Value: 20

20

> (untrace)

NIL

> (sum_square 2 4)

20

> (print 'a)

A

A

> (print a)

error: unbound variable - A

> (round 3.5)

4

-0.5

> (round 3.6)

4

-0.4

> (round 3.2)

3

0.2

> (type-of 7)

FIXNUM

> (type-of 3.6)

FLONUM

> (type-of 7/2)

RATIO

> (type-of 'apple)

SYMBOL