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
(define (square-tree1 tree) | |
(map (lambda (sub-tree) | |
(if (pair? sub-tree) | |
(square-tree1 sub-tree) | |
(* sub-tree sub-tree))) | |
tree)) |
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
(define (square-tree0 tree) | |
(cond ((null? tree) null) | |
((pair? tree) | |
(cons (square-tree0 (car tree)) | |
(square-tree0 (cdr tree)))) | |
(else (* tree tree)))) |
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
(define (fringe items) | |
(if (pair? items) | |
(apply append (map fringe items)) | |
(list 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
(define (deep-reverse items) | |
(if (pair? items) | |
(map deep-reverse (reverse items)) | |
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
(define (foreach proc items) | |
(define (iter i result) | |
(if (null? i) | |
result | |
(iter (cdr i) (cons result (proc (car i)))))) | |
(iter items null)) |
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
(define (square x) (* x x)) | |
(define (square-list items) | |
(if (null? items) | |
null | |
(cons (square (car items)) | |
(square-list (cdr items))))) | |
(define (square-list-map items) | |
(map square 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
(define (same-parity x . y) | |
(define (same-parity? a b) | |
(or (and (even? a) (even? b)) | |
(and (odd? a) (odd? b)))) | |
(define (rec y) | |
(cond ((null? y) null) | |
((same-parity? x (car y)) (cons (car y) (rec (cdr y)))) | |
(else (rec (cdr y))))) | |
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
(define (no-more? coin-values) (null? coin-values)) | |
(define (first-denomination coin-values) (car coin-values)) | |
(define (except-first-denomination coin-values) (cdr coin-values)) | |
(define (cc amount coin-values) | |
(cond ((= amount 0) 1) | |
((or (< amount 0) (no-more? coin-values)) 0) | |
(else | |
(+ (cc amount | |
(except-first-denomination coin-values)) |
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
;;; recursion | |
(define (reverse items) | |
(if (null? items) | |
null | |
(cons (reverse (cdr items)) (car items)))) | |
;;; iteration | |
(define (reverse-iter items) | |
(define (iter i result) | |
(if (null? i) |
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
;;; Assuming at least 1 element in items | |
(define (last-pair items) | |
(if (null? (cdr items)) | |
(car items) | |
(last-pair (cdr items)))) | |
;;; Assuming at least 1 element in items | |
(define (last-pair-iter items) | |
(define (iter i result) | |
(if (null? i) |