diff --git a/README.md b/README.md index 3b7d837..c419453 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Solving exercises from SICP with Clojure [![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) -![Progress](https://progress-bar.dev/120/?scale=356&title=Solved&width=500&suffix=) +![Progress](https://progress-bar.dev/121/?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. @@ -42,7 +42,7 @@ Jay Sussman on basics of computer science and software engineering. ### Chapter 2 - Building Abstractions with Data -![Progress](https://progress-bar.dev/74/?scale=97&title=Solved&width=500&suffix=) +![Progress](https://progress-bar.dev/75/?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) @@ -62,7 +62,7 @@ Jay Sussman on basics of computer science and software engineering. * [2.4](https://sarabander.github.io/sicp/html/2_002e4.xhtml#g_t2_002e4) Multiple Representations for Abstract Data * [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.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.5 Systems with Generic Operations * 2.5.1 Generic Arithmetic Operations * 2.5.2 Combining Data of Different Types diff --git a/src/sicp/chapter_2/part_4/book_2_4.clj b/src/sicp/chapter_2/part_4/book_2_4.clj index dee5bc2..672b430 100644 --- a/src/sicp/chapter_2/part_4/book_2_4.clj +++ b/src/sicp/chapter_2/part_4/book_2_4.clj @@ -134,15 +134,15 @@ (polar? z) (angle-polar (contents z)) :else (throw (Exception. (str "Unknown type: ANGLE " z))))) -(defn make-from-real-imag [x y] +(defn make-from-real-imag-v2 [x y] (make-from-real-imag-rectangular x y)) (defn add-complex [z1 z2] - (make-from-real-imag + (make-from-real-imag-v2 (+ (real-part z1) (real-part z2)) (+ (imag-part z1) (imag-part z2)))) -(defn make-from-mag-ang [r a] +(defn make-from-mag-ang-v2 [r a] (make-from-mag-ang-polar r a)) (comment "2.4.3") @@ -160,3 +160,15 @@ (apply proc (map contents args)) (throw (Exception. (str "No method for these types: APPLY-GENERIC " (list op type-tags))))))) + +(defn make-from-real-imag [x y] + (fn [op] + (cond + (= op :real-part) x + (= op :imag-part) y + (= op :magnitude) (Math/sqrt (+ (* x x) (* y y))) + (= op :angle) (Math/atan2 y x) + :else (throw (Exception. (str "Unknown op: MAKE-FROM-REAL-IMAG " op)))))) + +(defn apply-generic-v2 [op arg] (arg op)) + diff --git a/src/sicp/chapter_2/part_4/ex_2_75.clj b/src/sicp/chapter_2/part_4/ex_2_75.clj new file mode 100644 index 0000000..ca678dc --- /dev/null +++ b/src/sicp/chapter_2/part_4/ex_2_75.clj @@ -0,0 +1,15 @@ +(ns sicp.chapter-2.part-4.ex-2-75) + +; Exercise 2.75 +; +; Implement the constructor make-from-mag-ang in message-passing style. +; This procedure should be analogous to the make-from-real-imag procedure given above. + +(defn make-from-mag-ang [r a] + (fn [op] + (cond + (= op :real-part) (* r (Math/cos a)) + (= op :imag-part) (* r (Math/sin a)) + (= op :magnitude) r + (= op :angle) a + :else (throw (Exception. (str "Unknown op: MAKE-FROM-REAL-IMAG " op))))))