200 likes | 213 Views
Explore the concepts of Booleans and Backus-Naur Form (BNF) in Racket programming, covering syntax, definitions, and abstraction. Learn to represent programs compactly and understand the laws of thought. Dive into functions, lists, and more.
E N D
WiCS Women In Computer Science Dedicated to improving diversity and inclusion across gender identity in CS. Email: wics@lists.cs.brown.edu Add yourself to the listserv:https://tinyurl.com/brownuwics
Warmup: a new kind of data • Boolean datatype • George Boole, “The Laws of Thought” • Exactly two values • true • false • In Racket, these are written true, false • Both of these are keywords • That means you can’t use them as names • NB: so far three kinds of data: numbers, strings, booleans • Three still to come: functions, lists, (structures)
Last class • Broke Racket program text into pieces called “tokens” • Saw a standard form for definitions • Learned a way to compactly represent that standard form • Backus—Naur form, called BNF • Always written in green on slides • Some parts informal; written in italics
A small note on the behavior of definitions (demo) • This is not about what’s legal as a racket program (informally, what’s “grammatically correct”), but about what happens when you RUN a racket program • If you define the same thing twice, you get an error: (define class 17) (define class 18) generates an error!
Why am I obsessing about syntax? • Syntax: the “grammar” of the language, what it’s legal to write <prog> := <defn>* [<expr>] <defn> := (define <name> <expr>) … • Each item on the left-hand side of the BNF for Racket will become a named thing in your Rackette assignment later in the semester! • As you’re trying to write a program, it’s nice to have a guide to what might possibly work.
Our current BNF description of Racket Read as “or” <prog> := <defn>* [<expr>] <defn> := (define <name> <expr>) <name> := <CS17name> | <othername> <CS17name> := sequence of letters, digits, hyphens, starting with a letter, usually lowercase <othername> := token consisting of non-special characters that can’t be interpreted as a number and that isn’t a keyword <expr> := lots to fill in here!
Abstraction • Breaking things down into pieces (“tokens”, for our language) and • Giving rules for “legally” assembling pieces… • Makes describing the legal Racket programs fairly simple • An example of abstraction: • Don’t sweat the font • Don’t worry about what things mean yet • Focus on a narrow task, and ignore everything else
We can use BNF to describe other stuff • Example: tokens are single letters. No spaces allowed. <word> := <capital> <letter>* <capital> := A | B | C <letter> := a | b | c … | z • Legal “words” defined by this set of rules? Abd Bed Armchair Ccccc Activity: What’s an example of something that doesn’t fit this definition of word? cC, ABA, 14 Activity: What’s the shortest possible thing that fits this definition? (Multiple correct answers) A, B, C
Application of “matching patterns” as in BNF: Eliza • A program that breaks English sentences into words • Looks for patterns, like Ihate <something>. • Here “something” can be any sequence of words • When eliza gets a pattern it recognizes, it provides a response, like Why do you hate <something>?,filling in exactly the same text. • The author of the program gets to provide a lot of patterns/responses, and when you use the program, it feels almost as if you’re having a conversation. • What about input that doesn’t match any pattern? • We always include, as a final pattern, something that matches anything • And as a response, we use something like Tell me more.
Thoughts • Every program you write has consequences • They’re really hard to predict • Sometimes they’re hard to see because of the assumptions we make. • Who can’t use Eliza?
A BNF description for the expressions we’ve encountered so far <expr> := <name> | <num> | <misc> <num> := stuff that looks like a number <misc> := ( <op> <expr> <expr> ) <op> := + | - | * | / [NB: those last two lines will soon be replaced]
A few words from math • Racket is based on the mathematical notion of function • We’ll review that here • My notion of “function” may be different from yours • Yours may have been simplified to make it easy to teach • Mine is universally accepted by mathematicians • The difference is very small • Let’s start with “set”
Sets • A set is a mathematical representation of a collection of things • If is a set, and is some mathematical entity (a number, a function, a geometric point), then one of two things is true: • is in the set , which we write , or • is not in the set , which we write • When , we say is an element of • “Are you in or are you out?” • This completely describes the set
Ways to describe sets: Natural language • Natural language description: “ is the set of all even numbers” • Easy to read • Usually easy to understand • Can have complications (see…any logic course) • Activity • Using people in this classroom, describe a set called • Your answer should look like is the set consisting of … “ Sample answers • is the set consisting of all CS17 students” • is the set consisting of me and the person to my left” • …
Ways to describe sets: Enumeration • Enumeration: write out the elements of the set between braces. • Example1: • For this example, the statements are all true. • Example 2: • For this example, the statements are all true. • Conclusion: you can often write the same set in multiple ways using enumeration • Example 3: . That’s the same set again. The only things that are elements of are , , and
Activity • Describe a set (call it ) containing the first names of you and your neighbors. • [If you’re all alone, you may pretend Spike is your neighbor]
A special enumerated set • The empty set • It’s a set for which the statement is always false. • A weird enumerated set: • Has exactly three elements. • Two are numbers • One is a two-element set.
Ways to describe sets: Restriction • Restriction: You say “set consists of all elements of set that have such-and-such a property.” • Digression: We often use the name to denote the set of natural numbers, i.e., • Example of restriction: • Fancy-pants math way of writing that: The vertical bar is read “such that.”