1a: BSL Constants Functions Booleans and If Expressions Flashcards
What is the purpose of contact definitions (convenience)?
Constant definitions are more than just convenience. They (1) help us make programs that are easy to read-comprehensible to other programmers (2) easy for other people to change in the future. Those two properties, READABILITY and CHANGEABILITY are two of the most important properties a program can have.
How do you write a constant definition so you can give names to a value you use in other parts of the program?
(define )
(define WIDTH 400)
(define HEIGHT 600)
Defining a constant does not give it a value.
(* WIDTH HEIGHT) = 240,000
How does DrRacket evaluate constants?
Evaluate the expression and record the resulting value as the value of the constant with the given name. The value is the recorded value.
What are functions?
They are the mechanism in the beginning student language that’s going to let you write programs that produce a different value each time they run. They are important and powerful and FORM THE BULK OF OUR PROGRAMS.
When are function definitions useful?
They are useful for organizing redundancies, needless duplicated code and cumbersome code. f(x) = 2 * x. X is the parameter (the varying value) and the rest of the function definition is the unchanging portion.
How do functions work?
define (bulb c) –>calling this function bulb and the parameter “c”. Then I’m going to write the body of the function as (circle 40 “solid” c). f(x) = 2 * x. i.e. in the previous terminology [bulb(c) = circle 40 “solid” x]
What’s the benefit of using a function?
It makes the CODE more CONCISE; and if the functions is NAMED well, it gives the code more MEANING as well.
IF you’ve played 20 questions, the answer to true-false questions can say a lot. What is a boolean?
It is an answer value to a true/false question that are fundamental in computer programs. There are also primitive true-false questions and if statements that use them.
What are the two values that represent true/false answers in DrRacket?
true and false (we get the value themselves when we run them).
What is an example of a primitive true/false question?
(define WIDTH 100) (define HEIGHT 100) (> WIDTTH HEIGHT) = Answer false. The > is a primitive true/false question. >= primitive would give a true. These are called PREDICATES. (string=? “foo” “bar”) = false.
What is an image primitive true/false question or a predicate?
(require 2htdp/image)
(define I1 (rectangle 10 20 “solid” “red”) (define I2 (rectangle 20 10 “solid” “blue”) (< (image-width I1) (image-width I2) = true.
How do your form an if expression?
“(if () )”. The first expression is a question that must produce a boolean (< (image-width I1) (image width I2)). The second expression is the true answer and the third expression is the answer if the first expressions is false. The question expressions calls a PREDICATE of some form (something that asks a decisions of TRUE/FALSE: comparing).
Write an if expression that determines the shape of image I1.
if (< (image-WIDTH I1) (image-HEIGHT I1)) “tall” “wide”). Dr.Racket gives the right answer when run and also highlights the false answer in the code area.
What are the DETAILED RULES for EVALUATIING IF EXPRESSIONS?
(1) If the question expression (1st part-3 sections), is NOT a value, evaluate it, and replace with value. (2) If the question is true replace entire if expression with the TRUE answer expression. If the question is false replace ENTIRE IF expression with false answer expression. If THE QUESTION is a value other than true or false so produce an error.
How do use the PRIMITIVE AND OR and NOT?
AND is a special kind of expression like if. (and [expression1] [expression2]…). All expressions MUST PRODUCE Boolean value TRUE. Then is will produce a true boolean answer. OR expression stops evaluating when it receives the first true value.