This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 2.2 Representing data with procedures | |
(define (make-point x y)(cons x y)) | |
(define (x-point point) (car point)) | |
(define (y-point point) (cdr point)) | |
(define a-point (make-point 0 0)) | |
(define b-point (make-point 2 2)) | |
(x-point a-point) | |
(y-point a-point) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 2.18 | |
(define (reverse list) | |
(define (iter-list reversed-list unprocessed-remaining) | |
(if (null? unprocessed-remaining) | |
reversed-list | |
(iter-list (cons(car unprocessed-remaining) reversed-list) (cdr unprocessed-remaining)) | |
) | |
) | |
(iter-list null list)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 2.20 | |
(define (same-parity first . remaining) | |
(define (iter-list parity-list unprocessed-remaining) | |
(if (null? unprocessed-remaining) | |
parity-list | |
(if (= (remainder first 2) (remainder (car unprocessed-remaining) 2)) | |
(iter-list (cons(car unprocessed-remaining) parity-list) (cdr unprocessed-remaining)) | |
(iter-list parity-list (cdr unprocessed-remaining))) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 2.21 | |
(define (square-list1 items) | |
(if (null? items) | |
null | |
(cons (* (car items) (car items)) (square-list1 (cdr items))) | |
) | |
) | |
(define (square-list2 items) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 2.26 | |
(define x (list 1 2 3)) | |
(define y (list 4 5 6)) | |
(append x y) ;---> (1 2 3 4 5 6) | |
(cons x y) ;---> ((1 2 3) 4 5 6) | |
(list x y) ;---> ((1 2 3) (4 5 6)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 2.20 | |
(define (same-parity first . remaining) | |
(define (iter-list parity-list unprocessed-remaining) | |
(if (null? unprocessed-remaining) | |
parity-list | |
(if (= (remainder first 2) (remainder (car unprocessed-remaining) 2)) | |
(iter-list (cons(car unprocessed-remaining) parity-list) (cdr unprocessed-remaining)) | |
(iter-list parity-list (cdr unprocessed-remaining))) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 2.27 | |
(define (deep-reverse list) | |
(define (iter-list reversed-list unprocessed-remaining) | |
(if (null? unprocessed-remaining) | |
reversed-list | |
(iter-list (cons (if (list? (car unprocessed-remaining)) | |
(deep-reverse (car unprocessed-remaining)) | |
(car unprocessed-remaining)) | |
reversed-list) (cdr unprocessed-remaining)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 2.2 Representing data with procedures | |
(define (make-point x y)(cons x y)) | |
(define (x-point point) (car point)) | |
(define (y-point point) (cdr point)) | |
(define a-point (make-point 0 0)) | |
(define b-point (make-point 2 2)) | |
(x-point a-point) | |
(y-point a-point) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 2.4: Representing data AS procedures | |
(define (2.4-cons x y) | |
(lambda (m) (m x y))) | |
(define (2.4-car z) | |
(z (lambda (p q) p))) | |
(define (2.4-cdr z) | |
(z (lambda (p q) q))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 2.5 | |
(define expt (** a b)) | |
(define (2.5-cons a b) | |
(* (expt 2 a) (expt 3 b)) | |
) | |
(define (2.5-car product) | |
(if (= 0 (remainder product 2)) | |
(+ 1 (2.5-car (/ product 2))) |