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

3 comments:

weima said...

Hi Rahul,
check out my blog man.

hahaha said...

Rahul .. this is cool. Good going!

Rahul said...

As Vimal pointed out its O(n) rather than O(log n).
The correct solution is
(defun f (base counter limit product mult) (if (< (* 2 counter) limit) (f base (* 2 counter) limit (* product product) mult) (if (= counter limit) (* product

mult) (f base 1 (- limit counter) base (* product mult)))))


It was able to calculate as 9999 pow 9999 successfully.
(f 9999 1 9999 1 9999)