From 11f0ddb7c00449a9beea9543d8ad7b1e19c62f29 Mon Sep 17 00:00:00 2001 From: Denis Smetannikov Date: Sun, 15 Oct 2023 21:03:57 +0300 Subject: [PATCH] Exercise 1.10 (#5) --- Clojure-Sicp.iml | 4 +--- src/sicp/chapter_1/ex_1_10.clj | 25 +++++++++++++++++++++++++ test/sicp/chapter_1/ex_1_10_test.clj | 13 +++++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 src/sicp/chapter_1/ex_1_10.clj create mode 100644 test/sicp/chapter_1/ex_1_10_test.clj diff --git a/Clojure-Sicp.iml b/Clojure-Sicp.iml index 7ea0953..ed1c807 100644 --- a/Clojure-Sicp.iml +++ b/Clojure-Sicp.iml @@ -7,12 +7,10 @@ - - - + diff --git a/src/sicp/chapter_1/ex_1_10.clj b/src/sicp/chapter_1/ex_1_10.clj new file mode 100644 index 0000000..466f5c4 --- /dev/null +++ b/src/sicp/chapter_1/ex_1_10.clj @@ -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. diff --git a/test/sicp/chapter_1/ex_1_10_test.clj b/test/sicp/chapter_1/ex_1_10_test.clj new file mode 100644 index 0000000..d93f2eb --- /dev/null +++ b/test/sicp/chapter_1/ex_1_10_test.clj @@ -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))))