Lists defined inductively ( A ) is the smallest set satisfying this - - PowerPoint PPT Presentation

lists defined inductively
SMART_READER_LITE
LIVE PREVIEW

Lists defined inductively ( A ) is the smallest set satisfying this - - PowerPoint PPT Presentation

Lists defined inductively ( A ) is the smallest set satisfying this equation: LIST ( A ) f (cons a as ) j a 2 A ; as LIST( A ) g f () g LIST = [ 2 Equivalently, LIST ( A ) is defined by these rules: (E MPTY ) 2 List ( A ) () a 2 A as 2


slide-1
SLIDE 1

Lists defined inductively

LIST

(A ) is the smallest set satisfying this equation: LIST (A ) = f’()g [ f(cons a as) j a 2 A ;as 2 LIST(A )g

Equivalently, LIST

(A ) is defined by these rules:

’()

2 List (A )

(EMPTY)

a

2 A

as

2 List (A )

(cons a as)

2 List (A )

(CONS)

slide-2
SLIDE 2

One more inductive definition

A list of A is one of:

  • The empty list ’()
  • (cons a as), where a is an A and as is a list
  • f A
slide-3
SLIDE 3

Lists generalized: S-expressions

An ordinary S-expression is one of:

  • An atom (symbol, number, Boolean)
  • A list of ordinary S-expressions

Can write literally in source, with quote

slide-4
SLIDE 4 Scheme vs Impcore

New abstract syntax: LET (keyword, names, expressions, body) LAMBDAX (formals, body) APPLY (exp, actuals) New concrete syntax for LITERAL: (quote S-expression) ’S-expression

slide-5
SLIDE 5

Equations and function for append

(append ’() ys) == ys (append (cons z zs) ys) == (cons z (append zs ys)) (define append (xs ys) (if (null? xs) ys (cons (car xs) (append (cdr xs) ys))))

slide-6
SLIDE 6

Naive list reversal

(define reverse (xs) (if (null? xs) ’() (append (reverse (cdr xs)) (list1 (car xs)))))

slide-7
SLIDE 7

Reversal by accumulating parameters

(define revapp (xs ys) ; return (append (reverse xs) ys) (if (null? xs) ys (revapp (cdr xs) (cons (car xs) ys)))) (define reverse (xs) (revapp xs ’()))

slide-8
SLIDE 8

A-list example

  • > (find ’Building

’((Course 105) (Building Barnum) (Instructor Ramsey))) Barnum

  • > (val nr (bind ’Office ’Halligan-222

(bind ’Courses ’(105 150TW) (bind ’Email ’comp105-grades ’())))) ((Email comp105-grades) (Courses (105 150TW)) (Office Halligan-222))

  • > (find ’Office nr)

Halligan-222

  • > (find ’Favorite-food nr)

()

slide-9
SLIDE 9

Laws of association lists

(find k (bind k v a-l)) = v (find k (bind k’ v a-l)) = (find k a-l), provided k != k’ (find k ’()) = ’() --- bogus!