; Scheme and the Art of Programming
; Chapter 1

; -- Exercise 1.1
; Bring up Scheme on the computer and record this session in a file called "session1.rec".

15
; Value: 15

-200
; Value: -200

123456789012234
; Value: 123456789012234

(quote alphabet-soup)
; Value: alphabet-soup

'alphabet-soup
; Value: alphabet-soup

''alphabet-soup
; Value: (quote alphabet-soup)


; -- Exercise 1.2
; What values are returned when the following are entered 
; in response to the prompt?

(define big-number 10500900)
(define small-number 0.00000025)
(define cheshire 'cat)
(define number1 big-number)
(define number2 'big-number)

big-number
; Value: 10500900

small-number
; Value: 2.5e-07

'big-number
; Value: big-number

cheshire
; Value: cat

'cheshire
; Value: cheshire

number1
; Value: 10500900

number2
; Value: big-number

'number1
; Value: number1


; -- Exercise 1.3
; What is the result of entering eah of the following expressions 
; in response to the Scheme prompt?

(- 10 (- 8 (- 6 4)))
; Value: 4

(/ 40 (* 5 20))
; Value: 2/5

(/ 2 3)
; Value: 2/3

(+ (* 0.1 20) (/ 4 -3))
; Value: 0.6666666666666666



; -- Exercise 1.4
; Write the Scheme expressions that denote the same calculation 
; as the following arithmetic expressions.

; a. (4 x 7) - (13 + 5)
(- (* 4 7) (+ 13 5))

; b. (3 x (4 + (-5 - -3)))
(* 3 (+ 4 (- -5 -3)))

; c. (2.5 / (5 x (1 / 10)))
(/ 2.5 (* 5 (/ 1 10)))

; d. 5 x ((537 x (98.3 + (375 - (2.5 x 153)))) + 255)
(* 5 (+ (* 537 (+ 98.3 (- 375 (* 2.5 153)))) 255))



; -- Exercise 1.5
; Translate each of the following Scheme expresssions ino the 
; usual arithmetic expressions.

; a. (+ a (- (+ B g) a))
; a + ((B + g) - a)

; b. (+ (* a B) (* g B))
; a * B + g * B

; c. (/ (- a B) (- a g))
; (a - B) / (a - g)



; -- Exercise 1.6
; Using the quoted symbols one,two,three,and four and procedure 
; cons, construct the following lists.

(cons 'one (cons 'two (cons 'three (cons 'four '()))))
; Value: (one two three four)

(cons 'one (cons (cons 'two (cons 'three (cons 'four '()))) '()))
; Value: (one (two three four))

(cons 'one (cons (cons 'two (cons 'three '())) (cons 'four '())))
; Value: (one (two three) four)

(cons (cons 'one (cons 'two '())) (cons (cons 'three (cons 'four '())) '()))
; Value: ((one two) (three four))

(cons (cons (cons 'one '()) '()) '())
; Value: (((one)))



; -- Exercise 1.7
; Consider a list ls containing n values. If a evaluates to any value, 
; how many values does the list obtained by evaluating (cons a ls) contain?

; cons adds a single value (even if that item is itself a list) to the list ls
; therefore  n + 1 items



; -- Exercise 1.8
; What is the result of evaluating '(a 'b)? Explain.

'(a 'b)
; Value: (a (quote b))

; '(a 'b) = ('a ''b) = (a (quote b))



; -- Exercise 1.9
; If a and b evaluate to any values
(define a 5)
(define b 9)

(car (cons a b))
; Value: 5

(cdr (cons a b))
; Value: 9



; -- Exercise 1.10
; What is

(symbol? (cons a b))
; Value: #f

(pair? (cons a b))
; Value: #t

(null? (cons a b))
; Value: #f

(null? (cdr (cons a '())))
; Value: #t



; -- Exercise 1.11
; If a list ls contains only one item, what is (null? (cdr ls))?

; A list is a cons cell, and a singleton list is equivalent to (cons ITEM '())
; The cdr of that cons cell is null, and so (null? (cdr ls)) is #t



; -- Exercise 1.12
; Evaluate each of the following:

; a.
(cdr '((a (b c) d)))
; Value: ()

; b.
(car (cdr (cdr '(a (b c) (d e)))))
; Value: (d e)

; c.
(car (cdr '((1 2) (3 4) (5 6))))
; Value: (3 4)

; d.
(cdr (car '((1 2) (3 4) (5 6))))
; Value: (2)

; e.
(car (cdr (car '((car dog hen)))))
; Value: dog

; f.
(cadr '(a b c d))
; Value: b

; g.
(cadar '((a b) (c d) (e f)))
; Value: b



; -- Exercise 1.13
; For each of the following lists, write the expression using car and 
; cdr that extracts symbol a:

; a. (b c a d)
(car (cdr (cdr '(b c a d))))
; (cdr '(b c a d)) => (c a d)
; (cdr (cdr '(b c a d))) => (a d)
; (car (cdr (cdr '(b c a d)))) => a

; b. ((b a) (c d))
(car (cdr (car '((b a) (c d)))))
; (car '((b a) (c d))) => (b a)
; (cdr (car '((b a) (c d)))) => (a)
; (car (cdr (car '((b a) (c d))))) => a

; c. ((d c) (a) b)
(car (car (cdr '((d c) (a) b))))
; (cdr '((d c) (a) b)) => ((a) b)
; (car (cdr '((d c) (a) b))) => (a)
; (car (car (cdr '((d c) (a) b)))) => a

; d. (((a)))
(car (car (car '(((a))))))
; (car '(((a)))) => ((a))
; (car (car '(((a))))) => (a)
; (car (car (car '(((a)))))) => a



; -- Exercise 1.14
; Decide whether the following expressions are true or false:

; a.
(symbol? (car '(cat mouse)))
; Value: #t

; b.
(symbol? (cdr '((cat mouse))))
; Value: #f

; c.
(symbol? (cdr '(cat mouse)))
; Value: #f

; d.
(pair? (cons 'hound '(dog)))
; Value: #t

; e.
(pair? (car '(cheshire cat)))
; Value: #f

; f.
(pair? (cons '() ()))
; Value: #t



; -- Exercise 1.15
; Decide whether the following expressions are true or false:

(eqv? (car '(a b)) (car (cdr '(b a))))
; Value: #t

(eqv? 'flea (car (cdr '(dog flea))))
; Value: #t

(eq? (cons 'a '(b c)) (cons 'a '(b c)))
; Value: #f

(eqv? (cons 'a '(b c)) (cons 'a '(b c)))
; Value: #f

(equal? (cons 'a '(b c)) (cons 'a '(b c)))
; Value: #t

(null? (cdr (cdr '((a b c) d))))
; Value: #t

(null? (car '(())))
; Value: #t

(null? (car '((()))))
; Value: #f
