Skip to content

Commit

Permalink
Book 2.5.1 Add generic arithmetic operations and tests (#154)
Browse files Browse the repository at this point in the history
Introduced a new set of generic arithmetic operations including addition, subtraction, multiplication, and division providing support for different number types such as Scheme numbers and rational numbers. Corresponding unit tests were also added to ensure the proper functioning of these operations.
  • Loading branch information
SmetDenis authored Feb 4, 2024
1 parent ba8af8c commit 02f025c
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 5 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ Jay Sussman on basics of computer science and software engineering.
* [2.3.2](https://sarabander.github.io/sicp/html/2_002e3.xhtml#g_t2_002e3_002e2) Example: Symbolic Differentiation - [2.56](src/sicp/chapter_2/part_3/ex_2_56.clj), [2.57](src/sicp/chapter_2/part_3/ex_2_57.clj), [2.58](src/sicp/chapter_2/part_3/ex_2_58.clj)
* [2.3.3](https://sarabander.github.io/sicp/html/2_002e3.xhtml#g_t2_002e3_002e3) Example: Representing Sets - [2.59](src/sicp/chapter_2/part_3/ex_2_59.clj), [2.60](src/sicp/chapter_2/part_3/ex_2_60.clj), [2.61](src/sicp/chapter_2/part_3/ex_2_61.clj), [2.62](src/sicp/chapter_2/part_3/ex_2_62.clj), [2.63](src/sicp/chapter_2/part_3/ex_2_63.clj), [2.64](src/sicp/chapter_2/part_3/ex_2_64.clj), [2.65](src/sicp/chapter_2/part_3/ex_2_65.clj), [2.66](src/sicp/chapter_2/part_3/ex_2_66.clj)
* [2.3.4](https://sarabander.github.io/sicp/html/2_002e3.xhtml#g_t2_002e3_002e4) Example: Huffman Encoding Trees - [2.67](src/sicp/chapter_2/part_3/ex_2_67.clj), [2.68](src/sicp/chapter_2/part_3/ex_2_68.clj), [2.69](src/sicp/chapter_2/part_3/ex_2_69.clj), [2.70](src/sicp/chapter_2/part_3/ex_2_70.clj), [2.71](src/sicp/chapter_2/part_3/ex_2_71.clj), [2.72](src/sicp/chapter_2/part_3/ex_2_72.clj)
* [2.4](https://sarabander.github.io/sicp/html/2_002e4.xhtml#g_t2_002e4) Multiple Representations for Abstract Data
* [2.4](https://sarabander.github.io/sicp/html/2_002e4.xhtml#g_t2_002e4) Multiple Representations for Abstract Data - [Code in book](src/sicp/chapter_2/part_4/book_2_4.clj)
* [2.4.1](https://sarabander.github.io/sicp/html/2_002e4.xhtml#g_t2_002e4_002e1) Representations for Complex Numbers
* [2.4.2](https://sarabander.github.io/sicp/html/2_002e4.xhtml#g_t2_002e4_002e2) Tagged data
* [2.4.3](https://sarabander.github.io/sicp/html/2_002e4.xhtml#g_t2_002e4_002e3) Data-Directed Programming and Additivity - [2.73](src/sicp/chapter_2/part_4/ex_2_73.clj), [2.74](src/sicp/chapter_2/part_4/ex_2_74.clj), [2.75](src/sicp/chapter_2/part_4/ex_2_75.clj), [2.76](src/sicp/chapter_2/part_4/ex_2_76.clj)
* 2.5 Systems with Generic Operations
* 2.5.1 Generic Arithmetic Operations
* 2.5.2 Combining Data of Different Types
* 2.5.3 Example: Symbolic Algebra
* [2.5](https://sarabander.github.io/sicp/html/2_002e5.xhtml#g_t2_002e5) Systems with Generic Operations - [Code in book](src/sicp/chapter_2/part_5/book_2_5.clj)
* [2.5.1](https://sarabander.github.io/sicp/html/2_002e5.xhtml#g_t2_002e5_002e1) Generic Arithmetic Operations
* [2.5.2](https://sarabander.github.io/sicp/html/2_002e5.xhtml#g_t2_002e5_002e2) Combining Data of Different Types
* [2.5.3](https://sarabander.github.io/sicp/html/2_002e5.xhtml#g_t2_002e5_002e3) Example: Symbolic Algebra

### Chapter 3 - Modularity, Objects, and State

Expand Down
110 changes: 110 additions & 0 deletions src/sicp/chapter_2/part_5/book_2_5.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
(ns sicp.chapter-2.part-5.book-2-5
(:require [sicp.chapter-2.part-4.book-2-4 :as b24]
[sicp.misc :as m]))

(comment "2.5")
; Systems with Generic Operations ------------------------------------------------------------------

(comment "2.5.1")
; Generic Arithmetic Operations --------------------------------------------------------------------

; Custom helper functions. Made myself, just for testing -------------------------------------------

(def operation-table (atom {}))

(defn attach-tag [tag value]
{:tag tag, :contents value})

(defn put-op [operation types procedure]
(swap! operation-table assoc-in [operation types] procedure))

(defn get-op [operation types]
(get-in @operation-table [operation types]))

(defn apply-generic [op & args]
(let [type-tags (map #(-> % :tag) args)
operation (get-op op type-tags)]
(if operation
(apply operation (map #(:contents %) args))
(throw (IllegalArgumentException. (str "No method for: " op " " type-tags))))))

; --------------------------------------------------------------------------------------------------

(defn add [x y] (apply-generic :add x y))
(defn sub [x y] (apply-generic :sub x y))
(defn mul [x y] (apply-generic :mul x y))
(defn div [x y] (apply-generic :div x y))

(defn install-scheme-number-package []
(letfn [(tag [x] (attach-tag :scheme-number x))]
(put-op :add [:scheme-number :scheme-number] (fn [x y] (tag (+ x y))))
(put-op :sub [:scheme-number :scheme-number] (fn [x y] (tag (- x y))))
(put-op :mul [:scheme-number :scheme-number] (fn [x y] (tag (* x y))))
(put-op :div [:scheme-number :scheme-number] (fn [x y] (tag (/ x y))))
(put-op :make :scheme-number (fn [x] (tag x)))
:done))

(defn install-rational-package []
; Internal procedures
(letfn [(numer [x] (first x))
(denom [x] (second x))
(make-rat [n d] (let [g (m/gcd n d)]
(m/pair (/ n g) (/ d g))))
(add-rat [x y] (make-rat (+ (* (numer x) (denom y))
(* (numer y) (denom x)))
(* (denom x) (denom y))))
(sub-rat [x y] (make-rat (- (* (numer x) (denom y))
(* (numer y) (denom x)))
(* (denom x) (denom y))))
(mul-rat [x y] (make-rat (* (numer x) (numer y))
(* (denom x) (denom y))))
(div-rat [x y] (make-rat (* (numer x) (denom y))
(* (denom x) (numer y))))
; Interface to rest of the system
(tag [x] (attach-tag :rational x))]
(put-op :add [:rational :rational] (fn [x y] (tag (add-rat x y))))
(put-op :sub [:rational :rational] (fn [x y] (tag (sub-rat x y))))
(put-op :mul [:rational :rational] (fn [x y] (tag (mul-rat x y))))
(put-op :div [:rational :rational] (fn [x y] (tag (div-rat x y))))
(put-op :make :rational (fn [n d] (tag (make-rat n d))))
:done))

(defn install-complex-package []
; Imported procedures from rectangular and polar packages
(letfn [(make-from-real-imag [x y] ((get-op :make-from-real-imag :rectangular) x y))
(make-from-mag-ang [r a] ((get-op :make-from-mag-ang :polar) r a))
; Internal procedures
(add-complex [z1 z2] (make-from-real-imag
(+ (b24/real-part z1) (b24/real-part z2))
(+ (b24/imag-part z1) (b24/imag-part z2))))
(sub-complex [z1 z2] (make-from-real-imag
(- (b24/real-part z1) (b24/real-part z2))
(- (b24/imag-part z1) (b24/imag-part z2))))
(mul-complex [z1 z2] (make-from-mag-ang
(* (b24/magnitude z1) (b24/magnitude z2))
(+ (b24/angle z1) (b24/angle z2))))
(div-complex [z1 z2] (make-from-mag-ang
(/ (b24/magnitude z1) (b24/magnitude z2))
(- (b24/angle z1) (b24/angle z2))))
; Interface to rest of the system
(tag [z] (attach-tag :complex z))]
; Putting functions in a map or registry
(put-op :add [:complex :complex] (fn [z1 z2] (tag (add-complex z1 z2))))
(put-op :sub [:complex :complex] (fn [z1 z2] (tag (sub-complex z1 z2))))
(put-op :mul [:complex :complex] (fn [z1 z2] (tag (mul-complex z1 z2))))
(put-op :div [:complex :complex] (fn [z1 z2] (tag (div-complex z1 z2))))
(put-op :make-from-real-imag :complex (fn [x y] (tag (make-from-real-imag x y))))
(put-op :make-from-mag-ang :complex (fn [r a] (tag (make-from-mag-ang r a))))
:done))

(defn make-scheme-number [n]
((get-op :make :scheme-number) n))

(defn make-rational [n d]
((get-op :make :rational) n d))

(defn make-complex-from-real-imag [x y]
((get-op :make-from-real-imag :complex) x y))

(defn make-complex-from-mag-ang [r a]
((get-op :make-from-mag-ang :complex) r a))
37 changes: 37 additions & 0 deletions test/sicp/chapter_2/part_5/book_2_5_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
(ns sicp.chapter-2.part-5.book-2-5-test
(:require [clojure.test :refer [deftest is]]
[sicp.chapter-2.part-5.book-2-5 :refer [add
div
install-rational-package
install-scheme-number-package
make-rational
make-scheme-number
mul
sub]]))

(install-scheme-number-package)
(install-rational-package)

(deftest add-test
(is (= (make-scheme-number 3) (add (make-scheme-number 1) (make-scheme-number 2))))
(is (= (make-scheme-number 4) (add (make-scheme-number 2) (make-scheme-number 2))))
(is (= (make-rational 4 1) (add (make-rational 2 1) (make-rational 2 1))))
(is (= (make-rational 1 1) (add (make-rational 2 3) (make-rational 1 3)))))

(deftest sub-test
(is (= (make-scheme-number 1) (sub (make-scheme-number 4) (make-scheme-number 3))))
(is (= (make-scheme-number -6) (sub (make-scheme-number 4) (make-scheme-number 10))))
(is (= (make-rational 0 1) (sub (make-rational 2 1) (make-rational 2 1))))
(is (= (make-rational 1 3) (sub (make-rational 2 3) (make-rational 1 3)))))

(deftest mul-test
(is (= (make-scheme-number 12) (mul (make-scheme-number 4) (make-scheme-number 3))))
(is (= (make-scheme-number -40) (mul (make-scheme-number -4) (make-scheme-number 10))))
(is (= (make-rational 4 1) (mul (make-rational 2 1) (make-rational 2 1))))
(is (= (make-rational 2 9) (mul (make-rational 2 3) (make-rational 1 3)))))

(deftest div-test
(is (= (make-scheme-number 20) (div (make-scheme-number 100) (make-scheme-number 5))))
(is (= (make-scheme-number 1/2) (div (make-scheme-number 3) (make-scheme-number 6))))
(is (= (make-rational 1 1) (div (make-rational 2 1) (make-rational 2 1))))
(is (= (make-rational 2 1) (div (make-rational 2 3) (make-rational 1 3)))))

0 comments on commit 02f025c

Please sign in to comment.