Skip to content

Instantly share code, notes, and snippets.

@rsayers
rsayers / gist:935901
Created April 22, 2011 02:35
Dynamic Programming
(define lambda-cache (make-hash-table))
(define (add-to-cache args)
(let ((fn (car args))
(a (cadr args)))
(hash-table-set! lambda-cache args (fn a))
(hash-table-ref lambda-cache args)))
(define (dynamic-call . args)
(define (compile io-port)
(display "(display \"")
(let loop ((c (read-char io-port))
(compile-state "normal"))
(if (not (eof-object? c))
(begin
(loop (read-char io-port) (handle-token c compile-state)))
(if (equal? compile-state "normal")
(begin
(display "\")")
#include <stdio.h>
int fib(n) {
if ((n == 0)||(n == 1)) {
return n;
} else {
return fib(n-1) + fib(n-2);
}
}
#! /usr/bin/mzscheme
#lang scheme
(define (fib n)
(if (or (= 0 n) (= 1 n)) n
(+ (fib (- n 1)) (fib (- n 2)))))
(let runfib ((i 0))
(unless (= i 36)
(display (string-append "n = " (number->string i) " => " (number->string (fib i)) "\n"))
(runfib (+ i 1))))
(defun fib (n)
(if(or (= 0 n) (= 1 n)) n
(+ (fib (- n 1)) (fib (- n 2)))))
(defun start ()
(loop for x from 0 to 35 collect
(format t "n = ~d => ~d~%" x (fib x))))
(start)
(quit)
public class fib {
public static int fib(int n) {
if (n <= 1) {return n;}
return fib(n-1) + fib(n-2);
}
public static void main(String[] args) {
for (int i = 0; i < 36; i++)
System.out.println("n = " + i + " => " + fib(i));
def fib(n)
if n == 0 || n == 1
n
else
fib(n - 1) + fib(n - 2)
end
end
if $0 == __FILE__
36.times do |i|
#!/usr/local/bin/ruby
require "open-uri"
id=ARGV[0]
if !id then
puts "No id given"
else
begin
puts open("http://gist.github.com/#{id}.txt").read
rescue
#!/usr/bin/perl
package GopherScript;
sub new
{
my $class = shift;
my $self = {
hostname => shift,
port => shift,
mode => "dir",
class GopherClient
def parseurl(url)
opts=url.sub('gopher://','').split('/') # split the url by slashes
host=opts.shift # the first element will be our host
type=opts.shift # type is second
if !@types.has_key?(type) then # check to see if a type was provided
opts.unshift(type) # if not, then its part of the path, put it back
puts "No type given, assuming 1" # tell them the error of their ways
type="1" # and assume its a directory
end