CS 403 Fall 2002 Class Notes: Functional Programming in LISP, Study notes of Programming Languages

The class notes for cs 403 at the university of alabama, fall 2002 semester. The notes cover chapter 9 on functional programming using lisp. Topics include the history and syntax of lisp, basic lisp functions, and examples. Students are encouraged to read chapter 10 for the next class and are invited to attend an acm meeting with a programming competition and a presentation on weightlessness.

Typology: Study notes

Pre 2010

Uploaded on 02/25/2010

koofers-user-q39
koofers-user-q39 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Fall 2002 CS 403 Class Notes Page 1
CS 403 – 10/22/2002
Today
Chapter 9: Functional Programming -LISP
Reading Assignment for Thursday
Chapter 10
Fall 2002 CS 403 Class Notes Page 2
Association of Computing Machinery
“The First Society in Computing”
We will be having a meeting, Thursday, October 24, 2002
at 7:00 p.m. in EE 119.
Join us to find out more details about the upcoming
“HackOff” Programming Competition - Cash Prizes.
Also, find out what it’s like to ride the “Vomit Comet.”
Anne Thomas will be sharing her experiences in weightlessness.
For more info, visit us at www.acm.eng.ua.edu
ACM
Fall 2002 CS 403 Class Notes Page 3
Advising – Ends this week!
All advising should be finished this week.
Seniors who are close to graduation may
sign up for individual advising
appointments.
See the CS Dept web page for more
details.
If you have any problems or know of any
problems, see me!
pf3
pf4
pf5

Partial preview of the text

Download CS 403 Fall 2002 Class Notes: Functional Programming in LISP and more Study notes Programming Languages in PDF only on Docsity!

Fall 2002 CS 403 Class Notes Page 1

CS 403 – 10/22/

  • Today
    • Chapter 9: Functional Programming- LISP
  • Reading Assignment for Thursday
    • Chapter 10

Fall 2002 CS 403 Class Notes (^) Page 2

Association of Computing Machinery “The First Society in Computing”

We will be having a meeting, Thursday, October 24, 2002 at 7:00 p.m. in EE 119. Join us to find out more details about the upcoming “HackOff” Programming Competition - Cash Prizes.

Also, find out what it’s like to ride the “ Vomit Comet .” Anne Thomas will be sharing her experiences in weightlessness. For more info, visit us at www.acm.eng.ua.edu

ACM

Advising – Ends this week!

  • All advising should be finished this week.
  • Seniors who are close to graduation may

sign up for individual advising

appointments.

  • See the CS Dept web page for more

details.

  • If you have any problems or know of any

problems, see me!

Fall 2002 CS 403 Class Notes Page 4

LISP – a LISt Processing Lang.

  • Also said to be an acronym for “Lots of

Irrelevant Stupid Parentheses”

  • Developed in the ’50s
  • Need: to handle relationships between

data including high level concepts

  • John McCarthy, one of the pioneers of AI

(then at MIT) did lots of the original

design & development.

Fall 2002 CS 403 Class Notes (^) Page 5

LISP History

  • No early attempt to standardize, so many

variants developed.

  • ANSI standard in 1992 over 1000 pages

long Æ “Common LISP”

  • More recent versions
    • Scheme (used at MIT for all freshman CS courses)
    • Haskell
    • ML

LISP Syntax

  • Can be taught in one lecture, even to freshmen!
  • Balance parentheses!
  • Put “atoms” into “lists” with parentheses.
  • In many cases, the first atom of the list is a function, and the rest are arguments.
  • Functions return atoms, lists, ‘t’ (true) and ‘nil’ (false).
  • Functional languages live to return a value!

Fall 2002 CS 403 Class Notes Page 10

LISP ‘COND’ function

  • How to test for “if” or “case”? (COND ((predicate) (value to return)) ((predicate2) (value2 to return)) … ( T (default value to return) ) )

Fall 2002 CS 403 Class Notes (^) Page 11

Defining a LISP function

(DEFUN Foo (arg1 arg2 …) (… body of function Foo …) )

  • Define a name (an atom) and bind a value to it: (SET ‘text ‘(a funny phrase))
  • This returns (a funny phrase) and has the side effect of binding the name to the list.

An Example: Length

(DEFUN LENGTH(X) (COND ((NULL X) 0) (T (+ 1 (LENGTH (CDR X)))) ) )

  • (LENGTH ‘(A B C D)) returns 4
  • What do these return?
    • (LENGTH ‘(A B (C D))
    • (LENGTH ‘((A B C D))
    • (LENGTH ‘(A (B) C (D))

Fall 2002 CS 403 Class Notes Page 13

Another Example: Member

(DEFUN MEMBER(X Y) (COND ((NULL Y) nil) ((EQUAL X (CAR Y)) T) ((T (MEMBER X (CDR Y)))) ) )

  • (MEMBER ‘A ‘(B C A D (E F))) returns T
  • (MEMBER ‘E ‘(B C A D (E F)))?
  • (MEMBER ‘X ‘(B C A D (E F)))?

Fall 2002 CS 403 Class Notes (^) Page 14

Why is LISP good for AI?

  • It can be used to represent all types of relationships, and all kinds of data.
  • Consider the “Don Smith” example on p. 321.
    • Name, age, salary, hire date…
    • Could add anything else you like.
  • Data structured this way is called a “property list” or “p-list”.
  • An alternative is to list pairs of information:
    • ((name ‘(Don Smith)) (age 45) (salary 30000) …)
  • Called an “association list” or “a-list”

Useful for AI?

  • Many expert systems applications rely on

putting together “frames” of information.

  • These often consist of a-lists:
    • When X & Y & Z, then do something…
    • (Restaurant (Have-Menu T) & (Have-Drinks T) & (Waiter-Is-Here T) Î (Place-Order) )