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 (pascal row col) | |
(cond ((= col 0) 1) | |
((= col row) 1) | |
((< row col) 0) | |
(else (+ (pascal (- row 1) (- col 1)) | |
(pascal (- row 1) col))))) |
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 a . b) | |
(let ((test (if (even? a) | |
even? | |
odd?))) | |
(cons a (parity a test b)))) | |
(define (parity a test b) | |
(cond ((and (not (null? b))(test (car b))) (cons (car b) (parity a test (cdr b) ))) | |
((null? b) '()) | |
(else (parity a test (cdr b) )))) |
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 (last-pair list1) | |
(if (= (length list1) 1) | |
(car list1) | |
(last-pair (cdr list1)))) | |
(define (but-last list1) | |
(if (= (length list1) 1) | |
'() | |
(cons (car list1) (but-last (cdr list1))))) | |
(define (reverse x) | |
(if (null? x) |
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 (last-pair list1) | |
(if (= (length list1) 1) | |
(car list1) | |
(last-pair (cdr list1)))) | |
(define (but-last list1) | |
(if (= (length list1) 1) | |
'() | |
(cons (car list1) (but-last (cdr list1))))) | |
(define (reverse x) | |
(if (null? x) |
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 x) | |
(cond ((null? x) x) | |
((not (pair? x)) (list x)) | |
(list (append (fringe (car x)) (fringe (cdr x)))))) |
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 (make-mobile left right) | |
(list left right)) | |
(define (make-branch length structure) | |
(list length structure)) | |
(define (left-branch x) | |
(car x)) | |
(define (right-branch x) | |
(cadr x)) | |
(define (branch-length x) | |
(car x)) |
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 (filter predicate sequence) | |
(cond ((null? sequence) '()) | |
((predicate (car sequence)) | |
(cons (car sequence) | |
(filter predicate (cdr sequence)))) | |
(else (filter predicate (cdr sequence))))) | |
(define (accumulate op initial sequence) | |
(if (null? sequence) | |
initial |
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 (tree-map proc tree) | |
(cond ((null? tree) '()) | |
((not (pair? tree)) (proc tree)) | |
(else (cons (tree-map proc (car tree)) | |
(tree-map proc (cdr 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 (subsets s) | |
(if (null? s) | |
(list '()) | |
(let ((rest (subsets (cdr s)))) | |
(append rest (map (lambda (x) (cons (car s) x)) rest))))) |
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 (map p sequence) | |
(accumulate (lambda (x y) (cons (p x) y)) '() sequence)) | |
(define (append seq1 seq2) | |
(accumulate cons seq2 seq1)) | |
;; it took me about half an hour to figure length out. | |
(define (length sequence) | |
(accumulate (lambda (x y) (+ 1 y)) 0 sequence)) |