-
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
Robert Campbell
committed
Feb 5, 2012
1 parent
b6f33ef
commit d3c4e11
Showing
2 changed files
with
87 additions
and
5 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
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,71 @@ | ||
(ns chatper2.section1 | ||
(:refer-clojure :exclude (cons)) | ||
(:use clojure.test)) | ||
|
||
;; Exercise 2.1 | ||
|
||
(defn gcd [a b] | ||
(if (zero? b) | ||
a | ||
(gcd b (rem a b)))) | ||
|
||
(defn make-rat [n d] | ||
(let [g (gcd n d) | ||
n (/ n g) | ||
d (/ d g)] | ||
(if (and (pos? n) (neg? d)) | ||
[(* n -1) (* d -1)] | ||
[n d]))) | ||
|
||
|
||
;; Exercise 2.2 | ||
|
||
(def make-segment vector) | ||
|
||
(def start-segment first) | ||
|
||
(def end-segment second) | ||
|
||
(def make-point vector) | ||
|
||
(def x-point first) | ||
|
||
(def y-point second) | ||
|
||
(defn midpoint-segment [[[x1 y1] [x2 y2]]] | ||
[(/ (+ x1 x2) 2) | ||
(/ (+ y1 y2) 2)]) | ||
|
||
|
||
;; Exercise 2.3 | ||
|
||
(def make-rect vector) | ||
|
||
(letfn [(width [[[x1 _] [x2 _]]] | ||
(Math/abs (- x1 x2))) | ||
(height [[[_ y1] [_ y2]]] | ||
(Math/abs (- y1 y2)))] | ||
|
||
(defn rect-perimeter [hypotenuse] | ||
(* 2 (+ (width hypotenuse) | ||
(height hypotenuse)))) | ||
|
||
(defn rect-area [hypotenuse] | ||
(* (width hypotenuse) | ||
(height hypotenuse)))) | ||
|
||
|
||
;; Exercise 2.4 | ||
|
||
(defn cons [x y] | ||
(fn [m] | ||
(m x y))) | ||
|
||
(defn car [z] | ||
(z (fn [p _] | ||
p))) | ||
|
||
(defn cdr [z] | ||
(z (fn [_ q] | ||
q))) | ||
|