Download Scheme Quick Reference Sheet - Handout | CS 4700 and more Study notes Programming Languages in PDF only on Docsity! CS 4700 Handouts - Scheme Quick Reference Sheet 1 Types Scheme has literals of several primitive types. The literals are self-evaluating expressions, which means that they evaluate to the value that they represent. 3 ; integer literal -3 ; integer literal 4.3 ; floating point literal 4.65e2 ; floating point literal, scientific notation #t ; boolean literal, true #f ; boolean literal, false "hello" ; string literal "hello\n" ; string literal, note the newline "I said \"hello\"" ; other backslash characters similar to C Scheme has one composite type, a list. Lists (and strings) are allocated in the heap and automatically garbage collected, just like in Perl. So donโt worry too much about the cost of creating new lists, use them freely, they are the primary data structure in Scheme. Note the โquoteโ symbol in front of each list, it is actually a function call (see below). โ(3 4) ; a two element list โ((4 3) 3) ; lists can be nested โ() ; empty list โ(()) ; a one element list, the element is the empty list โ("hi" 43 ("joe")) ; lists can be heterogeneous Note that the list elements are not separated by commas. Type checking in Scheme is dynamic. 2 Built-in Functions A function is a list that is evaluated. In general, a list will be evaluated, only in some contexts are lists not evaluated, such as when it is preceded by a โquoteโ. Each function is in prefix notation. (function-name operand-1 operand-2 ... operand-N) Each function returns a value, and takes zero or more operands. 2.1 Arithmetic Functions (max 34 5 7 38 6) ==> 38 ; maximum of the operands (min 3 5 5 330 4 -24) ==> -24 ; minimum of the operands (+ 3 4) ==> 7 ; addition (* 4 4) ==> 16 ; multiplication (- 3 4) ==> -1 ; subtraction 1 (- 3) ==> -3 ; unary subtraction (magnitude -7) ==> 7 ; absolute value (magnitude 7) ==> 7 ; absolute value (quotient 35 7) ==> 5 ; div (quotient -35 7) ==> -5 ; div (quotient 35 -7) ==> -5 ; div (quotient -35 -7) ==> 5 ; div (modulo 13 4) ==> 1 ; mod (modulo -13 4) ==> 3 ; mod (remainder 13 4) ==> 1 ; remainder (remainder -13 4) ==> -1 ; remainder (gcd 32 4) ==> 4 ; greatest common divisor 2.2 Comparison Functions (= 2 3) ==> #f ; are all the integer operands equal? (eqv? "abs" "abs") ==> #t ; are all the operands equal? (eqv? โ(a) โ(a)) ==> #t ; are all the operands equal? (< 2 3) ==> #t ; less than (> 2 3) ==> #f ; greater than (>= 2 3) ==> #f ; greater than or equal to (or #f #t) ==> #t ; or (not #f) ==> #t ; negation (and #f #t) ==> #f ; and (integer? 2) ==> #t ; are all the operands integers? (real? 3) ==> #f ; all reals? (string? 3) ==> #f ; all strings? (zero? 3 0) ==> #f ; all zero? (positive? 3 4 5) ==> #t ; all positive? (negative? -3 4 -5) ==> #f ; all negative? (string= "2" "3") ==> #f ; are two strings equal? 2.3 List Functions These are by far the most important functions that we will be using. (quote (a b)) ==> (a b) ; quote suspends evaluation of the list โ(a b) ==> (a b) ; quote suspends evaluation of the list (car โ(a b)) ==> a ; get head of list (contents of address reg) (car โ()) ==> ERROR: car: Wrong type in arg1 () ; expects a list! (car โ((a) b)) ==> (a) ; get head of list, could be a list! (cdr โ(a b)) ==> (b) ; get rest of list (contents of address reg) (cdr โ()) ==> ERROR: car: Wrong type in arg1 () ; expects a list! (car โ((a) a (b))) ==> (a (b)) ; get rest of list (cons 3 โ()) ==> (3) ; put element on head of list (cons โ(a) โ(b c d)) ==> ((a) b c d) (cons "a" โ(b c)) ==> ("a" b c) (list a 7 c) ==> (a 7 c) ; build a list from the operands (list) ==> () ; build a list from the operands (length โ(a b c)) ==> 3 ; what is the length of a list? (length โ(a (b) (c d e))) ==> 3 ; what is the length of a list? (length โ()) ==> 0 ; what is the length of a list? 2