Skip to content

Instantly share code, notes, and snippets.

@gbluma
gbluma / gradientDescent.hs
Last active December 14, 2015 22:28
A simple gradient descent algorithm in Haskell
-- Desc: A simple gradient descent algorithm in Haskell
-- Author: Garrett Bluma
-- Date: March 12, 2013
gradientDescent :: Double -> Double -> Double -> Double -> (Double -> Double) -> Double
gradientDescent goal step x x_prev f
| (abs(x-x_prev) > goal) = gradientDescent goal step (x - step * (f x)) x f
| otherwise = x
main = do
@gbluma
gbluma / half-interval.hs
Created March 18, 2013 04:01
A direct port of SICP half-interval-method.
-- Desc: A direct port of SICP half-interval-method.
-- Author: Garrett Bluma
-- Date: March 17, 2013
average a b = (a + b) / 2.0
close_enough a b = abs (b - a) < 0.001
search f neg_point pos_point =
@gbluma
gbluma / racket-arrow.rkt
Created March 25, 2013 17:33
Draws two nodes with text in them and an arrow between them.
#lang slideshow
;; a function to draw a nice node
(define (node name prob)
(rb-superimpose ; attach to right-bottom of box
(text (string-append prob " ") ; add label for failure probability
null 9 0)
(cc-superimpose ; center pictures on top of eachother
(text name) ; output text
(rectangle 100 35)))) ; draw box
@gbluma
gbluma / plai-ex1.rkt
Created March 26, 2013 05:18
First demo from PLAI scheme book. This example constructs a parser and interpreter for simple arithmetic.
#lang plai
(define-type AE
[num (n number?)]
[add (lhs AE?) (rhs AE?)]
[sub (lhs AE?) (rhs AE?)])
(define (parse sexp)
(cond
[(number? sexp) (num sexp)]
@gbluma
gbluma / complex-newton.wxm
Created May 12, 2013 23:37
Newton's method for finding fixpoints in complex plane
good_enough(f, guess) :=
if (cabs(f(guess))<1E-15) then true
else false$
improve(f, guess) :=
guess - (f(guess)/(at (diff (f(z), z, 1), [z = guess])))$
newton(f,guess) :=
if (good_enough(f,guess)=true)
then guess
@gbluma
gbluma / primes.acl2
Last active December 17, 2015 16:39
Some cross-language testing I've done lately. Benchmarks are not accurate because the algorithm is not consistent enough.
(defun inner (k n)
(cond
((> (* k k) n) 1)
((= 0 (mod n k)) 0)
(t (inner (+ k 2) n))))
(defun is_prime (n)
(cond
((= n 2) 1)
((< n 2) 0)
@gbluma
gbluma / scan.py
Created June 12, 2013 19:16
A simple fuzzy string scanner
import zlib, os, sys
def getsize(s):
return len(zlib.compress(s))
def dist(a, b):
n1 = getsize(a); n2=getsize(b)
return (min(getsize(a+b), getsize(b+a)) - min(n1,n2))/float(max(n1,n2))
@gbluma
gbluma / typedEnums.php
Created September 23, 2013 22:11
Type inhabited enumerations and type-directed pattern matching
<?php
class Matcher {
private $cases = array();
function __construct($target, $source) {
$this->target = $target;
$this->source = $source;
}
@gbluma
gbluma / exhaustive.rkt
Created September 29, 2013 13:45
Exhaustive pattern matching (refinement types!) in Typed/Racket.
#lang typed/racket
;; We get exhaustive pattern matching
(: f ((U String Integer) -> Boolean))
(define (f x)
(cond
[(string? x) (string=? x "hi")] ; covers string needs
[(exact-nonnegative-integer? x) (= x 7)] ; covers positive integers
[(even? x) (= x -8)] ; we can actually cover a subset of negative integers
@gbluma
gbluma / vimrc
Last active March 5, 2019 21:58
vimrc
:syntax on
"Syntax Support
au BufRead,BufNewFile *.md set syntax=tex
:autocmd FileType * set formatoptions=tcql nocindent comments&
:autocmd FileType c,cpp set formatoptions=croql cindent comments=sr:/*,mb:*,ex:*/,://
au FileType htm,html,xhtml,xml so ~/.vim/ftplugin/html_autoclosetag.vim
au BufRead,BufNewFile *.fs,*.fsx set filetype=fs
au BufRead,BufNewFile *.fs,*.fsx set syntax=ocaml
au! BufRead,BufNewFile *.gsl setfiletype gsl