Skip to content

Instantly share code, notes, and snippets.

View manishym's full-sized avatar

Manish M Yathnalli manishym

View GitHub Profile
@manishym
manishym / normal-order-applicative-order.scm
Created December 17, 2011 03:46
Normal order and applicative order - Scheme
(define (p) (p)) ;; P is a recursive function which does not end.
(defun (text x y))
(if (= x 0)
0
y))
(test 0 (p))
@manishym
manishym / normal-order-applicative-order.lisp
Created December 17, 2011 04:35
Normal order and applicative order - CL
; Ex 1.5 Normal order vs applicative order
(defun p ()
(p))
(defun test (x y)
(if (= x 0)
y
(p)))
@manishym
manishym / sicp3-newtons-method.lisp
Created December 18, 2011 15:27
Square root by newtons method
(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 ))
(eps ()
@manishym
manishym / gist:1493729
Created December 18, 2011 15:40
if as a normal function in lisp
(define (new-if predicate then-clause else-clause)
(cond (predicate then-clause)
(else else-clause)))
(define (p) (p))
(define (new-test x)
(new-if (= x 0)
1
(p)))
@manishym
manishym / gist:1493736
Created December 18, 2011 15:43
Normal if and infinitely recursive function
(define (p) (p))
(define (test x)
(if (= x 0)
1
(p)))
@manishym
manishym / gist:1493750
Created December 18, 2011 15:49
Square root using new-if
(define (new-if predicate then-clause else-clause)
(cond (predicate then-clause)
(else else-clause)))
(define (sqrt-iter guess x)
(new-if (good-enough? guess x)
guess
(sqrt-iter (improve guess x)
x)))
@manishym
manishym / sqrt-newtons-method.lisp
Created December 22, 2011 04:20
Square root by newtons method
(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))))
@manishym
manishym / sqrt-newtons-method-improved.lisp
Created December 22, 2011 04:58
Square root by newtons method - improved
(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)
@manishym
manishym / cbrt-newtons-method.lisp
Created December 22, 2011 06:02
Cube root by newton's method
(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)
@manishym
manishym / sqrt-newtons-method.c
Created December 22, 2011 06:30
Square root by newtons method in c
#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) ;