Recursively Specified Data - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Recursively Specified Data

Description:

Define the set S to be the smallest set of natural numbers satisfying the ... (lambda (s) (if (number? S) 1 ( (count-nodes (cadr s)) (count-nodes (caddr s)) 1 ) ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 15
Provided by: davidwoo4
Category:

less

Transcript and Presenter's Notes

Title: Recursively Specified Data


1
Recursively Specified Data
  • Section 1.1

2
1.1.1 Inductive Specification
  • Define the set S to be the smallest set of
    natural numbers satisfying the following two
    properties
  • 1) 0 ? S, and
  • 2) Whenever x ? S, then x 3 ? S.
  • 0, 3, 6, 9,

3
Typical Inductive Definition
  • 1) Some specific values must be in S.
  • 2) If certain values are in S, then certain
    other values are also in S.

4
List-of-numbers
  • The set list-of-numbers is the smallest set of
    values satisfying the two properties
  • 1) The empty list is a list-of-numbers, and
  • 2) If L is a list-of-numbers and n is a
    number, then the pair (n . L) is a
    list-of-numbers
  • ( ) is a list-of-numbers
  • (3 . ( ) ) is a list-of-numbers
  • (9 . ( 3 . ( ) ) ) is a list of numbers
  • (9 3)

5
Backus-Naur Form (BNF)
  • A language defined to describe the syntactic
    structure of programming languages
  • Non-terminal symbols
  • Terminal symbols
  • Productions
  • Also used to define sets of values
  • ltlist-of-numbersgt ( )
  • ltlist-of-numbersgt (ltnumbergt .
    ltlist-of-numbersgt)

6
Leftmost Derivation with BNF
  • ltl-o-ngt ( )
  • ltl-o-ngt (ltnumgt . ltl-o-ngt)
  • Replacements
  • ltl-o-ngt (ltnumgt . ltl-o-ngt)
  • ( ltnumgt . (ltnumgt . ltl-o-ngt))
  • ( ltnumgt . (ltnumgt . ( ltnumgt .
    ltl-o-ngt)))
  • ( ltnumgt . ( ltnumgt . ( ltnumgt
    . ( ))))
  • ( ltnumgt . ( ltnumgt . ( 8 . (
    ))))
  • ( ltnumgt . ( 4 . ( 8 . ( ))))
  • ( 7 . ( 4 . ( 8 . ( ))))

7
BNF Shortcuts
  • ltl-o-ngt ( ) ( ltnumgt . ltl-o-ngt )
  • Kleen star zero or more items
  • ltl-o-ngt ( ltnumbergt )
  • Kleen plus one or more items

8
S-Lists
  • lts-listgt (ltsym-exprgt)
  • ltsym-exprgt ltsymgt lts-listgt
  • ltsym-exprgt (ltsym-exprgtltsym-exprgtltsym-exprgt)
  • ( (ltsym-exprgt) ltsym-exprgt
    (ltsym-exprgt))
  • ((ltsymgt) ltsym-exprgt
    (ltsym-exprgt))
  • ((ltsymgt) ltsymgt (ltsym-exprgt))
  • ((ltsymgt) ltsymgt (ltsymgt))
  • (( x ) ltsymgt (ltsymgt))
  • (( x ) ltsymgt ( a ))
  • (( x ) p ( a ))

9
Binary Trees
  • ltbtreegt ltnumgt (ltsymgt ltbtreegt ltbtreegt )
  • ltbtreegt (ltsymgt ltbtreegt ltbtreegt)
  • ( x ltbtreegt ltbtreegt)
  • (x (ltsymgtltbtreegtltbtreegt)
    ltbtreegt)
  • (x ( y ltbtreegtltbtreegt)
    ltbtreegt)
  • (x ( y ltnumgtltbtreegt)
    ltbtreegt)
  • (x ( y ltnumgtltnumgt) ltbtreegt)
  • (x ( y ltnumgtltnumgt) ltnumgt)
  • (x ( y 9 ltnumgt) ltnumgt)
  • (x ( y 9 7) ltnumgt)
  • (x ( y 9 7) 6)

x
y
6
9
7
10
Lambda Calculus Grammar
  • ltexpressiongt
  • ltidentifiergt
  • ( lambda (ltidentifiergt) ltexpressiongt)
  • (ltexpressiongtltexpressiongt)

11
Scheme Data Categories
  • ltlistgt (ltdatumgt)
  • ltdotted-datumgt (ltdatumgt . ltdatumgt)
  • ltvectorgt (ltdatumgt)
  • ltdatumgt ltnumbergt ltsymbolgt
  • ltbooleangt
    ltstringgt
  • ltlistgt
    ltdotted-datumgt
  • ltvectorgt

12
Context Free Productions
  • Here is a context-free rule
  • ltxgt ltxgt ltsymgt
  • A context sensitive rule would look like this
  • a ltxgt b a ltygt b
  • Context sensitive parts of a language are often
    described by other techniques.

13
Programs Based on Induction
  • E( 0, b) 1 (n 0)
  • E(n,b) b x E(n-1,b) (n gt 0)
  • (define E
  • (lambda (n b)
  • (if (zero? n)
  • 1
  • ( b (E (- n 1) b)))))

14
Count-nodes
  • ltbtreegt ltnumbergt (ltsymgtltbtreegtltbtreegt))
  • (define count-nodes
  • (lambda (s)
  • (if (number? S)
  • 1
  • ( (count-nodes (cadr s))
  • (count-nodes (caddr
    s))
  • 1 ))))
Write a Comment
User Comments (0)
About PowerShow.com