Permalink
Browse files

Merge branch 'master' of github.com:manishym/SICP

  • Loading branch information...
manishym committed Dec 23, 2011
2 parents 00a6b97 + 54a3ae5 commit b7b45312abaa767d33af50124a5a4bbe577fff17
View
@@ -0,0 +1 @@
+
Binary file not shown.
@@ -0,0 +1,47 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <math.h>
+
+#define EPS 0.0001
+float average (float x, float y) ;
+float improve (float guess, float x) ;
+int good_enough (float guess, float x) ;
+float my_square_root (float x, float guess) ;
+float square (float x) ;
+int main (int argc, char* argv[]) {
+
+ float x = atof(argv[1]) ;
+ if (!x)
+ return -1 ;
+ printf ("%f\n", my_square_root(x, 1.0)) ;
+ return 0;
+}
+
+float my_square_root (float x, float guess) {
+ if (good_enough(guess, x) ) {
+ return guess ;
+ }
+ else {
+ return my_square_root (x, improve (guess, x)) ;
+ }
+}
+
+int good_enough (float guess, float x) {
+ if (fabsf ( square (guess) - x) < EPS)
+ return 1 ;
+ else
+ return 0 ;
+}
+
+float improve (float guess, float x) {
+ float improved ;
+ return improved = average (guess, x/guess) ;
+}
+
+float average (float x, float y) {
+ return ( x + y ) / 2 ;
+}
+float square (float x)
+{
+ return (x * x) ;
+}
@@ -0,0 +1,27 @@
+;;; Begining attempt three of SICP.
+
+(defun hello-world1 (str)
+ (format t "Hello ~A" str))
+
+;; Square roots by newton's method
+; to get the square root of a number, take a guess.
+; then improve guess by averaging guess with x/guess till the
+; guess is close to x/guess
+(defparameter eps 0.000244)
+
+;; This is original implementation of square root
+(defun square-root (x)
+ (labels (
+ (try (guess)
+ (if (good-enough? guess)
+ guess
+ (try (improve guess))))
+ (good-enough? (guess)
+ (if (< (abs (- guess (/ x guess))) eps) guess nil ))
+ (improve (guess)
+ (average guess (/ x guess))))
+ (try 1.0)))
+
+
+(defun average (a b)
+ (/ (+ a b) 2))
@@ -0,0 +1,88 @@
+;;; Exercises in SICP book
+
+;; A simple hello world to test
+
+(defun hello-world (str)
+ (format t "Hello ~A" str))
+
+;Ex 1.2
+
+(/ (+ 5 4 (- 2 (- 3 (+ 6 (/ 4 5))))) (* 3 (- 6 2) (- 2 7)))
+
+;Ex 1.3
+(defun sum-big-square (a b c)
+ (sum-square (big a b) (med a b c)))
+
+(defun sum-square (a b)
+ (+ (square a)
+ (square b)))
+
+(defun square (x)
+ (* x x))
+
+(defun big (a b)
+ (if (> a b)
+ a
+ b))
+
+(defun med (a b c)
+ (if (= (big a b) (big b c))
+ (big a c)
+ (big b c)))
+
+
+;Ex 1.4
+; This example shows why scheme is elegant compared to common lisp.
+; scheme implementation would be simply
+; (defun a-p-a-p (if (> b 0) + -) a b)
+; no funcall no ugly sharp-quotes
+; common lisp is supposed to have more power when writing macros
+; because of this saperation of function and data name spaces.
+(defun a-plus-abs-b (a b)
+ (funcall
+ (if (> b 0)
+ #'+
+ #'-) a b))
+
+
+;Ex 1.5 Normal order vs applicative order
+
+(defun p ()
+ (p))
+(defun test (x y)
+ (if (= x 0)
+ y
+ (p)))
+
+;exercise 1.7 newtons method for square root.
+
+(defun my-sqrt (x)
+ (labels (
+ (try (guess)
+ (let ((improved (improve guess)))
+ (if (good-enough? guess improved)
+ improved
+ (try improved))))
+ (improve (guess)
+ (average guess (/ x guess)))
+ (good-enough? ( guess improved)
+ (if (< (abs (- 1 (/ guess improved))) eps )
+ improved
+ nil)))
+ (try 1.0)))
+;; Newtons method for cuberoot x/(square y) + 2 y / 3
+; all i need to change is improve
+(defun my-cube-rt (x)
+ (labels (
+ (try (guess)
+ (let ((improved (improve guess)))
+ (if (good-enough? guess improved)
+ improved
+ (try improved))))
+ (improve (guess)
+ (/ (+ (/ x (square guess)) (* 2 guess)) 3))
+ (good-enough? ( guess improved)
+ (if (< (abs (- 1 (/ guess improved))) eps )
+ improved
+ nil)))
+ (try 1.0)))
View
@@ -0,0 +1,5 @@
+;;;; package.lisp
+
+(defpackage #:sicp3
+ (:use #:cl))
+
View
@@ -0,0 +1,9 @@
+;;;; sicp3.asd
+
+(asdf:defsystem #:sicp3
+ :serial t
+ :components ((:file "chapter1/examples")
+ (:file "chapter1/exercises")
+ (:file "package")
+ (:file "sicp3")))
+
View
@@ -0,0 +1,6 @@
+;;;; sicp3.lisp
+
+(in-package #:sicp3)
+
+;;; "sicp3" goes here. Hacks and glory await!
+

0 comments on commit b7b4531

Please sign in to comment.