🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Introduction and Chapter 1

Started by
84 comments, last by bodchook 15 years, 7 months ago
Well, for my humble first post here's my solution to exercise 1.3 (sum of the squares of biggest two out of three parameters).
(define (x1_3 x y z)  (- (+ (* x x) (* y y) (* z z))     (expt (min x y z) 2)));Version 2 to appease Ezbez :)(define (x1_3 x y z)  (define (** x) (* x x))  (+ (** x) (** y) (** z)     (- (** (min x y z)))))


[Edited by - Diodor on December 22, 2006 1:42:42 PM]
Advertisement
Just for practice, Diodor, I think it might be good to write a square function that, well, squares something. You're doing that four times in your code, so it would sort of be worth it.
I'm doing the really early exercises, and I want to check my answers, as it were.







Watch for spoilers! My answers ahead! TURN BACK NOW!















1.1: 10,12,8,3,6,3,4,19,0,4,16,6,16
1.2:
(/ (+ 5 4 (- 2 (- 3 (+ 6 (/ 1 3) ) ) ) ) (* 3 (- 6 2) (- 2 7)))

Yeilded -(43)/(180). Seems wierd to me. Reason for this post.
1.3:
(define     (square-two-bigger-add x y z)     (cond      ((and (> x z) (> y z) )      (+ (* x x) (* y y)))      ((and (> y x) (> z x) )      (+ (* y y) (* z z)))      ((and (> x y) (> z y))      (+ (* y y) (* z z)))      ))

1.4: If B is positive, add a and b. If not, subtract b from a.
1.5: An applicative-order evaluation returns 0, and a normal-order evaluation never ends. Oh, and because it tries to expand p infinitly, whereas AO never evaluates p.

So, am I right?

And do these forums have a [ SPOILER ] tag? And why doesn't the [ SOURCE ] tag recognize lang="scheme" as correct?

[Edited by - EmrldDrgn on December 24, 2006 1:57:23 AM]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~I program in C++, on MSVC++ '05 Express, and on Windows. Most of my programs are also for windows.
Quote: Original post by EmrldDrgn
1.2:
*** Source Snippet Removed ***
Yeilded -(43)/(180). Seems wierd to me. Reason for this post.

I think the fraction is 4/5, not 1/3.

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

gives me an answer of -(37)/(150), which is correct. The interpreter is just giving the answer as an exact fraction. If you evaluate -37/150 on a calculator or enter (exact->inexact -37/150) into Dr Scheme, you will get the result -0.246666666666667.


Quote: 1.3:
*** Source Snippet Removed ***
This is basically the same as my solution, but I broke it down into smaller procedures:

(define (square x) (* x x))(define (sum-of-squares x y)  (+ (square x) (square y)))(define (sum-of-squares-of-largest-two a b c)  (cond ((and (> a c) (> b c)) (sum-of-squares a b))        ((and (> a b) (> c b)) (sum-of-squares a c))        (else (sum-of-squares b c))))


Quote: 1.4: If B is positive, add a and b. If not, subtract b from a.
Yes.

Quote: 1.5: An applicative-order evaluation returns 0, and a normal-order evaluation never ends. Oh, and because it tries to expand p infinitly, whereas AO never evaluates p.
Yes.
Exercise 1.31a, iterative and variant using closures
(define (1+ x) ( + x 1))(define (1- x) (- x 1))(define (identity x) x)(define (product a step b factor)  (define (pgo x ret)    (if (> x b)        ret        (pgo (step x)             (* ret (factor x)))))  (pgo a 1))(define (factorial n)  (product 1 1+ n identity))(define (pi n)  (* 4.0 (product 2 1+ (1+ n)                  (lambda (x)                    (/ (+ x (modulo x 2))                       (+ x (modulo (1+ x) 2))))))); using closures to create counters for 1 1 3 3 5 5 ... type streams(define (make-2-counter start)  (let ((plus -1))    (lambda ()       (set! plus (+ plus 1))      (+ start plus (- (modulo plus 2))))))(define (pi n)  (let ((top (make-2-counter 2))        (bottom (make-2-counter 3)))    (top)    (* 4.0 (product 1 1+ n (lambda (x)  (/ (top) (bottom)))))))


Common Lisp for exercise 1.19 . I hadn't encountered calculating Fibonnaci in log(n) time and the method seems applicable to other recursive formulas.
(defun fib (n)  (labels ((fibgo (a b n p q s)             (cond ((= 0 n)                    (list a b))                   ((oddp n)                    (fibgo (+ (* p a) (* q b))                        (+ (* q a) (* s b))                        (- n 1)                        p q s))                   ((evenp n)                    (fibgo a b (/ n 2)                        (+ (* p p) (* q q))                        (+ (* p q) (* q s))                        (+ (* q q) (* s s)))))))    (car (fibgo 1 1 n 1 1 0))))


[Edited by - Diodor on December 29, 2006 8:00:37 AM]
I have no idea what I'm doing wrong here.
(define (choice y)    (cond ((< 0) (-10))  //it highlights the (< 0)          (else 10)))> (choice 67)

. <: expects at least 2 arguments, given 1: 0

Beginner in Game Development?  Read here. And read here.

 

Quote: Original post by Alpha_ProgDes
I have no idea what I'm doing wrong here.
*** Source Snippet Removed ***
. <: expects at least 2 arguments, given 1: 0


The < operator takes at least two arguments. 'cond' has no way of knowing you're comparing with 'y'. (Using macros, you could define your own control structures, but I'll leave that for a later time (macros are not discussed by SICP, so if you're interested, you can always ask later)).

Also, don't put the -10 between parentheses. Parentheses are never optional in Scheme, they always mean "a list", and without quotation it will be interpreted as a function call (in this case, calling the 0-arity function '-10').

(define (choice y)    (cond ((< y 0) -10)          (else 10)))
Quote: Original post by SamLowry
Quote: Original post by Alpha_ProgDes
I have no idea what I'm doing wrong here.
*** Source Snippet Removed ***
. <: expects at least 2 arguments, given 1: 0


The < operator takes at least two arguments, and don't put the -10 between parentheses. Parentheses are never optional in Scheme, they always mean "a list", and without quotation it will be interpreted as a function call (in this case, calling the 0-arity function '-10').

(define (choice y)    (cond ((< y 0) -10)          (else 10)))

thanks you've answered two questions!

Beginner in Game Development?  Read here. And read here.

 

(define we 12)> (define us (+ we 2))> us14> (define we 20)> us14

i take it "we" is basically a new object every time i redefine it. something like:
void define (int*& object, int value){    if (object != NULL)    {       delete object;    }    object = new int;    *object = value;}


i'm using Dr. Scheme by the way.

Beginner in Game Development?  Read here. And read here.

 

Pretty much; when you redefine a name, you might as well have used a fresh name: the only difference is syntax. It helps to think of define just creating a convenient name for some value, and the value being substituted into the code whenever you use the name. Later you'll see set!, which does destructive assignment.

This topic is closed to new replies.

Advertisement