Create ten questions and answers on topics we have explored in the
past week (e.g., Boolean values, conditionals, preconditions and postconditions, precondition checking, and testing).
Title your email CSC 151.01 Flashcards for week 5 (YOUR NAME).
Include your questions and answers in the body of the email.
Submitted questions
Here you can find some of the questions and answers that have been
submitted so far (at least those that Sam had time to add).
Big picture
What is the difference between a keyword and a procdure?
For procedures, all the parameters are evaluated, then the procedure is applied. For keywords, custom orders of evaluation are possible. We also cannot use keywords in procedures like map.
What keywords have we learned?
and, or, not, lambda, if, cond, define, when. Maybe a few others.
Boolean values and Boolean operators
What is the difference between (andreal?positive?number) and (and(real?number)(positive?number))?
The first expression returns number, the second returns #t or #f
What is the order of parameters for tally?
(tallylstpred?)
What is the order of parameters for filter?
(filterpred?lst)
What is the format for and?
(andexp1exp2...)
How does and work?
It evaluates each expression in turn. If any of the expressions evaluates to false (#f), it returns false. Otherwise, it returns the value of the last expression.
What is (and)?
#t
What is the format for or?
(orexp1exp2...)
How does or work?
It evaluates each expression. If any is truish (that is, not false), returns the first one. Otherwise, it returns false.
What is (or)?
#f
What is the function of the procedure (tallylstpred?)?
It counts the number of elements of a list for which a certain predicate holds.
What is the function of the procedure (filterpred?lst)?
It extracts the elements of a list for which a certain predicate holds.
What does the procedure all do?
(allpred?lst) determines whether the predicate (pred?) holds for all elements in a list (lst).
What is a predicate?
A procedure that returns true or false.
What is Racket’s custom for naming predicates?
Predicates traditionally end in a question mark.
What is the difference between tally and filter?
tally counts the number of times the predicate holds in a list, filter returns a list of those values the predicate holds for. Also as a general note, the parameters for tally and filter are switched: (tallylstpred?)(filterpred?lst)
What does conjoin do?
It combines two unary predicates. This is useful with filter and other similar operations.
What is the difference between (regexp-match?regexpstr) and (regexp-match-exact?regexpstr)?
The difference is that while (regexp-match?regexpstr) finds whether or not a regexp appears at any point in a string, (regexp-match-exact?regexpstr) finds if the regexp matches the entire string exactly.
What is the difference between not and negate?
not reverses a value, returning #t or #f, while negate reverses the predicate, returning anew predicate.
What is the purpose of conjoin?
Similarly to negate’s relationship to not, conjoin allows us to use the equivalent of an and expression in combining two predicate into a new predicate.
Conditionals
What is the format for if?
(ifconditionconsequentalternative)
What does Racket do with an if expression do?
It evaluates the condition. If the condition is #t (or anything other than #f), Racket evaluates the consequent. If the condition is #f, Racket evaluates the alternative.
How does a cond expression work?
It works its way through the cond clauses, evaluating the guard at the beginning of each one, until it reaches a guard that succeeds (one that does not have #f as its value). Then it evaluates the corresponding consequents and returns the value of the last consequent.
What is the difference between a cond expression and a when expression?
The when expression is an alternative-free conditional expression; whereas, a cond procedure is a conditional expression that supports multiple tests and multiple alternatives.
How can one rewrite (iftestconsequentalternate) with and and or?
In many cases, (or(andtestconsequent)alternate), although that won’t work if the consequent evaluates to false.
Preconditions and Postconditions
What purpose do preconditions serve?
They document the requirements of inputs to the procedure. E.g. if a list has to be a certain length, specify that in the preconditions
What are some things to consider when writing a precondition?
Does the function depend on values named elsewhere? Are there any restrictions for the parameters? What inputs will and will not work?
What should be in the postconditions section?
A somewhat formal, detailed description of the output and relevant information about how it relates to the input.
They are two separate procedures that make up one larger procedure. A husk check the preconditions for a procedure and interacts with the outside world, while the kernel is the heart of the procedure. They exist to save time for checking preconditions.
Testing
What is the purpose of test suites?
To show that code achieves a desired goal without requiring formal proof. Also to help us clarify desired goals.
What is the difference between a check and a test?
A check is a singular comparison between an actual output and the expected. A test is a group of similar checks.