Skip to content

Instantly share code, notes, and snippets.

View osa1's full-sized avatar

Ömer Sinan Ağacan osa1

View GitHub Profile
@osa1
osa1 / gist:2877817
Created June 5, 2012 20:53
memoization
let memoize f =
let table = Hashtbl.create 10 in
fun p -> try Hashtbl.find table p
with Not_found ->
let _ = print_string "not found (memoize)\n" in
let v = f p in
let _ = Hashtbl.add table p v in
v
let memoize2 f =
(defun expand-params (params body &optional wrapper)
(if (= 1 (length params))
(if wrapper
`(,wrapper (lambda (,(car params)) ,@body))
`(lambda (,(car params)) ,@body))
(if wrapper
`(,wrapper (lambda (,(car params)) ,(expand-params (cdr params) body wrapper)))
`(lambda (,(car params)) ,(expand-params (cdr params) body wrapper)))))
(defmacro defcurry (name (&rest params) &body body)
@osa1
osa1 / gist:2904582
Created June 10, 2012 09:00
counting indentations in alex
getLexerIndentLevel :: Alex Int
getLexerIndentLevel = Alex $ \s@AlexState{alex_ust=ust} -> Right (s, indentation ust)
setLexerIndentLevel :: Int -> Alex ()
setLexerIndentLevel i = Alex $ \s@AlexState{alex_ust=ust} -> Right (s{alex_ust=(AlexUserState i)}, ())
collectIndent :: AlexInput -> Int -> Alex Lexeme
collectIndent input@(p, _, _, str) i = do
lastIndent <- getLexerIndentLevel
currIndent <- countIndent (drop 1 str) 0
@osa1
osa1 / gist:2974513
Created June 22, 2012 18:56
call/cc in Common Lisp
(defmacro defcont (name (&rest params) &body body)
(let ((result (gensym)))
`(defun ,name (continuation ,@params)
(let ((,result ,@body))
(funcall continuation ,result)))))
(defun call/cc (continuation fun)
(funcall fun continuation))
@osa1
osa1 / gist:2998310
Created June 26, 2012 19:37
my sublime text 2 settings
{
"color_scheme": "Packages/Color Scheme - Default/Monokai.tmTheme",
"font_face": "Droid Sans Mono",
"font_size": 8,
// "gutter": false,
"highlight_line": true,
"caret_style": "solid",
"trim_trailing_white_space_on_save": true,
"save_on_focus_lost": true,
"highlight_modified_tabs": true,
λ> (make-application (read "+") (read "'(1 2)"))
<Syntax>
λ> (eval (make-application (read "+") (read "'(1 2)")))
3
λ> eval
<Fexpr>
λ> (defun garip-fn (fn param) (+ 10 (fn param)))
<Function>
λ> (defun id (x) x)
applyCont DefineCont "id" EndCont env >>apply, eval, first, rest, cons, eq, +, -, *, div, mod, quot, rem, =, <, >, /=, >=, <=, &&, ||, string?, symbol?, number?, list?, boolean?, symbol->string, type-of, println, read, make-application, nil<< val <Function>
applyCont EndCont env >>id, apply, eval, first, rest, cons, eq, +, -, *, div, mod, quot, rem, =, <, >, /=, >=, <=, &&, ||, string?, symbol?, number?, list?, boolean?, symbol->string, type-of, println, read, make-application, nil<< val <Function>
<Function>
λ> (defun map (fun lst) (if (eq (list) lst) lst (cons (fun (first lst)) (map fun (rest lst)))))
applyCont DefineCont "map" EndCont env >>id, apply, eval, first, rest, cons, eq, +, -, *, div, mod, quot, rem, =, <, >, /=, >=, <=, &&, ||, string?, symbol?, number?, list?, boolean?, symbol->string, type-of, println, read, make-application, nil<< val <Function>
applyCont EndCont env >>map, id, apply, eval, first, rest, cons, eq, +, -, *, div, mod, quot, rem, =, <, >, /=, >=, <=, &&, ||, st
@osa1
osa1 / gist:3267084
Created August 5, 2012 20:47
export blog posts as octopress compatible markdown files
from django.core.management.base import BaseCommand
from main.models import *
class Command(BaseCommand):
def handle(self, *args, **options):
for post in Post.objects.all():
name = post.date.strftime("%Y-%m-%d") + "-" + post.verbose_name + ".markdown"
full_date = post.date.strftime("%Y-%m-%d %H:%M")
@osa1
osa1 / gist:3334166
Created August 12, 2012 20:12
unification??
{-# OPTIONS_GHC -Wall
-fno-warn-missing-signatures
-fno-warn-hi-shadowing
-fno-warn-unused-do-bind
-fno-warn-name-shadowing #-}
module Poly where
--import Control.Monad.Identity
import Control.Monad.Error
@osa1
osa1 / gist:3373536
Created August 16, 2012 20:47
wtf, ocaml record types
➜ ~ ocaml
OCaml version 4.00.0
# type record1 = { x : int; y : int };;
type record1 = { x : int; y : int; }
# { x = 10; y = 20 };;
- : record1 = {x = 10; y = 20}
# type record2 = { x : int };;
type record2 = { x : int; }
# { x = 10; y = 20 };;