Skip to content

Commit

Permalink
Exercise 2.75 (#152)
Browse files Browse the repository at this point in the history
Exercise 2.75 from Chapter 2 part 4 about implementing `make-from-mag-ang` in message-passing style has been added. This solution is meant to be analogous to the `make-from-real-imag` provided in the book.
  • Loading branch information
SmetDenis authored Feb 4, 2024
1 parent 17b4ea3 commit cf167b1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
18 changes: 15 additions & 3 deletions src/sicp/chapter_2/part_4/book_2_4.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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))

15 changes: 15 additions & 0 deletions src/sicp/chapter_2/part_4/ex_2_75.clj
Original file line number Diff line number Diff line change
@@ -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))))))

0 comments on commit cf167b1

Please sign in to comment.