-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
(define (make-segment start-point end-point) | ||
(cons start-point end-point)) | ||
|
||
(define (start-segment s) | ||
(car s)) | ||
|
||
(define (end-segment s) | ||
(cdr s)) | ||
|
||
(define (make-point x y) | ||
(cons x y)) | ||
|
||
(define (x-point p) | ||
(car p)) | ||
|
||
(define (y-point p) | ||
(cdr p)) | ||
|
||
(define (print-point p) | ||
(newline) | ||
(display "(") | ||
(display (x-point p)) | ||
(display ",") | ||
(display (y-point p)) | ||
(display ")")) | ||
|
||
(define (midpoint-segment s) | ||
(make-point | ||
(average (x-point (start-segment s)) | ||
(x-point (end-segment s))) | ||
(average (y-point (start-segment s)) | ||
(y-point (end-segment s))))) | ||
|
||
(define (average x y) | ||
(/ (+ x y) 2)) | ||
|
||
(print-point | ||
(midpoint-segment | ||
(make-segment (make-point 1 1) | ||
(make-point 10 20)))) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
(define (accumulate op initial sequence) | ||
(if (null? sequence) | ||
initial | ||
(op (car sequence) | ||
(accumulate op initial (cdr sequence))))) | ||
|
||
(define nil '()) | ||
|
||
(define (accu-map p sequence) | ||
(accumulate (lambda (x y) (cons (p x) y)) | ||
nil | ||
sequence)) | ||
|
||
(define (accu-append seq1 seq2) | ||
(accumulate cons seq2 seq1)) | ||
|
||
(define (accu-length sequence) | ||
(accumulate (lambda (x y) (+ 1 y)) 0 sequence)) |