Lecture 18 – October 26th

 

10/26/06 notes – courtesy of Jennifer Leahy

 

Snobol -> Be careful when exceeding array bounds: It won't exceed the array bounds, but won't give you an error message either.

 

Fortran -> Does not do array bounds checking -> will just print out what is currently in that place in memory.

 

Snobol Swap Problem -> Snobol passes by value.  If there is a separate SWAP function, will have to pass by reference and dereference.  If just passing the array, do not have to dereference.

 

Lisp ->

Good for students: Very few linguistic concepts

Difficult for students: Functional language (everything is a function call),

                        emphasizes recursion

What will be a pain -> Unmatched ( )

 

To get into xlispsml.exe, click on it.

To get out of xlispsml.exe, (exit )

 

LISP has two things: atoms & lists

* lists have ( ) around it

* lists can be made up of other lists, or  can be a function call

      function call: (+ 2 3)

      list: (a b c)

 

 

Slide Notes: (see slide number to see which slide note is referring to)

Slide #4 -> There are NO commas in between items in a list

Slide #5 -> an atom cannot be broken down into component parts (like a scalar)

Slide #6 -> Eval tries to evaluate every expression, and interprets it to be a list, atom, or whatever

Slide #7 -> VERY IMPORTANT: CAR is the first item in the list     (Contents of the address register)

                      CDR is the rest of the list           (Contents of the decrement register)

Slide #7 -> Everything is a function call, there are no procedures

Slide #8 -> We are using LISP 1.5 [Old LISP, not New LISP]

Slide #9 -> > is the prompt

Slide #10 -> nth position: First item in the list has an index of 0

 

Example:

Be careful of the ()

( (1 2) 3 (a (b c) ) ) <- bad function due to missing 'tick'

1 2   1   2  3   2 1 0

 

( -> count up

) -> count down

 

bad function because EVAL is trying to evaluate the function (though it's not a function)

Try this:

>(car '((1 2) 3 (a (b c)))) -> will return (1 2)

' will tell the EVAL not to evaluate the function

>(cdr '((1 2) 3 (a (b c)))) -> will return (3 (a (b c)))

 

 

Random Function Fun:

>(car (cdr '((1 2) 3 (a (b c)))))

The car of the cdr is the cadr

>(cadr '((1 2) 3 (a (b c))))

 

>(cdr (cdr '((1 2) 3 (a (b c)))))

The cdr of the cdr is the cddr

>(cddr '((1 2) 3 (a (b c))))

 

 

When doing cons->

(cons <list or atom> <list>)

When doing append->

(append <list> <list>) -> (.......)

[Both arguments must be lists, neither can be an atom]

 

Functions: (See slide 13)

;fib(1) = 1 (= number 1) return 1

;fib(2) = 1 (= number 2) return 1

;fib(n) = fib(n-1) + fib(n-2)

(defun myfib(x)

      (cond

      ( (= x 1) 1)

      ( (= x 2) 1)

      (t (+ fib(- n 1) fib(- n 2)))

      )

)

 

 

·        List of xlisp functions and other information about LISP from David Betz (author of xlisp) posted at Carnegie Mellon at  www.cs.cmu.edu/~rbd/doc/nyquist/part16.html

 

 

·        Class slides 

 

 

·        Fibonacci code examples – see the difference by downloading and running them.

·        Fib1.txt   fib2.txt   fib3.txt  fiblisp.txt  - they illustrate producing output

 

·        Note that you can load a notepad file into LISP by typing  (load “filename.ext”) at the LISP prompt.  If you get back a T (which stands for true) it worked.  If you get back nil which stands for false, it didn’t get loaded.  Either your file was not found or your code was not correct.

 

 

 

 


Computer Science 430 – Programming Languages

Oktoberfest 2006 (October 26th, 2006)

 

Remember: Test is on October 31st!

 

SNOBOL Follow-up from Last Class

 

SNOBOL will not tell you if things fail unless you use the :F(LABEL) to direct you to an error label

 

LISP – List Processing Language

 

       There are not any goto’s in LISP (lots of function calls and recursion…it cares about parentheses as opposed to spaces)

       Grammar

       List ::= (X1 X2 … Xn)

       X    ::= List | Atom

       Atom ::= Number | Name

       Number ::= … | -1024 | … | -2 | -1 | 0 | 1 | 2 | … | 1024 |…

       Name ::= A sequence of alphanumeric characters containing at least one letter and no separators

       Separator ::= .| space | (|)|[|]|’|tab|carriage return
all are acceptable

       Expressions

       (+ 1 2) ß Evaluatable Expression

       ‘(1 2 3) ß Don’t evaluate because of the ‘

       A ßVariable – may be unbound

       List: a sequence of atoms or other lists enclosed by parentheses

       Playing Around

> (+ 2 2) à 4
> ‘(+ 2 2)
à (+ 2 2)
> quote (+ 2 2)
à error: unbound variable
> (quote (+ 2 2) )
à (+ 2 2)

 

       To get out of LISP interpreter call > (exit)

       Semi-colon is a comment indicator

       Two very famous function calls

       car – contents of address register
returns an element (the first element)
if the first element is an atom it returns an atom
if the first element is a list it returns a list

       cdr – contents of data register
returns a list (with everything but the first element in it)

       Problems with Parentheses

       Count Up and then Count Down (Start on 1, end on 0)
( car ( cdr ( cdr ‘( 1 2 3 4 ) ) ) )
1        2       3       4            3210

       Other functions

       (car (cdr ‘())) à cadr

       (list 1 2 3 4 ) à (1 2 3 4 )
(list chickens were caught in troys room)
à error: unbound variable – CHICKENS (because the variables are not bound to values)
to fix:
(list ‘troy ‘loves ‘to ‘touch ‘his ‘pet ‘cow ‘bessy)
à (TROY LOVES TO TOUCH HIS PET COW BESSY)

       Cons – concatenate two arguments
(cons ‘(1 2 3) ‘(4 5 6))
à ((1 2 3) 4 5 6)

       Append – appends more than one list
(append ‘(Troy is) ‘(a nice gentleman))
à (TROY IS A NICE GENTLEMAN)

       Some of these functions return DOTS (they have special meanings that we are not going to discuss) DON’T GET THE DOTS!

(
     defun name
     (optional_input_variables)
     name_body
)

       Defun – this command is used to define functions which LISP refers to as a value-returning procedure


(
     defun Fib (someNum)
     ( cond
     ; fib (1) = 1
           (( = someNum) 1 )
     ; fib (2) = 1
           (( = someNum) 2 )    
     ; fib (2) = 1
           (( = someNum) 2 )
     ; fib (n) = fib (n -1) + fib (n – 2)
           (t (+ (Fib (- someNum 1)) (Fib (- someNum 2))))
     )
)