Lab Check-in 6: Scheme

Check-in questions and answers will be available after lab solutions are released.

Scheme-ing

Q1: List vs Cons

Encourage students to draw out the box-and-pointer diagrams for these if they get stuck or the wrong answer.

scm> (cons 1 (cons 2 (cons 3 nil)))
______
(1 2 3)
scm> (list 1 (list 2 (list 3)))
______
(1 (2 (3)))
scm> (list 1 (cons 2 (cons 3 nil)))
______
(1 (2 3))
scm> (list 1 (cons 2 3))
______
(1 (2 . 3))
scm> (cons 1 (list 2 3))
______
(1 2 3)
scm> (cons (list 1) (list 2 3 4))
______
((1) 2 3 4)

Q2: To Parentheses or Not To Parentheses

Notes for Academic Interns: Ask the student the following:

  • What will be displayed as a result of evaluating ((+ 1 2))? Why is this the case?

Solution: * Error: Cannot call 3 as it's not a procedure will be displayed. We end up trying to evaluate (3), which the interpreter reads as "Let's call the operator 3 on 0 arguments." However, 3 is not an operator, so this fails.
In general parentheses should go:
* Around procedure calls
* Around special forms
* Around procedure names and parameters in define special forms
* Around arguments in lambdas (on top of the parentheses surrounding the lambda)
* For example, (lambda (x) (+x 5)) but for define we would do (define (f x) (+ x 5)) The lambda argument has an extra set of parentheses around it.
* Basically we put parentheses around stuff whenever we are performing "actions": applying an operator to some operands