0
#lang eopl

  (define (expo  base n  )
         (cond( (or  (= base 1) (= n 0) ) 1)
                  (else ( (* base (expo(base (- n 1))) )   ) )))

-> (enter! "expo.rkt")
"expo.rkt"> (expo (2 1) )
; application: not a procedure;
;  expected a procedure that can be applied to arguments
;   given: 2
; [,bt for context]

I am trying to create a simple recursive exponentiation, but I get the error above. Code is self-explanatory. I am a newbie in Racket programming. I have been reading the manuals, but can't find my error. Supposedly, it shows the error because my function returns a void and not a procedure, but I don't see why it would return void. I am returning 1 or a computation. Help, please :/

Óscar López
  • 232,561
  • 37
  • 312
  • 386
knowKnothing
  • 45
  • 2
  • 10
  • 2
    Perhaps DrRacket would provide better highlighting over the context of the error? I suspect that if this were run in DrRacket, then the `(2 1)` part would be highlighted to better localize the source of the error. – dyoo Sep 19 '13 at 03:43
  • Also you may want to work on idomatic formating. Don't have space between parens. The should be a space before an open paren, but not a close paren. Usually you indent two spaces when nesting an expression in the expression above. Cond clauses should all be aligned vertically, as should any expression whose parameters span several lines. This helps people read your code, especially when it starts getting more complex – WorBlux Sep 19 '13 at 14:32

3 Answers3

3

You have several misplaced parentheses. This should solve the errors:

(define (expo base n)
  (cond ((or (= base 1) (= n 0)) 1)
        (else (* base (expo base (- n 1))))))

And this is how you call it:

(expo 2 3)
=> 8

For the record: in Scheme a pair of parentheses means function application, so when you write (2 3) the interpreter thinks that 2 is a function and 3 is its argument ... clearly that won't work.

So you'll have to be very careful where you put those (), they make all the difference in the world! To make things easier use a good IDE with bracket matching and nice syntax coloring, and be extra tidy with the indentation. As suggested by @dyoo in the comments, DrRacket is an excellent choice.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • Thank you for the information. I will study and keep in mind everyone's advice on how to start with Scheme/Racket programming. – knowKnothing Sep 19 '13 at 19:14
  • Wow, this is a new on for me. I thought I wouldn't even have credits/rep whatever to do that. I have zero rep. – knowKnothing Sep 20 '13 at 00:35
0

When you call the function, you want to write

(expo 2 1)

rather than

(expo (2 1))

Same in the definition of the recursive function's definition.

In addition, this part have double brackets, which is unnecessary.

( (* base (expo(base (- n 1))) )
ozooxo
  • 775
  • 5
  • 3
  • 3
    "Unnecessary" seems to imply "optional". That is not the case. In Scheme, any extra brackets (beyond what the syntax requires) will make the code wrong. Brackets either must be there, or must not be there; there is no middle ground. ;-) – C. K. Young Sep 19 '13 at 03:29
0

The cond syntactic form is best used when a) you have more than two clauses or b) you have a sequence of commands/expressions to perform for one or more clauses. Those two cases don't apply to your code. Thus, you'd have clearer code (easier to understand; easier to get correct) using if as such:

(define (expo base n)
  (if (or (= base 1) (= n 0))
      1
      (* base (expo base (- n 1)))))

Also, study the spacing and indentation of some 'good' code; it will help your understanding tremendously.

GoZoner
  • 67,920
  • 20
  • 95
  • 145
  • 1
    Some style guides prefer cond as it's easier to modify. For instance it's trivial to put the fast-expo rule into the cond ((even? n) (expo (*b b) (/ n 2))) , but running it into the if is tricker. – WorBlux Sep 19 '13 at 14:40