#lang racket ;; If you run this file, it will produce a lot of values. Another way ;; to use it is to copy expressions you want to play with and then paste ;; them into an Interactions window. ;; THE PAIR ------------------------------------------------------------- (define a (cons 10 20)) (define b (cons 3 7)) ;; (car a) ;; (car b) ;; (cdr a) ;; (cdr b) (define c (cons a b)) ;; (car (car c)) ;; (car c) ;; c ;; (cdr (car c)) ;; (cdr c) ;; (cdr a) ;; (cdr (car a)) ;; (car (car a)) ;; SOME PRACTICE WITH cons ---------------------------------------------- (define x (cons 1 2)) (define y (cons x x)) (define z (cons y 4)) (define w (cons z (cons y x))) ;; LISTS ---------------------------------------------------------------- ;; '(1 #t 'eugene 2 #f 'wallingford) ;; ;; (cons 3 (cons 4 (cons 5 '()))) ;; why quote the innermost ()? ;; ;; (list 3 4 5) ;; INTERLUDE: QUOTATION ------------------------------------------------- ;; (list 2 4 6 5 2) ;; ;; (list (list 2 4 (list 6 5) 2) 4 7 (list 4 5) 9 2 (list 1)) ;; ;; (list (list 2 4 (list 6 (list 1 2 3 4) 5) 2) 4 7 ;; (list 4 (list 2 8) 5) 9 2 (list 1)) (define a-tree (list 3 4 3 1 2)) ;; (define a-tree (3 4 3 1 2)) (define another-tree (* 4 3 1 2)) ;; (define a-tree '((3 4) (3 (1 2)))) ;; (define another-tree '(* 3 (+ 1 2))) ;; MORE ON LISTS -------------------------------------------------------- (define 3-to-5 (list 3 4 5)) ;; (car 3-to-5) ;; (cdr 3-to-5) ;; (car (cdr 3-to-5)) ;; (car (cdr (cdr 3-to-5))) ;; (cons 2 3-to-5) ;; 3-to-5 ;; ;; (cons 3-to-5 5) ;; (cons (cdr 3-to-5) (car 3-to-5)) ;; (cons 3 4) ;; A FUNCTIONAL DATA STRUCTURE ------------------------------------------ (define list-1 '((2 4 (6 (1 2 3 4) 5) 2) 4 7 (4 (2 8) 5) 9 2 (1))) (define list-2 (cons 42 (rest list-1))) ;; list-2 ;; list-1 (define list-3 (cons (car list-1) (cons 42 (rest (rest list-1))))) ;; list-3 ;; list-1 ;; LISTS EXERCISE ------------------------------------------------------- ;; (list 1 2) ;; (cons 1 (list 2)) ;; (cons 1 (cons 2 '())) ;; (cons (list 3 4) (cons 3 (cons 4 (list 4 5)))) ;; VECTORS -------------------------------------------------------------- ;; ;; This section accompanies the short reading you are asked to do. ;; (vector 1 2 3) (define 1-to-3 #(1 2 3)) (define also-1-to-3 (vector 1 2 3)) (define another-1-to-3 #3(1 2 3)) ;; 1-to-3 ;; also-1-to-3 ;; another-1-to-3 ;; ;; (vector-ref 1-to-3 1) ;; (vector-ref 1-to-3 0) ;; ;; (vector-ref 1-to-3 3) ;; BONUS ON VOID -------------------------------------------------------- ;; (void) ;; (display (void)) ;; THE END --------------------------------------------------------------