Scheme Introduction Lab

As always an unreadable or unclear answer is a wrong answer.

Expressions

Enter the following expressions into the Scheme transcript window and write the results. If an expression is entered and nothing happens, check your parentheses!

1. (/ 10 (/ 10 (/ 10 2)))

2. (+ (* 3 12) 10)

3. (+ 2 4 6 8)

4. (define x 12)

5. (define y 20)

6. (* (- y x) (+ x y))

7. (define z (+ x y))

8. z

9. (define ans (+ z (* x 2)))

10. ans

11. (> 50 43)

12. (<= 3 53)

13. (define one 5)

14. (define two 10)

15. (= one 5)

16. (= two (* 2 one))

17. (define name 'george)

18. (eq? name 'george)

19. (eq? name name)

20. (eq? 'name 'george)

21. (and (= one 1) (eq? name 'george))

22. (or (= one 1) (eq? name 'george))

23. (not (= one 1))

Built-in Procedures

The following built-in procedures will identify different types and perform operations on those types. Write the results of each expression. (Note: Most questions will have several results, i.e. number (1) will have four results, so make it CLEAR which results go with each expression.)

1. (number? 5)

(string? 5)

(symbol? 5)

(boolean? 5)

2. (define str 5)

(number? str)

3. (string? str)

(string? 'str)

(string? "str")

4. (define num "this is a string")

(string? num)

5. (symbol? num)

(symbol? str)

(symbol? 'num)

(symbol? 'str)

6. (string-append "Laurie King")

7. (define first "Laurie")

(define second "King")

(string-append first second)

(define long (string-append first second))

long

8. (define false #t)

(boolean? false)

(boolean? #f)

9. (pair? '(1 2 43))

(list? '(1 2 43))

(pair? 21)

(define false '('yes "no" 12))

(pair? false)

10. (cons 'a '(b c))

(cons '(a) '(b c))

(cons 'a 'b)

(cons 'a (cons 'b '() ))

11. (eqv? (+ 4 5 6) (+ 6 5 4))

(eqv? 5 'five)

(eqv? (car '(a b c)) 'a)

12. (equal? (+ 3 2) 5)

(equal? '(1 2 3) '(1 (2 3)))

13. (null? '())

(null? '(1 2))

14. (car (cons 'a '(b c)))

(cdr (cons 'a '(b c)))

(cadr (cons 'a '(b c)))

15. (list? 'a) ;;(pair? ‘a)

16. (list? '(a b c)) ;;(pair? '(a b c))

Lists

A list data structure is central to Scheme as an abstract data type. A list is formed by enclosing any number of items within matching left and right parentheses. Write the resulting list for each expression shown below.

1. (define one (cons 'a (cons 'b (cons 'c '()))))

2. (define two (cons 'a (cons 'b 'c)))

3. one

4. two

5. (define rslt1 (car one))

6. (define rslt2 (car two))

7. (define rslt3 (caddr one))

8. (define rslt4 (caddr two)) ----Why error??

[Try (list? two) and (pair? two)]

9. (define lst (cons (cons 'a '()) (cons 'b (cons 'c '()))))

10. (define get-a (car (car (cdr '(b (a c) d)))))

11. (define get-b (car (cdaddr '(z (y) (x b) (a)))))

12. (define make-new (cons (car '(a b c)) (cdr '(1 2 3))))

13. (define ans1 (equal? (car '(a b)) (car (cdr '(b a)))))

14. (define ans2 (equal? (cons 'a '(b c))

(cons 'a (cons 'b (cons 'c '())))))

The If-Construct

There are two syntactically correct forms for the if-construct:

(if <logical-test> <true-action> <false-action>)

(if <logical-test> <true-action>)

Enter the following lists:

(define lst1 '(1 2 3))

(define lst2 '(10 20 30))

(define lst3 '(15 25 35))

If we want the largest list returned/displayed, what should be put in place of the (?????) in the following if-construct? (Rewrite the whole if construct in your answer as if you did not know how the lists above were defined.)

(define get-true (if (< (car lst1) (car lst2)) (?????) (?????)))

 

More Practice with Lists

Using the symbols one and two and the procedure cons, we can construct the list object

(one two) by typing (cons 'one (cons 'two '()))

We can create the list object

((one) two) by typing (cons (cons 'one '()) (cons 'two '()))

Using the symbols one, two, three, and four and the procedure cons, construct the following list objects.

(one (two) three four)

(one (two three four))

(one (two three) four)

((one two) (three four))

For each of the following lists, write the expression using car and cdr that extracts the symbol a. You can use abbreviations like cadr, caar, etc.

(b c a d)

((b a) (c d))

(((a)))

((d c) (a) b)

 

Cond

The most widely used LISP syntactic form for conditional execution is cond which has the following syntax:

(cond
(<logical-test1> <exp1> <exp2> ... <expn>)
(<logical-test2> <exp1> <exp2> ... <expn>)
...
(else <exp1> <exp2> ... <expn>))

Each clause is evaluated in turn until a clause is encountered with a true logical-test, or until the else clause is encountered. When the first true clause is encountered, each associated expression is evaluated in left-to-right order. The result of the last expression evaluation is returned as the result of the cond.

(You may use lists from the above list section to fill in the following cond-construct.)

(define get-smallest
(cond
((< (car lst1) (car lst2)) (car lst1))
((< (car lst2) (car lst3)) (car lst2))
(else (car lst3))))