1a: BSL Constants Functions Booleans and If Expressions Flashcards

1
Q

What is the purpose of contact definitions (convenience)?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you write a constant definition so you can give names to a value you use in other parts of the program?

A

(define )

(define WIDTH 400)
(define HEIGHT 600)
Defining a constant does not give it a value.

(* WIDTH HEIGHT) = 240,000

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How does DrRacket evaluate constants?

A

Evaluate the expression and record the resulting value as the value of the constant with the given name. The value is the recorded value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are functions?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When are function definitions useful?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do functions work?

A

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]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What’s the benefit of using a function?

A

It makes the CODE more CONCISE; and if the functions is NAMED well, it gives the code more MEANING as well.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

IF you’ve played 20 questions, the answer to true-false questions can say a lot. What is a boolean?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the two values that represent true/false answers in DrRacket?

A

true and false (we get the value themselves when we run them).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is an example of a primitive true/false question?

A

(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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is an image primitive true/false question or a predicate?

A

(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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do your form an if expression?

A

“(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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Write an if expression that determines the shape of image I1.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the DETAILED RULES for EVALUATIING IF EXPRESSIONS?

A

(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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do use the PRIMITIVE AND OR and NOT?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the ROLE of the STEPPER?

A

Stepper is a functionality that can help you understand the step by step evaluation of complex expressions when it’s too complicated to see just by looking at the expression. THIS IS WHY IT’S IMPORTANT TO KNOW THE EVALUATION RULES.

17
Q

How do you run the STEPPER?

A

You go to the top right and click Step >1. It’s next to the Run > symbol.

18
Q

What is the stepper always showing you?

A

It’s always showing what’s the innermost expressions where all the operands are values. Highlights them. Steppers are great for figuring out why a function IS NOT producing the value you think it should be.

19
Q

How do you discover primitives in Dr. Racket?

A

(1) Take a guess. (2) SEARCH and Scroll or LOOK UP and scroll (Right click and pick SEARCH in Help Desk for the primitive).