$30
Scheme uses the same recursive definition of lists as Prolog. What is called the head and tail in Prolog is called the car and cdr in Scheme. (These names come from the original Lisp implementation and stand for "Contents of Address Register" and "Contents of Data Register" referring to some Assembler Macros).
The main function to build lists is the cons function with makes a pair of a car and cdr.
The main function to get at the content of lists are the car and cdr.
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: Sum of Squares Digit
Consider the digits d_k,d_(k-1),…,d_1,d_0 of a positive integer number. The squares of the digits are then s = d_k^2 + d_(k-1)^2 + … + d_1^2 + d_0^2.
Create a function sosd that calculates the sum of square digits.
; The function calculates the sum of square digits.
;
; Example:
; (sosd 130)
; = 10
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Exercise 5: Every k-th element
; The function takes a list and an number selecting every kth
; element. Start counting at 1.
;
; Example:
; (drop '(a b c d e f g h i k) 3)
; = (a b d e g h k)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Exercise 5 and Quiz: List Manipulation
Define a function addSubList that processes a list of lists of numbers and adds up all sub-lists. The output of your function is a flat list of numbers. (You can assume that your function only receives valid input).
(define Q '(1 2 (3 4) 1 5 (7 8)))
;;;;;;;;;;;;;;;;;;;;;
; (addSubList Q)
; = '(1 2 7 1 5 15)
;;;;;;;;;;;;;;;;;;;