CS 603 Programming Language Organization Lecture 6 Spring

  • Slides: 11
Download presentation
CS 603: Programming Language Organization Lecture 6 Spring 2002 Department of Computer Science University

CS 603: Programming Language Organization Lecture 6 Spring 2002 Department of Computer Science University of Alabama © 2002 Joel Jones

Outline • Questions • Lots of code • Reading for next time © 2002

Outline • Questions • Lots of code • Reading for next time © 2002 Joel Jones

Questions • What data structures can be represented using LISP-like lists? • What data

Questions • What data structures can be represented using LISP-like lists? • What data structures can’t be represented using LISP-like lists? • Any other questions from the reading? © 2002 Joel Jones

Review: Operations on SExpressions: • car: first element of list or nil • cdr:

Review: Operations on SExpressions: • car: first element of list or nil • cdr: everything in list except first element • cons: if S = (S 1, …, Sn), then (cons S’ S) is (S’ S 1, …, Sn) • =: returns T if equal, nil otherwise, for non-lists and empty lists • number? , symbol? , list? , null? : T if argument of type, nil otherwise • +, -, *, /: like Chapter 1 language • <, >: T if both numbers and condition holds, nil otherwise © 2002 Joel Jones

. Some utility functions • (define caar (l) (car l))) • (define cadr (l)

. Some utility functions • (define caar (l) (car l))) • (define cadr (l) (car (cdr l))) • (define cadar (l) (car (cdr (car l)))) © 2002 Joel Jones

Let’s Play at the Board (but your books can’t play) • Insertion sort–given a

Let’s Play at the Board (but your books can’t play) • Insertion sort–given a list of n elements, sort the last n-1 recursively, then insert the first in its proper position. • (define insert (x l) … • (define insertion-sort (l) … © 2002 Joel Jones

Association Lists • association list(a-list)–a list of the form ((k 1 a 1), …,

Association Lists • association list(a-list)–a list of the form ((k 1 a 1), …, (km am)), where the ki are symbols (called keys) and the ai are attributes. • retrieve data: (define assoc (x alist) (if (null? alist) ‘() (if (= x (caar alist)) (cadar alist) (assoc x (cdr alist))))) © 2002 Joel Jones

Association Lists (cont. ) • add data (define mkassoc (x y alist) (if (null?

Association Lists (cont. ) • add data (define mkassoc (x y alist) (if (null? alist) (list 1 (list 2 x y)) (if (= x (caar alist) (cons (list 2 x y) (cdr alist)) (cons (car alist) (mkassoc x y (cdr alist)))))) © 2002 Joel Jones

Let’s play at the board again (No Books!) • Property lists–a-list where attribute is

Let’s play at the board again (No Books!) • Property lists–a-list where attribute is an attribute list • Examples – (set fruits ‘((apple ((texture crunch))) (banana ((color yellow))))) – (getprop ‘apple ‘texture fruits) crunchy • Write (getprop x p plist), where x is the individual, p is the property and plist is the property list © 2002 Joel Jones

Let’s play at the board again (No Books!) • (putprop x p y plist)

Let’s play at the board again (No Books!) • (putprop x p y plist) give individual x value y for property p in plist – (set fruits (putprop ‘apple ‘color ‘red fruits)) ->((apple ((texture crunchy)(color red)))(banana ((color yellow)))) © 2002 Joel Jones

Reading & Questions for Next Class • Chapter 2, pages 46 -51 © 2002

Reading & Questions for Next Class • Chapter 2, pages 46 -51 © 2002 Joel Jones