Skip to content

Instantly share code, notes, and snippets.

View enigmaticape's full-sized avatar

Enigmatic Ape enigmaticape

View GitHub Profile
<?php
if( ($dir = session_save_path() ) == null ) {
$dir = sys_get_temp_dir();
}
var_dump( $dir );
if( $dir ) {
@enigmaticape
enigmaticape / sicp_ex_1.13.scm
Created November 4, 2012 13:22
Some code to go with an exploration of SICP exercise 1.13, a proof of closed form of Fibonacci function.
;; expt is three characters too long on the REPL
;; I use on my iPhone
(define (^ x n) (expt x n))
;; Golden Ratio, phi and the conjugate, psi
(define psi (/ (- 1 (sqrt 5)) 2))
(define phi (/ (+ 1 (sqrt 5)) 2))
;; Linear recursive Fib
(define (fib n)
@enigmaticape
enigmaticape / sicp_1_13_pyfib.py
Created November 4, 2012 15:07
Various python code to do Fibonacci related computations
#!/usr/bin/python
# This is the python script I used to generate
# the table that illustrates my rambling answer
# to SICP exercise 1.13
import math
# Golden ratio and conjugate
phi = (1 + math.sqrt( 5 ) ) / 2
@enigmaticape
enigmaticape / pascal-element.scm
Created November 4, 2012 16:31
Recursivley compute the elements of Pascal's triangle in Scheme. Answer to SICP exercise 1.12
;; Recursive function to compute the elements
;; of Pascal's triangle. Note the complete lack
;; of any sanity checks on the input. GIGO.
(define (pelem row col)
(cond((= col 0) 1)
((= col row) 1)
(else(+(pelem(- row 1)(- col 1))
(pelem(- row 1) col) ) )))
@enigmaticape
enigmaticape / sicp_ex_1.11.scm
Created November 4, 2012 17:50
Iterative function f(n) from SICP exercise 1.11
; f(n) = n if n < 3,
; f(n) = f(n - 1) + 2f(n - 2) + 3f(n - 3) if n > 3
;
; iterative (via tail recursion optimisation)
(define (F-iter a b c count)
(if (= count 0)
c
(F-iter (+ a (* 2 b) (* 3 c))
a
@enigmaticape
enigmaticape / recfib.scm
Created November 4, 2012 18:53
Recursive Fibonacci in Scheme, a snippet from SICP
(define (fib n)
(cond ((= n 0) 0)
((= n 1) 1)
(else (+ (fib (- n 1))
(fib (- n 2))))))
@enigmaticape
enigmaticape / iterfib.scm
Created November 4, 2012 18:55
Iterative (tail recursive) Fibonacci in Scheme (a snippet from SICP)
(define (fib n)
(fib-iter 1 0 n))
(define (fib-iter a b count)
(if (= count 0)
b
(fib-iter (+ a b) a (- count 1))))
@enigmaticape
enigmaticape / recf.scm
Created November 4, 2012 19:19
Recursive function f(n), for completeness, from SICP exercise 1.11
(define (F n)
(cond ( (< n 3) n)
( else (+ (F (- n 1))
(* (F (- n 2)) 2)
(* (F (- n 3)) 3)))))
@enigmaticape
enigmaticape / ackermann.scm
Created November 4, 2012 19:39
Ackermann's function, as given in SICP.
(define (A x y)
(cond ((= y 0) 0)
((= x 0) (* 2 y))
((= y 1) 2)
(else (A (- x 1)
(A x (- y 1))))))
@enigmaticape
enigmaticape / sicp_ex_1.10a.scm
Created November 5, 2012 08:24
(h)n = 2^(h-1) SICP Exercise 1.10
;; Recursively defined analogue of (h n) = (A 2 n)
(define (h-rec n)
(cond ((= n 0) 0)
((= n 1) 2)
(else (expt 2 (h-rec (- n 1))))))