1
votes

The interpreter for Racket gives me errors

in my attempt to implement the recursive

function for Exercise 1.11:

#lang sicp

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

(f 2)
(f 5)

The errors given by the Racket intrepreter are:

 2
 application: not a procedure;
 expected a procedure that can be applied to arguments
 given: 4
 arguments...: [none]
 context...:

/Users/tanveersalim/Desktop/Git/EPI/EPI/Functional/SICP/chapter_1/exercise_1-11.rkt: [running body]

1
You're calling f incorrectly in the procedure definition; the parenthesis goes before the f not after. - Isaac Kleinman
Your advice worked! - Tanveer Salim

1 Answers

2
votes

As others noted, you're calling f incorrectly

Change f((- n 1)) (and other similar instances) to (f (- n 1))

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

(f 2) ; 2
(f 5) ; 25