#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 :/
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.