mit-scheme-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [MIT-Scheme-users] SICP ex. 1.16


From: Broseph
Subject: Re: [MIT-Scheme-users] SICP ex. 1.16
Date: Thu, 16 Aug 2012 09:00:28 -0700 (PDT)

(define (expt x n)
  (exp-it x n 1))

(define (exp-it x n odds)
  (cond ((= n 0) 1)
           ((= n 1) (* x odds))
           ((even? n) (exp-it (* x x) (/ n 2) odds))
           ((odd? n) (exp-it x (- n 1) (* odds x)))
           (else "ERROR: non integral n -- exp-it")))
 

antiloquax wrote:
> 
> Hi, I am learning Scheme using SCIP and R. Kent Dybvig's "The Scheme
> Programming Language".
> I am stuck on exercise 1.16 in Abelson and Sussman.
> The task is to "design a procedure that evolves and iterative
> exponentiation process that uses successive squaring".
> 
> Well, I can't seem to get it working.
> The code I have tried looks something like this:
> [code]
> ; iterative process for fast exponent
> ; NOT WORKING!!!
> 
> (define (expt b p)
>   (exp-it b p 1))
> 
> (define (exp-it b p a)
>   (cond 
>     ((= p 0) a)
>     ((even? p) (exp-it b (/ p 2) (* a (square b)))))
>     (else (exp-it b (- p 1) (* b a)))))
> [/code]
> 
> 

-- 
View this message in context: 
http://old.nabble.com/SICP-ex.-1.16-tp34305765p34307557.html
Sent from the Gnu - MIT Scheme - Users mailing list archive at Nabble.com.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]