CSI 2120

Lab 10: Scheme

Prerequisites

This lab explores list which will be covered in detail this week. In order to complete the lab, you will need to know:

Exercise 1: Building Lists

Use cons to build the following lists:

		'(3 4)
		'(1 2 3)
		'(a (b c))
		'(1)
		'(2 (3 (4)))

Exercise 2: List Entries

Use car and cdr to obtain the elements from the list.


;  	Example:
;       (define L '(1 2 3 4 5))    
;
; 	(car L)
; 	=> 1
;
; 	(cdr L)
;       => '(2 3 4 5)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Combine calls car and cdr to get the element 2, 3, 4 and 5 from the list L (4 solutions).

;       (define L '(1 2 3 4 5))    
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Combine calls car and cdr to get the element 2 and 5 from the list LL (2 solutions).

;       (define LL '(1 (2 3 4) (5)))    
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Note that you can use shorthand commands such

;       (cadr '(1 2))
;       => 2
;       (car (cdr '(1 2)))
;       => 2    
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Exercise 3: Integer Range

Give a function that creates a list with integers in the specified range.

;       The function takes two indices, i and k, and produces the integers
;       between i and k including i and k.
;
; 	Example:
; 	(range 4 9)
; 	=> (4 5 6 7 8 9)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Exercise 4 and Quiz: List access

Please hand-in the answer to this question on Virtual Campus during your lab session but at the latest by Friday 6:00pm! Remember, your submission will only count if you have signed the lab attendance sheet.

Give a single function to access the element a in the list Q.

  (define Q '(1 2 (a 3 4 5)))
;;;;;;;;;;;;;;;;;;;;;
; (your_answer Q)      
; => 'a
;;;;;;;;;;;;;;;;;;;