Skip to content

Commit

Permalink
Exercise 2.77 and refactor parts of SICP chapter 2's code (#166)
Browse files Browse the repository at this point in the history
* Update progress and add exercise 2.77 in README

The number of completed exercises was updated in the progress bar of the README, demonstrating more tasks have been accomplished. Additionally, code related to exercise 2.77 has been added, which covers generic arithmetic operations.

* Refactor parts of SICP chapter 2's code

The code refactor focused on the parts related to packages in chapter 2 of SICP. It included separating different packages into their own files, implementing a generic operation system, and standardizing the function interface. This has resulted in cleaner, more maintainable, and more understandable code.
  • Loading branch information
SmetDenis authored Feb 18, 2024
1 parent 03626ed commit 550f8d7
Show file tree
Hide file tree
Showing 13 changed files with 279 additions and 166 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Clojure CI](https://github.com/SmetDenis/Clojure-Sicp/actions/workflows/main.yml/badge.svg?branch=main)](https://github.com/SmetDenis/Clojure-Sicp/actions/workflows/main.yml)
[![Coverage Status](https://coveralls.io/repos/github/SmetDenis/Clojure-SICP/badge.svg?branch=main)](https://coveralls.io/github/SmetDenis/Clojure-SICP?branch=main)
![Progress](https://progress-bar.dev/122/?scale=356&title=Solved&width=500&suffix=)
![Progress](https://progress-bar.dev/123/?scale=356&title=Solved&width=500&suffix=)

SICP (Structure and Interpretation of Computer Programs) is the book of Harold Abelson and Gerald
Jay Sussman on basics of computer science and software engineering.
Expand Down Expand Up @@ -43,7 +43,7 @@ Jay Sussman on basics of computer science and software engineering.

### Chapter 2 - Building Abstractions with Data

![Progress](https://progress-bar.dev/76/?scale=97&title=Solved&width=500&suffix=)
![Progress](https://progress-bar.dev/77/?scale=97&title=Solved&width=500&suffix=)

* [2.1](https://sarabander.github.io/sicp/html/Chapter-2.xhtml#Chapter-2) Introduction to Data Abstraction - [Code in book](src/sicp/chapter_2/part_1/book_2_1.clj)
* [2.1.1](https://sarabander.github.io/sicp/html/2_002e1.xhtml#g_t2_002e1_002e1) Example: Arithmetic Operations for Rational Numbers - [2.1](src/sicp/chapter_2/part_1/ex_2_01.clj)
Expand All @@ -65,7 +65,7 @@ Jay Sussman on basics of computer science and software engineering.
* [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](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.1](https://sarabander.github.io/sicp/html/2_002e5.xhtml#g_t2_002e5_002e1) Generic Arithmetic Operations - [2.77](src/sicp/chapter_2/part_5/ex_2_77.clj)
* [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

Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
:source-paths ["src"]
:test-paths ["test"]
:main ^:skip-aot sicp
:target-path "build/%s"
:target-path "target/%s"
:plugins [[lein-cloverage "1.2.2"]]
:profiles {:dev {:dependencies [[org.clojure/clojure "1.11.1"]
[io.github.noahtheduke/splint "1.12"]]}
Expand Down
45 changes: 45 additions & 0 deletions src/sicp/chapter_2/packages/install_complex.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
(ns sicp.chapter-2.packages.install-complex
(:require
[sicp.chapter-2.packages.op-table :as ot]
[sicp.chapter-2.part-4.book-2-4 :as b24]))

(defn install-complex-package
[]
; Imported procedures from rectangular and polar packages
(letfn [(make-from-real-imag [x y] ((ot/get :make-from-real-imag :rectangular) x y))
(make-from-mag-ang [r a] ((ot/get :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] (ot/attach-tag :complex z))]
; Putting functions in a map or registry
(ot/put :add [:complex :complex] (fn [z1 z2] (tag (add-complex z1 z2))))
(ot/put :sub [:complex :complex] (fn [z1 z2] (tag (sub-complex z1 z2))))
(ot/put :mul [:complex :complex] (fn [z1 z2] (tag (mul-complex z1 z2))))
(ot/put :div [:complex :complex] (fn [z1 z2] (tag (div-complex z1 z2))))
(ot/put :make-from-real-imag :complex (fn [x y] (tag (make-from-real-imag x y))))
(ot/put :make-from-mag-ang :complex (fn [r a] (tag (make-from-mag-ang r a))))
:done))

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

(defn make-complex-from-mag-ang
[r a]
((ot/get :make-from-mag-ang :complex) r a))
24 changes: 24 additions & 0 deletions src/sicp/chapter_2/packages/install_polar.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
(ns sicp.chapter-2.packages.install-polar
(:require
[sicp.chapter-2.packages.op-table :as ot]))

(defn install-polar-package
[]
(letfn [(magnitude [z] (first z))
(angle [z] (second z))
(make-from-mag-ang [r a] (list r a))
(real-part [z] (* (magnitude z) (Math/cos (angle z))))
(imag-part [z] (* (magnitude z) (Math/sin (angle z))))
(make-from-real-imag
[x y]
(list (Math/sqrt (+ (Math/pow x 2) (Math/pow y 2)))
(Math/atan2 y x)))
(tag [x] (cons :polar x))]
; interface to the rest of the system
(ot/put :real-part :polar real-part)
(ot/put :imag-part :polar imag-part)
(ot/put :magnitude :polar magnitude)
(ot/put :angle :polar angle)
(ot/put :make-from-real-imag :polar (fn [x y] (tag (make-from-real-imag x y))))
(ot/put :make-from-mag-ang :polar (fn [r a] (tag (make-from-mag-ang r a))))
:done))
44 changes: 44 additions & 0 deletions src/sicp/chapter_2/packages/install_rational.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
(ns sicp.chapter-2.packages.install-rational
(:require
[sicp.chapter-2.packages.op-table :as ot]
[sicp.misc :as m]))

(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] (ot/attach-tag :rational x))]
(ot/put :add [:rational :rational] (fn [x y] (tag (add-rat x y))))
(ot/put :sub [:rational :rational] (fn [x y] (tag (sub-rat x y))))
(ot/put :mul [:rational :rational] (fn [x y] (tag (mul-rat x y))))
(ot/put :div [:rational :rational] (fn [x y] (tag (div-rat x y))))
(ot/put :make :rational (fn [n d] (tag (make-rat n d))))
:done))

(defn make-rational
[n d]
((ot/get :make :rational) n d))
25 changes: 25 additions & 0 deletions src/sicp/chapter_2/packages/install_rectangular.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
(ns sicp.chapter-2.packages.install-rectangular
(:require
[sicp.chapter-2.packages.op-table :as ot]))

(defn install-rectangular-package
[]
; internal procedures
(letfn [(real-part [z] (first z))
(imag-part [z] (second z))
(make-from-real-imag [x y] (list x y))
(magnitude
[z]
(Math/sqrt (+ (Math/pow (real-part z) 2)
(Math/pow (imag-part z) 2))))
(angle [z] (Math/atan2 (imag-part z) (real-part z)))
(make-from-mag-ang [r a] (list (* r (Math/cos a)) (* r (Math/sin a))))
(tag [x] (ot/attach-tag :rectangular x))]
; interface to the rest of the system
(ot/put :real-part '(:rectangular) real-part)
(ot/put :imag-part '(:rectangular) imag-part)
(ot/put :magnitude '(:rectangular) magnitude)
(ot/put :angle '(:rectangular) angle)
(ot/put :make-from-real-imag :rectangular (fn [x y] (tag (make-from-real-imag x y))))
(ot/put :make-from-mag-ang :rectangular (fn [r a] (tag (make-from-mag-ang r a))))
:done))
17 changes: 17 additions & 0 deletions src/sicp/chapter_2/packages/install_scheme_number.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(ns sicp.chapter-2.packages.install-scheme-number
(:require
[sicp.chapter-2.packages.op-table :as ot]))

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

(defn make-scheme-number
[n]
((ot/get :make :scheme-number) n))
36 changes: 36 additions & 0 deletions src/sicp/chapter_2/packages/op_table.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
(ns sicp.chapter-2.packages.op-table
(:refer-clojure :exclude [get])
(:require
[clojure.pprint :as pp]))

(comment "Custom helper functions. Made myself, just for testing ---------------------------------")

(def operation-table (atom {}))

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

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

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

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

(defn inspect
[]
(->> @operation-table
(map (fn [item]
{:operation (first item)
:types (first (first (into '() (second item))))}))
(pp/print-table)))
13 changes: 7 additions & 6 deletions src/sicp/chapter_2/part_4/book_2_4.clj
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@

(defn type-tag
[datum]
(if (sequential? datum)
(first datum)
(throw (Exception. (str "Bad tagged datum: TYPE-TAG " datum)))))
(cond (sequential? datum) (first datum)
(map? datum) (get datum :tag)
:else (throw (Exception. (str "Bad tagged datum: TYPE-TAG " datum)))))

(defn contents
[datum]
(if (sequential? datum)
(rest datum)
(throw (Exception. (str "Bad tagged datum: CONTENTS " datum)))))
(cond (sequential? datum) (rest datum)
(map? datum) (get datum :contents)
:else (throw (Exception. (str "Bad tagged datum: CONTENTS " datum)))))

(defn rectangular?
[z]
Expand Down Expand Up @@ -215,6 +215,7 @@
; * 2.76

(defn apply-generic
"Just a wrapper for the dispatch table. It's a bit more verbose than the original, I don't use it"
[op & args]
(let [type-tags (map type-tag args)
proc (get op type-tags)]
Expand Down
Loading

0 comments on commit 550f8d7

Please sign in to comment.