Pages

Wednesday, September 11, 2013

Power program in LISP

[1]Write LISP program and define function named power that takes two numeric arguments, n and m, that computes nth power of m (i.e., m^n).

Answer

Without Recursion
-------------------
(defun  pow( )
(setf  m(read))
(setf  n(read))
(setf  c 1)
(dotimes  (i n)

   (setf  c ( * c m))
)



Expln:

Every lisp program will start a symbol '(' .
1) Define a function named 'pow'
2) Read 'm' variable
ie; scanf  in C
3) Read 'n' variable
4) Initialise  c=1
5) For loop 
ie;  in C, for(i=0;i<n;i++)

'dotimes' only for  increment operations.
For decrementing......use  another method for 'For-loop'..

(loop for ...) refer this 

6) Set  c=c * m
In LISP,

We will use  prefix notation.
ie; why we using 

(*  c m )

No comments:

Post a Comment