On this page:
Values
Expressions
Evaluations
Definitions
Stepping
Conditionals
But Wait, There’s More!
6.6

BSL Practice

This web page is deprecated.

Please see the main page for Fundamentals I.

This page is intended to provide extra practice with and further vocabulary regarding BSL syntax and semantics.

Values

A value is an instance of data. "barb", 5, and #f are all examples of values.

Sample Problem Write three more examples of values in BSL.

Expressions

An expression is either a value, a variable, or a function being called on expressions. For example, "Tweety bird", (+ 4 5) and x are all expressions.

Sample Problem Write three more examples of expressions in BSL.

Evaluations

An expression is evaluated to another expression, which is how a program advances when it is run. For example, the expression (+ 4 5) evaluates to the value 9. The expression (+ 6 (string-length "bobette")) evaluates to (+ 6 7), which then evaluates to 13.

Sample Problem Evaluate two other valid expressions in BSL: one that will error and one that won’t.

Much like in algebra, BSL evaluates its expressions left to right, and innermost before outermost. Evaluation stops when a value is reached (or an error occurs).

Definitions

A definition is either a constant, function, or structure definition.

(define BANANA-COST 10) is an example of a constant definition. BANANA-COST now evaluates to 10.

(define (add3 x) (+ x 3)) is an example of a function definition. add3 can now be used as the function in a function call.

(define-struct book [title pages]) is a structure definition. book?, make-book, book-title, and book-pages are all now functions that can be used.

Sample Problem define a new constant, function, and structure, different from the ones above.

Sample Problem Explain, in english, what the following function does: (define (2nd a b) b)

Stepping

(define TWO 2)
(define (f x y)
  (+ (* 3 x) (g y)))
(define (g x)
  (* x TWO))

Sample Problem In a comment, describe each step of how DrRacket would evaluate (f TWO 4). A step can be a plug-in, in which values replace variables, or arithmetic, in which case a pre-defined function evaluates its arguments. Describe what kind of step it is and the result of the step. For example:
(define (h z) (+ (string-length z) 2))
(h "bob")
; (h "bob")
; > plug-in
; (+ (string-length "bob") 2)
; > arithmetic
; (+ 3 2)
; > arithmetic
; 5
When running this code in the stepper, it clearly shows that these are the 3 steps that take place for (h "bob") is evaluated to a value. Check your work on this above exercise in the stepper, too.

Conditionals

There are four special kinds of forms which have special branching evaluation rules: cond, if, and, and or. We call them branching because they branch off into specific evaluations of their sub-expressions based on certain rules. We will discuss them in increasing difficulty.

(or e1 e2 e3 ...) evaluates to #true when any of e1, e2, or e3 return #t; otherwise, it will evaluate to #false. It is special because e2 will only be evaluated if e1 evaluates to #false; the same is true of e3 and e2.

Sample Problem
(define (my-or a b)
  (or a b))
(my-or (number? 5) (/ 5 0)) will error but (or (number? 5) (/ 5 0)) will not. Why is this? Explain it in english.

(and e1 e2 e3 ...) evaluates to #true when all of e1, e2, and e3 return #t; otherwise, it will evaluate to #false. It is special because e2 will only be evaluated if e1 evaluates to #true; the same is true of e3 and e2.

Sample Problem
(define (my-and a b)
  (and a b))
(my-and (number? "5") (/ 5 0)) will error but (and (number? "5") (/ 5 0)) will not. Why is this? Explain it in english.

(if test e-true e-false) evaluates to e-true when test evaluates to #t and e-false when test evaluates to #f.

Sample Problem Which of the following expressions will produce an error? Make a prediction and then try each one out to see if your prediction was correct.
(if (number? "5") (/ 5 0) 0)
(if (number? "5") 0 (/ 5 0))
(if (number? 5) (/ 5 0) 0)
(if (number? 5) 0 (/ 5 0))
(if 5 5 5)

cond is the last kind of special evaluation form.
(cond [question-1 answer-1]
      [question-2 answer-2]
      [question-3 answer-3]
      ...
      [else answer-else]) ; the else clause is OPTIONAL
The result of the cond will be the evaluation of answer-1 if question-1 evaluates to #t. If question-1 evaluates to #f, then question-2 will be evaluated, and so on and so forth. If there is an else clause at the end of the cond, then answer-else is evaluated and that is the result of the cond.

Sample Problem Explain in english why the following two functions are equivalent.
(define (funky n)
  (or (= 3 (modulo n 15))
      (not (= n 40))
      (positive? n)))
(define (funky n)
  (cond [(= 3 (modulo n 15)) #t]
        [(not (= n 40)) #t]
        [(positive? n) #t]
        [else #f]))

Sample Problem Similar to the above exercise, define a function whose body begins with a cond that is equivalent to funky2. Do not use if, or, and, or cond (beside the first usage).
(define (funky2 n)
  (and (= 3 (modulo n 15))
       (not (= n 40))
       (positive? n)))
Do not use any mathematical domain knowledge (so no >, <, negative?, etc.), but not will prove helpful.

But Wait, There’s More!

For more info on BSL please check out the docs or Intermezzo 1 in the textbook.