Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exercise 3.34 solution #205

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions doc/chapter03/ex_3_34.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# [Глава 3](../index.md#Глава-3)

### Упражнение 3.34
Хьюго Дум хочет построить квадратор, блок-ограничение с двумя выводами, такое, что значение соединителя `b` на втором выводе всегда будет равно квадрату значения соединителя `a` на первом выводе. Он предлагает следующее простое устройство на основе умножителя:

```clojure
(defn squarer [a b]
(multiplier a a b))
```

В такой идее есть существенная ошибка. Объясните ее.

#### Решение
Установка значения для вывода `b` не позволяет получить значение `a`.

[Code](../../src/sicp/chapter03/3_34.clj) | [Test](../../test/sicp/chapter03/3_34_test.clj)

1 change: 1 addition & 0 deletions doc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,4 @@
* [Упражнение 3.31](./chapter03/ex_3_31.md) Альтернативный вариант процедуры `accept-action-procedure!`
* [Упражнение 3.32](./chapter03/ex_3_32.md)
* [Упражнение 3.33](./chapter03/ex_3_33.md) Система ограничений. Процедура `averager`.
* [Упражнение 3.34](./chapter03/ex_3_34.md) Система ограничений. Процедура `squarer`.
6 changes: 6 additions & 0 deletions src/sicp/chapter03/3_34.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(ns sicp.chapter03.3-34
(:require [sicp.chapter03.constraints :refer [multiplier]]))

(defn squarter
[a b]
(multiplier a a b))
26 changes: 26 additions & 0 deletions test/sicp/chapter03/3_34_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
(ns sicp.chapter03.3-34-test
(:require [clojure.test :refer :all]
[sicp.chapter03.3-34 :refer [squarter]]
[sicp.chapter03.constraints :refer [get-value make-connector probe
set-value!]]))

(deftest test-squarter
(let [a (make-connector)
b (make-connector)]
(squarter a b)

(probe "A" a)
(probe "B" b)

(is (= :done (set-value! a 3 :user)))
(is (= 9 (get-value b))))

(let [a (make-connector)
b (make-connector)]
(squarter a b)

(probe "A" a)
(probe "B" b)

(is (= :done (set-value! b 9 :user)))
(is (nil? (get-value a)))))