Sicp

{"content":{"id":261,"title":"SICP Solutions - 1.8","body":"\u003cp\u003e\u003cb\u003eQ:\u003c/b\u003e Newton's method for cube roots is based on the fact that if y is an approximation to the cube root of x, then a better approximation is given by the value:\r\n\u003c/p\u003e\r\n\u003cimg src=\"/uploads/c7.gif\" class=\"card-image\"\u003e\r\n\u003cp\u003e\r\nUse this formula to implement a cube-root procedure analogous to the square-root procedure. (In section 1.3.4 we will see how to implement Newton's method in general as an abstraction of these square-root and cube-root procedures.)\r\n\u003c/p\u003e\r\n\u003cbr\u003e\r\n\u003cp\u003e\u003cb\u003eA:\u003c/b\u003e \r\n\u003c/p\u003e\r\n\u003cpre\u003e\u003ccode class=\"lisp\"\u003e;; The setup is the same, except for the names.\r\n(define (cbrt-iter guess x)\r\n (if (good-enough? guess x)\r\n guess\r\n (cbrt-iter (improve guess x)\r\n x)))\r\n\r\n(define (cube x)\r\n (* x x x))\r\n\r\n(define (good-enough? guess x)\r\n (\u003c (abs (- (cube guess) x)) 0.001))\r\n\r\n(define (cbrt x)\r\n (cbrt-iter 1.0 x))\r\n\r\n;; The formula in scheme.\r\n(define (improve guess x)\r\n (/ \r\n (+ (/ x (* guess guess)) (* 2 guess))\r\n 3))\r\n\r\n;; A few guesses.\r\n(display (cbrt 8))\r\n(newline)\r\n(display (cbrt 27))\r\n(newline)\r\n(display (cbrt 60))\r\n(newline)\r\n\u003c/code\u003e\u003c/pre\u003e\r\n\u003cpre\u003e\u003ccode class=\"lisp\"\u003e2.0000049116755\r\n3.00000054106418\r\n3.91487458417134 ; My calculator gives 3.91486764117\r\n\u003c/code\u003e\u003c/pre\u003e","publication_date":"2018-09-15T03:00:00.000Z","created_at":"2018-09-08T03:13:03.000Z","updated_at":"2019-01-16T02:28:24.000Z","user_id":1,"rating":null},"tags":"\u003ca class=\"changeable-title\" href=\"/q?tag=sicp\"\u003esicp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=programming\"\u003eprogramming\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=answers\"\u003eanswers\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=lisp\"\u003elisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=scheme\"\u003escheme\u003c/a\u003e"}

SICP Solutions - 1.8

Q: Newton's method for cube roots is based on the fact that if y is an approximation to the cube root of x, then a better approximation is given by the value:

Use this formula to implement a cube-root procedure analogous to the square-root procedure. (In section 1.3.4 we will see how to implement Newton's method in general as an abstraction of these square-root and cube-root procedures.)


A:

;; The setup is the same, except for the names.
(define (cbrt-iter guess x)
  (if (good-enough? guess x)
    guess
    (cbrt-iter (improve guess x)
               x)))

(define (cube x)
  (* x x x))

(define (good-enough? guess x)
  (< (abs (- (cube guess) x)) 0.001))

(define (cbrt x)
  (cbrt-iter 1.0 x))

;; The formula in scheme.
(define (improve guess x)
  (/ 
    (+ (/ x (* guess guess)) (* 2 guess))
    3))

;; A few guesses.
(display (cbrt 8))
(newline)
(display (cbrt 27))
(newline)
(display (cbrt 60))
(newline)
2.0000049116755
3.00000054106418
3.91487458417134 ; My calculator gives 3.91486764117
{"content":{"id":117,"title":"SICP Solutions - 1.7","body":"\u003cp\u003e\u003cb\u003eQ:\u003c/b\u003e The good-enough? test used in computing square roots will not be very effective for finding the square roots of very small numbers. Also, in real computers, arithmetic operations are almost always performed with limited precision. This makes our test inadequate for very large numbers. Explain these statements, with examples showing how the test fails for small and large numbers. An alternative strategy for implementing good-enough? is to watch how guess changes from one iteration to the next and to stop when the change is a very small fraction of the guess. Design a square-root procedure that uses this kind of end test. Does this work better for small and large numbers?\u003c/p\u003e\r\n\u003cbr\u003e\r\n\u003cp\u003e\u003cb\u003eA:\u003c/b\u003e This is best demonstrated through examples using perfect squares:\u003c/p\u003e\r\n\u003cpre\u003e\u003ccode class=\"lisp\"\u003e\u0026gt (sqrt 1000000000000)\r\n1000000.0\r\n\u0026gt (sqrt 10000000000000)\r\n; Infinite loop. \r\n\u0026gt (sqrt 0.01)\r\n0.100325785109606\r\n\u0026gt (sqrt 0.001)\r\n0.0412454260749911 ; Good enough for government work.\r\n\u0026gt (sqrt 0.0001)\r\n0.0323084483304812 ; Uh oh!\r\n\u0026gt (sqrt 0.0001)\r\n0.0323084483304812\r\n\u0026gt (sqrt 0.00001)\r\n0.0313564901077172\r\n\u0026gt (sqrt 0.000001)\r\n0.0312606555254453\r\n\u003c/code\u003e\u003c/pre\u003e\r\n\u003cp\u003eVery big numbers run into the limitations of your implementation of Scheme and/or how your machine handles decimals, and good-enough? doesn't work for numbers less than its threshold. Now, let's try changing good-enough? as the question suggests:\u003c/p\u003e\r\n\u003cpre\u003e\u003ccode class=\"lisp\"\u003e(define (good-enough? guess old-guess)\r\n (\u003c (abs (- guess old-guess)) (* guess 0.001)))\r\n\r\n; It's a good thing I already read Little Schemer,\r\n; because this book really glosses over recursion.\r\n(define (sqrt-iter guess old-guess x)\r\n (if (good-enough? guess old-guess)\r\n guess\r\n (sqrt-iter (improve guess x) guess x)))\r\n\r\n(define (sqrt x)\r\n (sqrt-iter 1.0 2.0 x))\r\n\u003c/code\u003e\u003c/pre\u003e\r\n\u003cpre\u003e\u003ccode class=\"lisp\"\u003e\u0026gt (sqrt 1000000000000)\r\n1000000.10346124 ; Not better, but carrying on...\r\n\u0026gt (sqrt 10000000000000)\r\n3162277.66401048\r\n\u0026gt (sqrt 100000000000000) \r\n10000000.000044\r\n\u0026gt (sqrt 1000000000000000) \r\n31622779.2799952 ; And so on. Wowie!\r\n; Now the small numbers\r\n\u0026gt (sqrt 0.01)\r\n0.100000000001399\r\n\u0026gt (sqrt 0.001)\r\n0.031622782450701\r\n\u0026gt (sqrt 0.0001)\r\n0.0100000000254907\r\n\u0026gt (sqrt 0.00001)\r\n0.0031622776602039\r\n\u0026gt (sqrt 0.000001)\r\n0.00100000015330166 ; And so on.\r\n\u003c/code\u003e\u003c/pre\u003e\r\n\u003cp\u003eThis new way is obviously better. To get some intuition as to why, you can modify the new good-enough? to print each iteration, and play around in a REPL. This post is already pretty long, so I'll leave it as an exercise to the answer-reader.\u003c/p\u003e\r\n\u003cpre\u003e\u003ccode class=\"lisp\"\u003e(define (good-enough? guess old-guess)\r\n (display (abs (- guess old-guess)))\r\n (newline)\r\n (display (* guess 0.001))\r\n (newline)\r\n (newline)\r\n (\u003c (abs (- guess old-guess)) (* guess 0.001)))\r\n\u003c/code\u003e\u003c/pre\u003e","publication_date":"2018-06-26T15:00:00.000Z","created_at":"2018-06-20T16:33:23.000Z","updated_at":"2018-07-05T16:30:50.000Z","user_id":1,"rating":null},"tags":"\u003ca class=\"changeable-title\" href=\"/q?tag=sicp\"\u003esicp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=programming\"\u003eprogramming\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=answers\"\u003eanswers\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=lisp\"\u003elisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=scheme\"\u003escheme\u003c/a\u003e"}

SICP Solutions - 1.7

Q: The good-enough? test used in computing square roots will not be very effective for finding the square roots of very small numbers. Also, in real computers, arithmetic operations are almost always performed with limited precision. This makes our test inadequate for very large numbers. Explain these statements, with examples showing how the test fails for small and large numbers. An alternative strategy for implementing good-enough? is to watch how guess changes from one iteration to the next and to stop when the change is a very small fraction of the guess. Design a square-root procedure that uses this kind of end test. Does this work better for small and large numbers?


A: This is best demonstrated through examples using perfect squares:

> (sqrt 1000000000000)
1000000.0
> (sqrt 10000000000000)
; Infinite loop. 
> (sqrt 0.01)
0.100325785109606
> (sqrt 0.001)
0.0412454260749911 ; Good enough for government work.
> (sqrt 0.0001)
0.0323084483304812 ; Uh oh!
> (sqrt 0.0001)
0.0323084483304812
> (sqrt 0.00001)
0.0313564901077172
> (sqrt 0.000001)
0.0312606555254453

Very big numbers run into the limitations of your implementation of Scheme and/or how your machine handles decimals, and good-enough? doesn't work for numbers less than its threshold. Now, let's try changing good-enough? as the question suggests:

(define (good-enough? guess old-guess)
  (< (abs (- guess old-guess)) (* guess 0.001)))

; It's a good thing I already read Little Schemer,
; because this book really glosses over recursion.
(define (sqrt-iter guess old-guess x)
  (if (good-enough? guess old-guess)
    guess
    (sqrt-iter (improve guess x) guess x)))

(define (sqrt x)
  (sqrt-iter 1.0 2.0 x))
> (sqrt 1000000000000)
1000000.10346124 ; Not better, but carrying on...
> (sqrt 10000000000000)
3162277.66401048
> (sqrt 100000000000000)   
10000000.000044
> (sqrt 1000000000000000)  
31622779.2799952 ; And so on. Wowie!
; Now the small numbers
> (sqrt 0.01)
0.100000000001399
> (sqrt 0.001)
0.031622782450701
> (sqrt 0.0001)
0.0100000000254907
> (sqrt 0.00001)
0.0031622776602039
> (sqrt 0.000001)
0.00100000015330166 ; And so on.

This new way is obviously better. To get some intuition as to why, you can modify the new good-enough? to print each iteration, and play around in a REPL. This post is already pretty long, so I'll leave it as an exercise to the answer-reader.

(define (good-enough? guess old-guess)
  (display (abs (- guess old-guess)))
  (newline)
  (display (* guess 0.001))
  (newline)
  (newline)
  (< (abs (- guess old-guess)) (* guess 0.001)))
{"content":{"id":109,"title":"SICP Solutions - 1.6","body":"\u003cbr\u003e\r\n\u003cp\u003e\u003cb\u003eQ:\u003c/b\u003e Alyssa P. Hacker doesn't see why if needs to be provided as a special form. ``Why can't I just define it as an ordinary procedure in terms of cond?'' she asks. Alyssa's friend Eva Lu Ator claims this can indeed be done, and she defines a new version of if: \u003c/p\u003e\r\n\u003cpre\u003e\u003ccode class=\"lisp\"\u003e(define (new-if predicate then-clause else-clause)\r\n (cond (predicate then-clause)\r\n (else else-clause)))\r\n\u003c/code\u003e\u003c/pre\u003e\r\n\u003cp\u003e\r\nEva demonstrates the program for Alyssa:\r\n\u003c/p\u003e\r\n\u003cpre\u003e\u003ccode class=\"lisp\"\u003e(new-if (= 2 3) 0 5)\r\n5\r\n\r\n(new-if (= 1 1) 0 5)\r\n0\r\n\u003c/code\u003e\u003c/pre\u003e\r\n\u003cp\u003eDelighted, Alyssa uses new-if to rewrite the square-root program:\u003c/p\u003e\r\n\u003cpre\u003e\u003ccode class=\"lisp\"\u003e(define (sqrt-iter guess x)\r\n (new-if (good-enough? guess x)\r\n guess\r\n (sqrt-iter (improve guess x)\r\n x)))\r\n\u003c/code\u003e\u003c/pre\u003e\r\n\u003cp\u003eWhat happens when Alyssa attempts to use this to compute square roots? Explain.\u003c/p\u003e\r\n\u003cbr\u003e\r\n\u003cp\u003e \u003cb\u003eA:\u003c/b\u003e The program hangs forever. This is a similar issue to the \u003ca href=\"https://sananab.ca/posts/106\" target=\"_blank\"\u003elast problem\u003c/a\u003e. Because of application order, it will continuously evaluate sqrt-iter in an infinite loop. Normal 'if' in Scheme is a special procedure built into the language and does not have that behaviour.\u003c/p\u003e","publication_date":"2018-06-23T03:00:00.000Z","created_at":"2018-06-17T17:53:06.000Z","updated_at":"2018-06-22T14:14:07.000Z","user_id":1,"rating":null},"tags":"\u003ca class=\"changeable-title\" href=\"/q?tag=sicp\"\u003esicp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=programming\"\u003eprogramming\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=answers\"\u003eanswers\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=lisp\"\u003elisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=scheme\"\u003escheme\u003c/a\u003e"}

SICP Solutions - 1.6


Q: Alyssa P. Hacker doesn't see why if needs to be provided as a special form. ``Why can't I just define it as an ordinary procedure in terms of cond?'' she asks. Alyssa's friend Eva Lu Ator claims this can indeed be done, and she defines a new version of if:

(define (new-if predicate then-clause else-clause)
  (cond (predicate then-clause)
        (else else-clause)))

Eva demonstrates the program for Alyssa:

(new-if (= 2 3) 0 5)
5

(new-if (= 1 1) 0 5)
0

Delighted, Alyssa uses new-if to rewrite the square-root program:

(define (sqrt-iter guess x)
  (new-if (good-enough? guess x)
          guess
          (sqrt-iter (improve guess x)
                     x)))

What happens when Alyssa attempts to use this to compute square roots? Explain.


A: The program hangs forever. This is a similar issue to the last problem. Because of application order, it will continuously evaluate sqrt-iter in an infinite loop. Normal 'if' in Scheme is a special procedure built into the language and does not have that behaviour.

{"content":{"id":106,"title":"SICP Solutions - 1.5","body":"\u003cbr\u003e\r\n\u003cp\u003e\u003cb\u003eQ:\u003c/b\u003e Ben Bitdiddle has invented a test to determine whether the interpreter he is faced with is using applicative-order evaluation or normal-order evaluation. He defines the following two procedures:\u003c/p\u003e\r\n\u003cpre\u003e\u003ccode class=\"lisp\"\u003e(define (p) (p))\r\n\r\n(define (test x y)\r\n (if (= x 0)\r\n 0\r\n y))\u003c/code\u003e\u003c/pre\u003e\r\n\u003cp\u003eThen he evaluates the expression:\u003c/p\u003e\r\n\u003cpre\u003e\u003ccode class=\"lisp\"\u003e(test 0 (p))\u003c/code\u003e\u003c/pre\u003e\r\n\u003cp\u003eWhat behavior will Ben observe with an interpreter that uses applicative-order evaluation? What behavior will he observe with an interpreter that uses normal-order evaluation? Explain your answer. (Assume that the evaluation rule for the special form if is the same whether the interpreter is using normal or applicative order: The predicate expression is evaluated first, and the result determines whether to evaluate the consequent or the alternative expression.)\u003c/p\u003e\r\n\u003cp\u003e\u003cb\u003eA:\u003c/b\u003e\u003c/p\u003e\r\n\u003cpre\u003e\u003ccode class=\"lisp\"\u003e; Normal Order:\r\n(if (= 0 0) 0 (p))\r\n(if #t 0 (p))\r\n0\r\n\r\n; Applicative: Because the interpreter evaluates (p)\r\n; independently of any condition, you are stuck in\r\n; an infinite loop as each instance of (p) evaluates\r\n; (p), independently of the results of the if condition.\r\n\u003c/code\u003e\u003c/pre\u003e","publication_date":"2018-06-21T15:00:00.000Z","created_at":"2018-06-15T19:24:06.000Z","updated_at":"2018-06-22T02:37:27.000Z","user_id":1,"rating":null},"tags":"\u003ca class=\"changeable-title\" href=\"/q?tag=sicp\"\u003esicp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=programming\"\u003eprogramming\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=answers\"\u003eanswers\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=lisp\"\u003elisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=scheme\"\u003escheme\u003c/a\u003e"}

SICP Solutions - 1.5


Q: Ben Bitdiddle has invented a test to determine whether the interpreter he is faced with is using applicative-order evaluation or normal-order evaluation. He defines the following two procedures:

(define (p) (p))

(define (test x y)
       (if (= x 0)
           0
           y))

Then he evaluates the expression:

(test 0 (p))

What behavior will Ben observe with an interpreter that uses applicative-order evaluation? What behavior will he observe with an interpreter that uses normal-order evaluation? Explain your answer. (Assume that the evaluation rule for the special form if is the same whether the interpreter is using normal or applicative order: The predicate expression is evaluated first, and the result determines whether to evaluate the consequent or the alternative expression.)

A:

; Normal Order:
(if (= 0 0) 0 (p))
(if #t 0 (p))
0

; Applicative: Because the interpreter evaluates (p)
; independently of any condition, you are stuck in
; an infinite loop as each instance of (p) evaluates
; (p), independently of the results of the if condition.
{"content":{"id":92,"title":"SICP Solutions - 1.4","body":"\u003cp\u003e\u003cb\u003eQ:\u003c/b\u003e Observe that our model of evaluation allows for combinations whose operators are compound expressions. Use this observation to describe the behavior of the following procedure:\u003c/p\u003e\r\n\u003cpre\u003e\u003ccode class=\"lisp\"\u003e(define (a-plus-abs-b a b)\r\n ((if (\u003e b 0) + -) a b))\r\n\u003c/code\u003e\u003c/pre\u003e\r\n\u003cp\u003e\u003cb\u003eA:\u003c/b\u003e The procedure checks if b is greater than 0, and returns the addition procedure if true, giving us (+ a b), and the subtraction procedure if false, giving us (- a b).\u003c/p\u003e","publication_date":"2018-06-21T00:00:00.000Z","created_at":"2018-06-12T18:02:20.000Z","updated_at":"2020-07-06T19:08:51.000Z","user_id":1,"rating":null},"tags":"\u003ca class=\"changeable-title\" href=\"/q?tag=sicp\"\u003esicp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=programming\"\u003eprogramming\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=answers\"\u003eanswers\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=lisp\"\u003elisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=scheme\"\u003escheme\u003c/a\u003e"}

SICP Solutions - 1.4

Q: Observe that our model of evaluation allows for combinations whose operators are compound expressions. Use this observation to describe the behavior of the following procedure:

(define (a-plus-abs-b a b)
  ((if (> b 0) + -) a b))

A: The procedure checks if b is greater than 0, and returns the addition procedure if true, giving us (+ a b), and the subtraction procedure if false, giving us (- a b).

{"content":{"id":89,"title":"SICP Solutions - 1.3","body":"\u003cp\u003e\u003cb\u003eQ: \u003c/b\u003eDefine a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers. \u003c/p\u003e\r\n\u003cp\u003e\u003cb\u003eA:\u003c/b\u003e\u003c/p\u003e\r\n\u003cpre\u003e\u003ccode class=\"lisp\"\u003e(define (square x)\r\n (* x x))\r\n\r\n(define (sum-of-squares x y)\r\n (+ (square x) (square y)))\r\n\r\n(define (sum-of-two-largest-squares x y z)\r\n (cond ((and (\u003e x z) (\u003e y z))\r\n (sum-of-squares x y))\r\n ((and (\u003e y x) (\u003e z x))\r\n (sum-of-squares y z))\r\n (else (sum-of-squares x z))))\r\n\r\n; Tests\r\n(display (sum-of-two-largest-squares 3 8 4))\r\n(newline)\r\n(display (sum-of-two-largest-squares 8 4 3))\r\n(newline)\r\n(display (sum-of-two-largest-squares 4 3 8))\r\n(newline)\r\n(display (sum-of-two-largest-squares 3 8 4))\r\n(newline)\r\n(display (sum-of-two-largest-squares 3 3 1))\r\n(newline)\r\n(display (sum-of-two-largest-squares 1 3 3))\r\n(newline)\r\n(display (sum-of-two-largest-squares 3 1 3))\r\n(newline)\r\n(display (sum-of-two-largest-squares 3 3 3))\r\n(newline)\r\n\u003c/code\u003e\u003c/pre\u003e\r\n\u003cpre\u003e\u003ccode class=\"c++\"\u003e80\r\n80\r\n80\r\n80\r\n18\r\n18\r\n18\r\n18\r\n\u003c/code\u003e\u003c/pre\u003e","publication_date":"2018-06-19T00:00:00.000Z","created_at":"2018-06-11T21:30:13.000Z","updated_at":"2018-06-19T00:20:37.000Z","user_id":1,"rating":null},"tags":"\u003ca class=\"changeable-title\" href=\"/q?tag=sicp\"\u003esicp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=programming\"\u003eprogramming\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=answers\"\u003eanswers\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=lisp\"\u003elisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=scheme\"\u003escheme\u003c/a\u003e"}

SICP Solutions - 1.3

Q: Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers.

A:

(define (square x)
  (* x x))

(define (sum-of-squares x y)
  (+ (square x) (square y)))

(define (sum-of-two-largest-squares x y z)
  (cond ((and (> x z) (> y z))
         (sum-of-squares x y))
        ((and (> y x) (> z x))
         (sum-of-squares y z))
        (else (sum-of-squares x z))))

; Tests
(display (sum-of-two-largest-squares 3 8 4))
(newline)
(display (sum-of-two-largest-squares 8 4 3))
(newline)
(display (sum-of-two-largest-squares 4 3 8))
(newline)
(display (sum-of-two-largest-squares 3 8 4))
(newline)
(display (sum-of-two-largest-squares 3 3 1))
(newline)
(display (sum-of-two-largest-squares 1 3 3))
(newline)
(display (sum-of-two-largest-squares 3 1 3))
(newline)
(display (sum-of-two-largest-squares 3 3 3))
(newline)
80
80
80
80
18
18
18
18
{"content":{"id":75,"title":"SICP Solutions - 1.2","body":"\u003cbr\u003e\r\n\u003cp\u003e\u003cb\u003eQ:\u003c/b\u003e Translate the following expression into prefix form:\u003c/p\u003e\r\n\u003cbr\u003e\r\n\u003cimg src=\"/uploads/36.gif\" class=\"card-image\"\u003e\r\n\u003cp\u003e\u003cb\u003eA:\u003c/b\u003e\u003c/p\u003e\r\n\u003cpre\u003e\u003ccode class=\"lisp\"\u003e(/ (+ 5 4 (- 2 3 (+ 6 4/5)))\r\n (* 3 (- 6 2) (- 2 7)))\u003c/code\u003e\u003c/pre\u003e\r\n\u003cbr\u003e\r\n\u003cp\u003e(gives \u003ci\u003e-1/50\u003c/i\u003e on \u003ca href=\"https://www.gnu.org/software/guile/\" target=\"_blank\"\u003eGuile\u003c/a\u003e and \u003ci\u003e-0.02\u003c/i\u003e on \u003ca href=\"https://www.call-cc.org/\" target=\"_blank\"\u003eChicken\u003c/a\u003e)\u003c/p\u003e","publication_date":"2018-06-18T00:00:00.000Z","created_at":"2018-06-08T23:45:15.000Z","updated_at":"2018-09-28T03:20:10.000Z","user_id":1,"rating":null},"tags":"\u003ca class=\"changeable-title\" href=\"/q?tag=sicp\"\u003esicp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=programming\"\u003eprogramming\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=answers\"\u003eanswers\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=lisp\"\u003elisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=scheme\"\u003escheme\u003c/a\u003e"}

SICP Solutions - 1.2


Q: Translate the following expression into prefix form:


A:

(/ (+ 5 4 (- 2 3 (+ 6 4/5)))
   (* 3 (- 6 2) (- 2 7)))

(gives -1/50 on Guile and -0.02 on Chicken)

{"content":{"id":49,"title":"SICP Solutions - 1.1","body":"\u003cp\u003e\r\n\u003cb\u003eQ:\u003c/b\u003e Below is a sequence of expressions. What is the result printed by the interpreter in response to each expression? Assume that the sequence is to be evaluated in the order in which it is presented.\r\n\u003c/p\u003e\r\n\u003cp\u003e\u003cb\u003eA:\u003c/b\u003e\u003c/p\u003e\r\n\u003cpre\u003e\u003ccode class=\"lisp all\"\u003e\u003e 10\r\n10\r\n\u003e (+ 5 3 4)\r\n12\r\n\u003e (- 9 1)\r\n8\r\n\u003e (/ 6 2)\r\n3\r\n\u003e (+ (* 2 4) (- 4 6))\r\n6\r\n\u003e (define a 3)\r\n\u003e (define b (+ a 1))\r\n\u003e (+ a b (* a b))\r\n19\r\n\u003e (= a b)\r\n#f\r\n\u003e (if (and (\u003e b a) (\u003c b (* a b)))\r\n b\r\n a)\r\n4\r\n\u003e (cond ((= a 4) 6)\r\n ((= b 4) (+ 6 7 a))\r\n (else 25))\r\n16\r\n\u003e (+ 2 (if (\u003e b a) b a))\r\n6\r\n\u003e (* (cond ((\u003e a b) a)\r\n ((\u003c a b) b)\r\n (else -1))\r\n (+ a 1))\r\n16\r\n\u003c/pre\u003e\u003c/code\u003e","publication_date":"2018-06-05T12:00:00.000Z","created_at":"2018-06-04T01:30:17.000Z","updated_at":"2018-06-18T16:35:54.000Z","user_id":1,"rating":null},"tags":"\u003ca class=\"changeable-title\" href=\"/q?tag=sicp\"\u003esicp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=programming\"\u003eprogramming\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=answers\"\u003eanswers\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=lisp\"\u003elisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=scheme\"\u003escheme\u003c/a\u003e"}

SICP Solutions - 1.1

Q: Below is a sequence of expressions. What is the result printed by the interpreter in response to each expression? Assume that the sequence is to be evaluated in the order in which it is presented.

A:

> 10
10
> (+ 5 3 4)
12
> (- 9 1)
8
> (/ 6 2)
3
> (+ (* 2 4) (- 4 6))
6
> (define a 3)
> (define b (+ a 1))
> (+ 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))
16
> (+ 2 (if (> b a) b a))
6
> (* (cond ((> a b) a)
           ((< a b) b)
           (else -1))
     (+ a 1))
16