The following are the results from defining and evaluating the Ackermann's function on various arguments.
(define (A x y)
(cond ((= y 0) 0)
((= x 0) (* 2 y))
((= y 1) 2)
(else (A (- x 1)
(A x (- y 1))))))
; A
# This script is designed to work with ubuntu 16.04 LTS | |
# ensure system is updated and has basic build tools | |
sudo apt-get update | |
sudo apt-get --assume-yes upgrade | |
sudo apt-get --assume-yes install tmux build-essential gcc g++ make binutils git | |
sudo apt-get --assume-yes install software-properties-common | |
# download and install GPU drivers | |
wget "http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_8.0.44-1_amd64.deb" -O "cuda-repo-ubuntu1604_8.0.44-1_amd64.deb" |
#lang planet neil/sicp | |
(define (append x y) | |
(if (null? x) | |
y | |
(cons (car x) (append (cdr x) y)))) | |
(define (last-pair x) | |
(if (null? (cdr x)) | |
x |
import pygraphviz as pgv | |
import networkx as nx | |
nodeCount = 1 | |
G = nx.DiGraph() | |
Ranks = {} | |
def countChange(amount): | |
global G | |
global Ranks |
;; Rows and columns start with index 1 | |
(define (pascal row col) | |
(cond ((or (= col 1) (= col row)) 1) | |
(else (+ (pascal (- row 1) col) (pascal (- row 1) (- col 1)))))) | |
;; Test cases | |
(pascal 1 1) | |
; 1 | |
(pascal 13 7) | |
; 924 |
;;; Original function | |
(define (f n) | |
(cond ((< n 3) n) | |
(else (+ (f (- n 1)) (* 2 (f (- n 2))) (* 3 (f (- n 3))))))) | |
; f | |
(f 0) | |
; 0 | |
(f 1) | |
; 1 |
The following are the results from defining and evaluating the Ackermann's function on various arguments.
(define (A x y)
(cond ((= y 0) 0)
((= x 0) (* 2 y))
((= y 1) 2)
(else (A (- x 1)
(A x (- y 1))))))
; A
Expansion of (+ 4 5)
using first definition
(define (+ a b)
(if (= a 0)
b
(inc (+ (dec a) b))))
(+ 4 5)
(inc (+ (dec 4) 5))
(define (cbrt-iter guess prev-guess x) | |
(if (good-enough? guess prev-guess) | |
guess | |
(sqrt-iter (improve guess x) | |
guess | |
x))) | |
; cbrt-iter | |
(define (improve guess x) | |
(/ (+ (/ x (square guess)) (* 2 guess)) 3)) |
The good-enough?
function is not so effective when we use small numbers as arguments in the function.
For example, when we want to find the square root of 0.0001 which is 0.01,
(sqrt 0.0001)
;.03230844833048122
(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)))