### BSL Practice

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.

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

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

(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

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

(cond [question-1 answer-1] [question-2 answer-2] [question-3 answer-3] ... [else answer-else]) ; the else clause is OPTIONAL

Sample Problem Explain in english why the following two functions are equivalent.

### But Wait, There’s More!

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