Chapter 1 - Toys

Chapter 1 - Toys#

1996 Friedman, Daniel P. & Matthias Felleisen. The Little Schemer. 4e. MIT Press.


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Chapter 1 - Toys

; Does (atom? '()) return #f? If not, define atom?.
(define atom?
  (lambda (x)
    (and (not (pair? x)) (not (null? x)))))

(atom? 'atom)                                 ; #t - a string of characters
(atom? 'turkey)                               ; #t - a string of characters
(atom? '1492)                                 ; #t - a string of digits
(atom? 'u)                                    ; #t - a string of one or more characters
(atom? '*abc$)                                ; #t - a string of characters not beginning with ( or )
(list? '(atom))                               ; #t - an              atom  enclosed by parentheses
(list? '(atom turkey or))                     ; #t - a collection of atoms enclosed by parentheses
;(list? '(atom turkey) or)                    ; #f - 2 S-expressions not enclosed by parentheses
(list? '((atom turkey) or))                   ; #t - 2 S-expressions     enclosed by parentheses
;(sexp? 'xyz)                                 ; #t - all atoms are S-expressions
;(sexp? '(x y z))                             ; #t - all lists are S-expressions
;(sexp? '((x y) z))                           ; #t - all lists are S-expressions
(list? '(how are you doing so far))           ; #t - 6 S-expressions
(list? '(((how) are) ((you) (doing so)) far)) ; #t - 3 S-expressions
                                              ;        ((how) are)
                                              ;        ((you) (doing so))
                                              ;        far
(list? '())                                   ; #t - the empty/null list
(atom? '())                                   ; #f - a list is not an atom
(list? '(() () () ()))                        ; #t - a collection of S-expressions enclosed by parentheses
(car '(a b c))                                ; a
(car '((a b c) x y z))                        ; (a b c)
;(car 'hotdog)                                ; no answer - you cannot ask for the car of an atom
;(car '())                                    ; no answer - you cannot ask for the car of the empty/null list
(car '(((hotdogs)) (and) (pickle) relish))    ; ((hotdogs)) "the list of the list of hotdogs"
(car (car '(((hotdogs)) (and))))              ; (hotdogs)
(cdr '(a b c))                                ; (b c)
(cdr '((a b c) x y z))                        ; (x y z)
(cdr '(hamburger))                            ; ()
(cdr '((x) t r))                              ; (t r)
;(cdr 'hotdogs)                               ; no answer - you cannot ask for the cdr of an atom
;(cdr '())                                    ; no answer - you cannot ask for the cdr of the empty/null list
(car (cdr '((b) (x y) ((c)))))                ; (x y)
(cdr (cdr '((b) (x y) ((c)))))                ; (((c)))
;(cdr (car '(a (b (c)) d)))                   ; no answer - you cannot ask for the cdr of an atom
(cons 'peanut '(butter and jelly))            ; (peanut butter and jelly)
(cons '(banana and) '(peanut butter and jelly))    ; ((banana and) peanut butter and jelly)
(cons '((help) this) '(is very ((hard) to learn))) ; (((help) this) is very ((hard) to learn))
(cons '(a b (c)) '())                              ; ((a b (c)))
(cons 'a '())                                      ; (a)
(cons '((a b c)) 'b)                               ; no answer - the second argument to cons must be a list
                                                   ;   in practice, (((a b c)) . b) - a non-list pair whose car is ((a b c)) and cdr is b
(cons 'a 'b)                                       ; no answer - the second argument to cons must be a list
                                                   ;   in practice, (a . b) - a non-list pair whose car is a and cdr is b
(cons 'a (car '((b) c d)))                         ; (a b)
(cons 'a (cdr '((b) c d)))                         ; (a c d)
(null? '())                                        ; #t
(null? '(a b c))                                   ; #f
(null? 'spaghetti)                                 ; no answer - you cannot ask null? of an atom
                                                   ;   in practice, #f
(atom? 'Harry)                                     ; #t
(atom? '(Harry had a heap of apples))              ; #f
(atom? (car '(Harry had a heap of apples)))        ; #t
(atom? (cdr '(Harry had a heap of apples)))        ; #f
(atom? (cdr '(Harry)))                             ; #f - the empty/null list is not an atom
(atom? (car (cdr '(swing low sweet cherry oat))))  ; #t
(atom? (car (cdr '(swing (low sweet) cherry oat)))); #f
(eq? 'Harry 'Harry)                                ; #t
(eq? 'margarine 'butter)                           ; #f
(eq? '() '(strawberry))                            ; no answer - the two arguments to eq? must be non-numeric atoms
                                                   ;   in practice, #f
(eq? 6 7)                                          ; no answer - the two arguments to eq? must be non-numeric atoms
                                                   ;   in practice, #f
(eq? (car '(Mary had a little lamb chop)) 'Mary)   ; #t
(eq? (cdr '(soured milk)) 'milk)                   ; no answer - the two arguments to eq? must be non-numeric atoms
                                                   ;   in practice, #f
(eq? (car '(beans beans we need jelly beans)) (car (cdr '(beans beans we need jelly beans)))) ; #t