CSE341 Section #6 Problems Summary of basic Scheme features: Types: numbers: integers (3, 802), reals (3.4), rationals (3/4), complex (2+3.4i) symbols: x, y, hello, r2d2 booleans: #t, #f strings: "hello", "how are you?" lists: (3 4 5) (98.5 "hello" (3 82.9) 73) Constructs: function call: (f arg1 arg2 arg3 ... arg-n) variable binding: (define sym expr) function binding: (define (f p1 p2 ... pn) expr) function binding with helper functions: (define (f p1 p2 ... pn) (define ...) (define ...) expr) let binding: (let ((sym1 e1) (sym2 e2) ... (sym-n en)) expr) let* binding: (let* ((sym1 e1) (sym2 e2) ... (sym-n en)) expr) if expression: (if test e1 e2) cond expression: (cond (test1 e1) (test2 e2) ... (test-n e-n)) (cond (test1 e1) (test2 e2) ... (else e-n)) Useful functions arithmetic: +, -, *, /, modulo, quotient, remainder mathematical: abs, sin, cos, max, min, expt (exponent), sqrt, floor, ceiling, truncate, round relational (for numbers): =, <, >, <=, >= equality (for other structures): eq?, eqv?, equal? logical: and, or, not type predicates: number? integer? symbol? boolean? string? list? higher-order: map, filter, foldl, foldr, sort list operations: length -- length of a list car -- first element of a list cdr -- rest of the list cons -- takes a value and a list and constructs a new list with the value at the front and the list after append -- joins lists together list -- forms a list from a sequence of values null? -- is something an empty list? pair? -- is something a nonempty list? 1. For each of the following definitions of a factorial function, identify the parenthesis error: (define (fact n) (if (= n 0) (1) (* n (fact (- n 1))))) (define (fact n) (if = n 0 1 (* n (fact (- n 1))))) (define fact (n) (if (= n 0) 1 (* n (fact (- n 1))))) (define (fact n) (if (= n 0) 1 (* n fact (- n 1)))) (define (fact n) (if (= n 0) 1 (* n ((fact) (- n 1))))) 2. How do you form a comment in Scheme? Is there a syntax for multi-line comments? How is the expression (/ a b c d) evaluated (i.e., left-to-right or right-to-left)? How would you compare to see if one string is less than another? 3. Define a function stutter that takes a list as an argument and that returns the list obtained by replacing every value in the list with two of that value. 4. Define a function sum that takes a list as an argument and that returns the sum of the numbers in the list. Don't worry about embedded numbers (numbers in inner lists). For example, (sum '(1 3.4 a b "hello" (2 3))) should return 4.4. 5. Define a function (multiples n m) that returns a list of the first n multiples of m. For example, (multiples 3 5) should return (5 10 15). 6. Define a function flatten that takes a list as an argument and that returns the list obtained by eliminating internal list structures. For example: (flatten '(1 2 a (b c (d e (f)) g) () () 13)) should return (1 2 a b c d e f g 13)