Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

SICP exercise 1.11

Name: Anonymous 2009-03-05 10:24

Exercise 1.11. A function f is defined by the rule that f(n) = n if n<3 and f(n) = f(n - 1) + 2f(n - 2) + 3f(n -
3) if n> 3. Write a procedure that computes f by means of a recursive process.


(define (f n)
    (cond ((< n 3) n)
    (else ((+ (f (- n 1))
            (*
                (f (- n 2)) 2)
            (*
                (f (- n 3)) 3))))))


It gives me "The object 4 is not applicable".
Can you guys help me?

Name: Anonymous 2009-03-05 10:24

>>1
READ SICP

Name: Anonymous 2009-03-05 10:43

dicks> let fun n = if n < 3 then n else fun (n - 1) + 2*fun (n - 2) + 3*fun (n - 3)
dicks> fun 4
11

Name: Anonymous 2009-03-05 13:59

(define (f n)
    (cond
        ((< n 3)
            n
        )
        (else
            (+
                (   f (- n 1))
                (* (f (- n 2))
                    2
                )
                (* (f (- n 3))
                    3
                )
            )
        )
    )
)

Name: Anonymous 2009-03-05 15:57

>>4
Good Sussman! Where on earth did you learn how to indent? Python?

Name: Anonymous 2009-03-05 16:31

Since you're reading your SICP, I'll help him!

When you wrap something in parentheses it will be evaluated with the first symbol as a function and the following symbols as parameters.
So (+ 5 2) is the function + with arguments 5 and 2.
But what happens if you write ((+ 5 2))?  It thinks there is a nested function, so it evaluates the inner list (+ 5 2) first, and then it tries to evaluate the result inserted into the place where (+ 5 2) was, i.e. (7).  Since there's no function called 7 it complains.

If that confuses you, you may want to review the substitution model detailed in section 1.1.5.  The video lecture on the same subject may also be helpful, it's 1b and can be viewed or downloaded here <http://groups.csail.mit.edu/mac/classes/6.001/abelson-sussman-lectures/>;

Name: Anonymous 2009-03-05 16:32

>>6
Actually, whenever you wrap something in parenthesis, it actually parenthessis

Name: Anonymous 2009-03-05 16:34

Shiichan is pretty terrible.  Here is the correct URL: http://groups.csail.mit.edu/mac/classes/6.001/abelson-sussman-lectures/

Name: Anonymous 2009-03-05 18:50

>>8
I think you are pretty terrible!

downloaded here <http://groups.csail.mit.edu/mac/classes/6.001/abelson-sussman-lectures/>;;
Works fine for me. Check your SIBBC, section 3.2, "Creating URLs"

Name: Anonymous 2009-03-05 18:50

.... What the fuck.

Name: Anonymous 2009-03-05 22:30

Also it might be worth mentioning that (cond ...) is a special form, and that its arguments, even though they are surrounded by parentheses, aren't evaluated as functions.  This is because (cond ...) actually manipulates its arguments prior to the evaluation pass.
That is to say, the bolded sets of parentheses in the below snippet are syntactic sugar, they just make things look nice and neat by keeping the condition and the result easily identifiable with one another.
(cond((> x 5) 1)((< x 3) 2)(else 12))

Name: Anonymous 2009-03-05 22:32

I did not know that the [code] tag was so greedy in removing whitespace.  orz

Name: Shitchan Meme Fan 2009-03-05 22:32

Name: Anonymous 2009-03-05 22:33

Name: Anonymous 2009-03-05 22:33

Well, I don't get how you guys did it wrong.

Name: Anonymous 2009-08-17 0:09

Lain.

Name: ​​​​​​​​​​ 2010-09-07 15:15

Name: Anonymous 2010-12-22 3:27

Name: Anonymous 2011-02-03 7:39

Name: Anonymous 2011-02-17 20:09

that's cool and all, but check my doubles over thereNewer Posts
Don't change these.
Name: Email:
Entire Thread Thread List