Cs234-2 - Lecture notes 2 PDF

Title Cs234-2 - Lecture notes 2
Course Data Types and Structures
Institution University of Waterloo
Pages 1
File Size 35.5 KB
File Type PDF
Total Downloads 44
Total Views 132

Summary

lec2...


Description

oolean Values Scheme represents Boolean values with the literals #t and #f (true and false are also usable in the Scheme teaching languages), representing true and false respectively. The equality function (= x y) ((= Num Num) -> Boolean) tests whether two numbers are equal and results in a boolean value. (< x y) and (>= x y) behave similarly. Predicates are expressions that result in Boolean values. They are, by convention, given names that end with a question mark. For example, (even? x) is clearly a predicate. The most common Boolean operators are (and x y ...), (or x y ...), and (not x). They represent x \wedge yx∧y, x \vee yx∨y, and \neg x¬x, respectively. Scheme has no inequality (not-equals) operator. However, it can be implemented as follows: (not (= x y)). Scheme uses short circuit evaluation. For and and or, if the result of the expression is known before the evaluation is complete, the rest is not evaluated: If or has a true argument, it knows that the result must be true regardless of other arguments - (or #t (/ 1 0)) will not give an error, since the division is never evaluated. If and has a false argument, it knows that the result must be false regardless of other arguments - (and #f (/ 1 0)) will not give an error, since the division is never evaluated. This is made possible by and and or being special forms. Many types have an equality predicate, like symbol=? and string=?, which should be used whenever possible. However, if the types of the operands are not known befrehand, (equal? x y ...) can be used to check that they are compatible types and that they have the same value. This does not work with inexact numbers. Strings Strings are denoted by double quotes: "CS135", "abc", "". The length of a string is determined with (string-length x). We determine if a value is a string with the predicate function (string? x). We concatenate strings using (string-append x y ...) String comparisons are done based on ASCII values. Symbols Symbols are denoted by a single quote: 'symbol. A symbol represents a particular idea. They are used to define a finite set of values, each one with a name. Symbols can only be compared, not manipulated like with strings. Write a predicate function that checks if the input is a valid multiple choice answer:...


Similar Free PDFs