commit 8b37951b285db9cbedae2c85cc6a24f28cbb8d30 Author: Petar Kapriš Date: Wed Jan 15 16:52:59 2025 +0100 Add solutions to exercises from section 1.1 diff --git a/ex-1.01.txt b/ex-1.01.txt new file mode 100644 index 0000000..f981ef0 --- /dev/null +++ b/ex-1.01.txt @@ -0,0 +1,28 @@ +10 -> 10 +(+ 5 3 4) -> 12 +(- 9 1) -> 8 +(/ 6 2) -> 3 +(+ (* 2 4) (- 4 6)) -> 6 +(define a 3) -> a (3) +(define b (+ a 1)) -> b (4) +(+ a b (* a b)) -> 19 +(= a b) -> #f + +(if (and (> b a) (< b (* a b))) + b + a) +-> 4 + +(cond ((= a 4) 6) + ((= b 4) (+ 6 7 a)) + (else 25)) +-> (+ 6 7 a) -> 16 + +(+ 2 (if (> b a) b a)) +-> 6 + +(* (cond ((> a b) a) + ((< a b) b) + (else -1)) + (+ a 1)) +-> 16 diff --git a/ex-1.02.scm b/ex-1.02.scm new file mode 100644 index 0000000..6f39e14 --- /dev/null +++ b/ex-1.02.scm @@ -0,0 +1,6 @@ +(/ (+ 5 + 4 + (- 2 (- 3 (+ 6 (/ 4 5))))) + (* 3 + (- 6 2) + (- 2 7))) diff --git a/ex-1.03.scm b/ex-1.03.scm new file mode 100644 index 0000000..2ff120c --- /dev/null +++ b/ex-1.03.scm @@ -0,0 +1,9 @@ +#| BEGIN (Write your solution here) |# +(define (sqr x) (* x x)) +(define (solution a b c) + (cond ((and (<= a b) (<= a c)) + (+ (sqr b) (sqr c))) + ((and (<= b a) (<= b c)) + (+ (sqr a) (sqr c))) + (else (+ (sqr a) (sqr b))))) +#| END |# diff --git a/ex-1.04.txt b/ex-1.04.txt new file mode 100644 index 0000000..45277b8 --- /dev/null +++ b/ex-1.04.txt @@ -0,0 +1,2 @@ +The if statement will select the appropriate function to execute, if b>0, it +will add b, otherwise it will subtract it. diff --git a/ex-1.05.txt b/ex-1.05.txt new file mode 100644 index 0000000..579c0fb --- /dev/null +++ b/ex-1.05.txt @@ -0,0 +1,4 @@ +If the interpreter uses normal-order evaluation, it will expand test into an +"if", which will then evaluate to 0, if it uses applicative-order evaluation, +it will first evaluate 0 and (p), while trying to evaluate (p), it will enter +an infinite loop. diff --git a/ex-1.06.txt b/ex-1.06.txt new file mode 100644 index 0000000..66fcc5c --- /dev/null +++ b/ex-1.06.txt @@ -0,0 +1,3 @@ +The program will run into an infinite loop, because in every execution of +square-iter, the improve function is called again, since this is an +applicative-ordered language diff --git a/ex-1.07.scm b/ex-1.07.scm new file mode 100644 index 0000000..e9b943c --- /dev/null +++ b/ex-1.07.scm @@ -0,0 +1,18 @@ +#| BEGIN (Write your solution here) |# +(define (average a b) + (/ (+ a b) 2)) + +(define (improve guess x) + (average guess (/ x guess))) + +(define (good-enough? guess x) + (< (abs (- (improve guess x) guess)) 0.0001)) +(define (sqrt-iter guess x) + (if (good-enough? guess x) + guess + (sqrt-iter (improve guess x) + x))) + +(define (square-root x) + (sqrt-iter 1.0 x)) +#| END |# diff --git a/ex-1.08.scm b/ex-1.08.scm new file mode 100644 index 0000000..95021ce --- /dev/null +++ b/ex-1.08.scm @@ -0,0 +1,18 @@ +#| BEGIN (Write your solution here) |# +(define (improve guess x) + (/ (+ (/ x guess guess) + (* 2 guess)) + 3)) + +(define (good-enough? guess x) + (< (abs (- (improve guess x) guess)) 0.0001)) + +(define (cbrt-iter guess x) + (if (good-enough? guess x) + guess + (cbrt-iter (improve guess x) + x))) + +(define (cube-root x) + (cbrt-iter 1.0 x)) +#| END |#