Skip to content

Commit

Permalink
Exercise 1.10 (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
SmetDenis authored Oct 15, 2023
1 parent f194904 commit 11f0ddb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
4 changes: 1 addition & 3 deletions Clojure-Sicp.iml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/dev" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/resources" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
</content>
<orderEntry type="jdk" jdkName="19" jdkType="JavaSDK" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Deps: org.clojure/clojure:1.11.1" level="project" />
<orderEntry type="library" name="Deps: io.github.cognitect-labs/test-runner:dfb30d" level="project" />
Expand Down
25 changes: 25 additions & 0 deletions src/sicp/chapter_1/ex_1_10.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
(ns sicp.chapter-1.ex-1-10)

; Exercise 1.10
; The following procedure computes a mathematical function called Ackermann’s function.
(defn A [x y]
(cond (= y 0) 0
(= x 0) (* 2 y)
(= y 1) 2
:else (A (- x 1)
(A x (- y 1)))))

; What are the values of the following expressions?
(A 1 10)
(A 2 4)
(A 3 3)

; Consider the following procedures, where A is the procedure defined above:
(defn f [n] (A 0 n))
(defn g [n] (A 1 n))
(defn h [n] (A 2 n))
(defn k [n] (* 5 n n))

; Give concise mathematical definitions for the functions computed
; by the procedures f, g, and h for positive integer values of n.
; For example, (k n) computes 5*n^2.
13 changes: 13 additions & 0 deletions test/sicp/chapter_1/ex_1_10_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(ns sicp.chapter-1.ex-1-10-test
(:require [clojure.test :refer :all]
[sicp.chapter-1.ex-1-10 :refer [A f g h k]]))

(deftest A-test
(is (= 1024 (A 1 10)))
(is (= 65536 (A 2 4)))
(is (= 65536 (A 3 3)))

(is (= 6 (f 3)))
(is (= 8 (g 3)))
(is (= 16 (h 3)))
(is (= 45 (k 3))))

0 comments on commit 11f0ddb

Please sign in to comment.