CMPU-145: Foundations of Computer Science Spring, 2019

Slides:



Advertisements
Similar presentations
Artificial Intelligence
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Discrete Structures Chapter 5: Sequences, Mathematical Induction, and Recursion 5.2 Mathematical Induction I [Mathematical induction is] the standard proof.
Introduction Even though the syntax of Scheme is simple, it can be very difficult to determine the semantics of an expression. Hacker’s approach: Run it.
Adapted from Discrete Math
Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office.
CS 152: Programming Language Paradigms February 24 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Introduction to Proofs
HOMEWORK REVIEW This is an if else statement layout if (condition) { code to be executed if condition is true; } else { code to be executed if condition.
CPS120 Introduction to Computer Science Iteration (Looping)
CPS120 Introduction to Computer Science Iteration (Looping)
Lecture on Set! And Local CS 2135 Copyright Kathi Fisler, 2002 This material requires Advanced Language Level.
Fall 2008Programming Development Techniques 1 Topic 17 Assignment, Local State, and the Environment Model of Evaluation Section 3.1 & 3.2.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office Functional Programming.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Functional Programming
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2016.
Functional Programming Languages
CprE 185: Intro to Problem Solving (using C)
Defining Macros in Lisp
6.001 Jeopardy.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 15 Macros
Introduction to Scheme
Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications) cs7100(Prasad) L8Interp.
CS 326 Programming Languages, Concepts and Implementation
Disjunctive Normal Form
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Great Theoretical Ideas in Computer Science
Introduction to Functional Programming in Racket
CSE341: Programming Languages Lecture 15 Macros
Propositional Calculus: Boolean Algebra and Simplification
Mini Language Interpreter Programming Languages (CS 550)
Chapter 1 The Foundations: Logic and Proof, Sets, and Functions
Alternate Version of STARTING OUT WITH C++ 4th Edition
Chapter 3: Introduction to Problem Solving and Control Statements
FP Foundations, Scheme In Text: Chapter 14.
The Metacircular Evaluator
CSE341: Programming Languages Lecture 15 Macros
TRUTH TABLES.
Rules of evaluation The value of a number is itself.
Explicit Application of Procedures, and Explicit Evaluation
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
6.001 SICP Variations on a Scheme
Introduction to Functional Programming in Racket
CSE341: Programming Languages Lecture 15 Macros
Chapter 3: Selection Structures: Making Decisions
CISC101 Reminders All assignments are now posted.
CSE341: Programming Languages Lecture 15 Macros
Chapter 3: Selection Structures: Making Decisions
CPSC 121: Models of Computation
Assignments and Procs w/Params
Lecture 3: Rules of Evaluation CS200: Computer Science
CPSC 121: Models of Computation
Logical and Rule-Based Reasoning Part I
Rules of evaluation The value of a number is itself.
More Scheme CS 331.
Defining Macros in Scheme
Validity and Soundness, Again
CSE341: Programming Languages Lecture 15 Macros
Truth tables.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2019.
CSE341: Programming Languages Lecture 15 Macros
Presentation transcript:

CMPU-145: Foundations of Computer Science Spring, 2019 Not Chapter 1, and not Chapter 2. It is the middle ground between light and shadow, between science and superstition, and it lies between the pit of man's fears and the summit of his knowledge.

(Bi)Conditional Statements from last week The conditional if α then β is true in virtually all cases The lone exception: when α is true and β is false. Also written as α => β 11/11/2019 CMPU 145 – Foundations of Computer Science

Conditional Statement: truth table (corrected) α β α=>β TRUE FALSE 11/11/2019 CMPU 145 – Foundations of Computer Science

Biconditional Statement: α The biconditional statement, α if and only if β is true whenever α and β have the same truth value α if and only if β is false whenever they have opposite truth values Also written as α iff β, α  β, or α≡β 11/11/2019 CMPU 145 – Foundations of Computer Science

Biconditional Statement: truth table (updated symbol) α β αβ TRUE FALSE TRUE (!) 11/11/2019 CMPU 145 – Foundations of Computer Science

NEW! Conditional / BiConditional Statements Material from https://en.wikipedia.org/wiki/Wason_selection_task Given 4 cards, each with a color on side, a number on the other Cards show a 3, 8, red color, brown color Which card (or cards) do you have to turn over to test the following: If a card shows an even number on one side of the card, Then the opposite side of the card is red. 11/11/2019 CMPU 145 – Foundations of Computer Science

NEW! Conditional / BiConditional Statements Material from https://en.wikipedia.org/wiki/Wason_selection_task Given 4 cards, each with a color on side, a number on the other Cards show a 3, 8, red color, brown color Which card (or cards) do you have to turn over to test the following: If a card shows an even number on one side of the card, Then the opposite side of the card is red. Can we all agree on this? If the 8 card is not red on the obverse, the proposition is not correct. α = T, β = F, F What else, if anything? 11/11/2019 CMPU 145 – Foundations of Computer Science

Let’s rephrase as a social construct If you receive “meals on wheels” (ready-made food delivered to your home) Then you are over 65 years of age. Two cards represent ages: 19 and 72 Two cards represent meals on wheels: Receiving MOW, Not receiving MOW What card(s) should you turn over in this case? 11/11/2019 CMPU 145 – Foundations of Computer Science

Let’s rephrase as a social construct If you receive “meals on wheels” (ready-made food delivered to your home) Then you are over 65 years of age. Two cards represent meals on wheels: Receiving MOW, Not receiving MOW Two cards represent ages: 19 and 72 What card(s) should you turn over in this case? Receiving MOW ! Because: There better be a number >= 65 on the obverse. If there’s like a 55, someone is ‘ripping off’ the govt. α = T, β = F, F or α = T, β = T, T 11/11/2019 CMPU 145 – Foundations of Computer Science

Let’s rephrase as a social construct If you receive “meals on wheels” (ready-made food delivered to your home) : α Then you are over 65 years of age: β Two cards represent meals on wheels: Receiving MOW, Not receiving MOW Two cards represent ages: 19 and 72 What card(s) should you turn over in this case? Receiving MOW ! There better be a number >= 65 on the obverse. If there’s like a 55, someone is ‘ripping off’ the govt. α = T, β = F, F or α = T, β = T, T Similarly, if a 19 year old is getting meals on wheels… α = T, β = F, F and the statement will not be true. Think of an example where someone or some group needs to qualify for an achievement or benefit… driving, drinking, running a marathon, etc. 11/11/2019 CMPU 145 – Foundations of Computer Science

Revisiting Power Sets (p24 in our textbook) Let’s take a look at exercise 1.4 (a) on Power sets: Prove the following: For any sets A and B, if A ⊆ B, then 𝒫(A) ⊆ 𝒫(B) 11/11/2019 CMPU 145 – Foundations of Computer Science

CMPU 145 – Foundations of Computer Science Power Set Theorem For any sets A and B, if A ⊆ B, then 𝒫(A) ⊆ 𝒫(B) 11/11/2019 CMPU 145 – Foundations of Computer Science

CMPU 145 – Foundations of Computer Science Power Set Theorem For any sets A and B, if A ⊆ B, then 𝒫(A) ⊆ 𝒫(B) How do we approach this? Let’s restate the premise with concrete example… Let A and B be two sets s.t. A ⊆ B Then let’s restate our goal, if we can… Goal: show that 𝒫(A) ⊆ 𝒫(B) i.e. Restated Goal: show that for all x, if x ⊆ 𝒫(A), then x ⊆ 𝒫(B) Now the proof… By definition of power set: since x ⊆ 𝒫(A), it follows that x ⊆ A We’ve proven that: since x ⊆ A and A ⊆ B, it follows that x ⊆ B By definition of power set: since x ⊆ B, it follows that x ⊆ 𝒫(B) The restated goal has been shown true, the theorem is proven true too! 11/11/2019 CMPU 145 – Foundations of Computer Science

Some information for tonight’s lab don’t forget to right click on an api to find out what it does! don’t forget to step through your code to debug if the output is not what you expect if code doesn’t build as you expect it to, check/change the language being used! 11/11/2019 CMPU 145 – Foundations of Computer Science

DrRacket api’s (procedures) From the Racket Reference, let’s look more closely at: cons list append map 11/11/2019 CMPU 145 – Foundations of Computer Science

CMPU 145 – Foundations of Computer Science DrRacket api’s cons (cons x y) → list?   x : any/x  y : list? Constructs a list. list append map 11/11/2019 CMPU 145 – Foundations of Computer Science

CMPU 145 – Foundations of Computer Science DrRacket api’s cons list (list x ...) → list?   x : any/c Constructs a list of its arguments. > (list 1 2 3 4 5 6 7 8 9 0) (1 2 3 4 5 6 7 8 9 0) append map 11/11/2019 CMPU 145 – Foundations of Computer Science

DrRacket api’s (procedures) cons list append (append l ...) → (listof any)   l : (listof any) Creates a single list from several, by concatenation of the items. In ISL and up: append also works when applied to one list or none. > (append (cons 1 (cons 2 '())) (cons "a" (cons "b" '())))(1 2 "a" "b") > (append)() map 11/11/2019 CMPU 145 – Foundations of Computer Science

DrRacket api’s (procedures) map : Constructs a new list by applying a function to each item on one or more existing lists: Form: (map f (list x-1 ... x-n)) = (list (f x-1) ... (f x-n)) (map f (list x-1 ... x-n) (list y-1 ... y-n)) = (list (f x-1 y-1) ... (f x-n y-n)) > (map add1 '(3  -4.01  2/5)) (4 -3.01 7/5) > (map (lambda (x) (list 'my-list (+ x 1)))  '(3  -4.01  2/5)) ((my-list 4) (my-list -3.01) (my-list 7/5)) > (map (lambda (x y) (+ x (* x y))) '(3 -4 2/5) '(1 2 3)) (6 -12 8/5) 11/11/2019 CMPU 145 – Foundations of Computer Science

DrRacket api’s (procedures) let and let* (let* ([name expr-for-let] ...) expression) Like letrec, but each name can only be used in expression, and in expr-for-lets occuring after that name. syntax (let ([name expr-for-let] ...) expression) Like letrec, but the defined names can be used only in the last expression, not the expr-for-lets next to the names. (letrec ([name expr-for-let] ...) expression) Like local, but with a simpler syntax. Each name defines a variable (or a function) with the value of the corresponding expr-for-let. If expr-for-let is a lambda, letrec defines a function, otherwise it defines a variable. (local [definition ...] expression) Groups related definitions for use in expression. Each definition can be either a define or a define-struct. When evaluating local, each definition is evaluated in order, and finally the body expression is evaluated. Only the expressions within the local (including the right-hand-sides of the definitions and the expression) may refer to the names defined by the definitions. If a name defined in the local is the same as a top-level binding, the inner one “shadows” the outer one. That is, inside the local, any references to that name refer to the inner one. 11/11/2019 CMPU 145 – Foundations of Computer Science

DrRacket api’s (procedures) let and let* (let* ([name expr-for-let] ...) expression) Like letrec, but each name can only be used in expression, and in expr-for-lets occuring after that name. syntax (let ([name expr-for-let] ...) expression) Like letrec, but the defined names can be used only in the last expression, not the expr-for-lets next to the names. (letrec ([name expr-for-let] ...) expression) Like local, but with a simpler syntax. Each name defines a variable (or a function) with the value of the corresponding expr-for-let. If expr-for-let is a lambda, letrec defines a function, otherwise it defines a variable. (local [definition ...] expression) Groups related definitions for use in expression. Each definition can be either a define or a define-struct. When evaluating local, each definition is evaluated in order, and finally the body expression is evaluated. Only the expressions within the local (including the right-hand-sides of the definitions and the expression) may refer to the names defined by the definitions. If a name defined in the local is the same as a top-level binding, the inner one “shadows” the outer one. That is, inside the local, any references to that name refer to the inner one. WHAT??!? 11/11/2019 CMPU 145 – Foundations of Computer Science

Let and Let* (with apologies to Paul McCartney) Let’s start again. In Professor Hunsberger’s textbook, Introduction to Computer Science via Scheme, p35, the special form of define is discussed. Check out example 7.1.1 11/11/2019 CMPU 145 – Foundations of Computer Science

CMPU 145 – Foundations of Computer Science Let The Racket Guide provides the following introduction in 4.6 Local Binding Although internal defines can be used for local binding, Racket provides three forms that give the programmer more control over bindings: let, let* and letrec. A let form binds a set of identifiers, each to the result of some expression, for use in the let body: (let ([id expr] ...) body ...+) The [id expr] is not very different than the define a few slides back. An example, already in progress… …(let ([x (+ x 1 2 3)] [y (+ x 4 5 6)]) body) The closest parallel may be to think of let constructs as temporary #define macros in c/c++. 11/11/2019 CMPU 145 – Foundations of Computer Science

CMPU 145 – Foundations of Computer Science Let An example, already in progress… (define (favorite-number x) (let ([x (+ x 1 2 3)] [y (+ x 4 5 6)]) (+ x y + 21))) Here, expressions are evaluated from before the let expression If we test this define with x set to zero, what happens? [x (+ x 1 2 3)] => let x be set to 0 + 1 + 2 + 3 [y (+ x 4 5 6)] => let y be set to 0 + 4 + 5 + 6 ( + x y + 21 ) => (body) => 6 + 15 + 21 = 42 Pay attention to the red text. 11/11/2019 CMPU 145 – Foundations of Computer Science

CMPU 145 – Foundations of Computer Science Let* The Racket Guide provides the following for let* in 4.6 Local Binding The syntax of let* is the same as let: (let* ([id expr] ...) body ...+) The difference is that each id is available for use in later exprs, as well as in the body. Furthermore, the ids need not be distinct, and the most recent binding is the visible one. Another (!) example, already in progress… …(let*([x (+ x 1 2 3)] [y (+ x 4 5 6)]) body) The closest parallel may be to think of let constructs as temporary #define macros in c/c++. 11/11/2019 CMPU 145 – Foundations of Computer Science

CMPU 145 – Foundations of Computer Science Let* An example, already in progress… (define (favorite-number x) (let ([x (+ x 1 2 3)] [y (+ x 4 5 6)]) (+ x y + 21))) Here, expressions are evaluated based on previous bindings If we test this define with x set to zero, what happens? [x (+ x 1 2 3)] => let x be set to 0 + 1 + 2 + 3 = 6 [y (+ x 4 5 6)] => let y be set to 6 + 4 + 5 + 6 ( + x y + 21 ) => (body) => 6 + 21 + 21 = 48 :-\ Pay attention to the red and blue text. Y is setup based on the local binding within the let expression. 11/11/2019 CMPU 145 – Foundations of Computer Science