This is for excercise 1.16:

(defun fast_expt (b counter limit product) (if (= counter limit) product (if (leq (* counter 2) limit) (fast_expt b (* counter 2) limit (* product product)) (fast_expt b (+ counter 1) limit (* product b)))))

FAST_EXPT

COMMON-LISP-USER> (setf res 2)

2

COMMON-LISP-USER> (fast_expt 2 1 7 res)

128

COMMON-LISP-USER> (fast_expt 2 1 8 res)

256

COMMON-LISP-USER> (fast_expt 2 1 9 res)

512

COMMON-LISP-USER> (fast_expt 2 1 10 res)

1024

COMMON-LISP-USER> (fast_expt 2 1 11 res)

2048

## Sunday, June 22, 2008

## Saturday, June 21, 2008

### SICP study

From today I'll be putting the solutions of SICP on the web (which I took some time to finish). This is done to motivate myself to finish the SICP faster.

Also the solution will be in COMMON LISP rather than SCHEME since I want to learn the LISP also.

Till now I have finished till excersise 1.16 of SICP of which I have only left excersise 1.5 and 1.14( I never understood what is nickel etc ?).

Till now I found the excersise 1.11 coolest. Well it taught me what can be achieved by recursion also can be passed through state varaiables as a function (aka procedure it takes time to get out of C/C++ mindset :) ).

Probably I already knew that but the way it explains I have never seen in any book.

You can review the solutions and add the comments:

Pascal Triangle: Excerise 1.12

(defun pascal (rowName columnName) ( if (or (= rowName 1) (= columnName rowName)) 1 (if (= columnName 1) 1 (+ (pascal (- rowName 1) (- columnName 1)) (pascal (- rowName 1) columnName)))))

Excersise: 1.11 (iterative process only as recursive is straight forward)

(defun f (counter limit result fn1 fn2 fn3) (if (= counter limit) result (f (+ counter 1) limit (+ fn1 (+ (* 2 fn2) (* 3 fn3))) (+ fn1 (+ (* 2 fn2) (* 3 fn3))) fn1 fn2)))

COMMON-LISP-USER> (setf res1 0)

COMMON-LISP-USER> (f 3 4 res1 3 2 1)

10

Also the solution will be in COMMON LISP rather than SCHEME since I want to learn the LISP also.

Till now I have finished till excersise 1.16 of SICP of which I have only left excersise 1.5 and 1.14( I never understood what is nickel etc ?).

Till now I found the excersise 1.11 coolest. Well it taught me what can be achieved by recursion also can be passed through state varaiables as a function (aka procedure it takes time to get out of C/C++ mindset :) ).

Probably I already knew that but the way it explains I have never seen in any book.

You can review the solutions and add the comments:

Pascal Triangle: Excerise 1.12

(defun pascal (rowName columnName) ( if (or (= rowName 1) (= columnName rowName)) 1 (if (= columnName 1) 1 (+ (pascal (- rowName 1) (- columnName 1)) (pascal (- rowName 1) columnName)))))

Excersise: 1.11 (iterative process only as recursive is straight forward)

(defun f (counter limit result fn1 fn2 fn3) (if (= counter limit) result (f (+ counter 1) limit (+ fn1 (+ (* 2 fn2) (* 3 fn3))) (+ fn1 (+ (* 2 fn2) (* 3 fn3))) fn1 fn2)))

COMMON-LISP-USER> (setf res1 0)

COMMON-LISP-USER> (f 3 4 res1 3 2 1)

10

Subscribe to:
Posts (Atom)