Skip to content

Instantly share code, notes, and snippets.

@manuel
manuel / dyn-wind-commentary.scm
Created August 11, 2012 12:45
Commenting on Oleg's dyn-wind.scm
;; See http://okmij.org/ftp/continuations/implementations.html#dynamic-wind
;; and http://axisofeval.blogspot.com/2012/08/delimited-continuations-do-dynamic-wind.html
;; Slight trick here: use identity of yield-record-tag function as the actual tag
(define (yield-record-tag) yield-record-tag)
(define (make-yield-record v k)
(list yield-record-tag v k))
;; Yield simply aborts up to the generator's caller, delivering to it
;; the yielded value and the continuation for resuming after the call
@manuel
manuel / callcc.scm
Created August 1, 2012 15:21
Call/CC in terms of Feeley's "Better API for First-Class Continuations"
(define (call/cc f)
(continuation-capture
(lambda (k)
(f (lambda (val)
(continuation-graft k (lambda () val)))))))
@manuel
manuel / stack.virtua
Created March 18, 2012 23:55
Fexprs give you teh amazing stack traces
;; Try pasting this into the Virtua REPL at http://manuel.github.com/virtua/
;; (better use V8 - stack trace printing is quite slow atm)
(defun foo (a b c) (bar b))
(defun bar (x) (quux x))
(defun quux (x) (print-stack-trace (stack-frame)))
(foo 1 2 3)
#[Native-Combiner] ()
@manuel
manuel / conditions.txt
Created March 16, 2012 22:21
Integrating conditions and exceptions chat (german)
(10:56:17 PM) msimoni: also: ein lisp mit CS verwendet native exceptions ja für BLOCK/RETURN-FROM
(10:56:30 PM) Chris Neukirchen: ok
(10:56:48 PM) msimoni: ich habe (js-throw obj) === throw obj
(10:57:22 PM) msimoni: und ($js-try handler body-expr) === try { eval(body-expr) } catch(e) { apply(handler, e) }
(10:57:35 PM) msimoni: das sind die primitives in der VM
(10:57:51 PM) msimoni: darauf baue ich BLOCK/RETURN-FROM so:
(10:58:06 PM) msimoni:
(def Block-Escape (make-class ()))
(def make-block-escape (lambda () (make-instance Block-Escape)))
@manuel
manuel / swap.lisp
Created March 10, 2012 01:26
Hygienic swap macro in EdgeLisp
(defmacro swap (a b)
#`(let ((tmp ,b))
(setq ,b ,a)
(setq ,a tmp)))
(let ((x 1) (tmp 2))
(swap x tmp)
(assert (and (= x 2) (= tmp 1)))
(swap tmp x)
(assert (and (= x 1) (= tmp 2))))
@manuel
manuel / define-constructor.scm
Created February 22, 2012 15:40
Adventures in Hygienic Fexprs
;; As part of the implementation of Virtua's [1] object system I need
;; a facility called DEFINE-CONSTRUCTOR that defines a convenient
;; function for creating an instance of a class and setting its slots.
(define-constructor make-person Person (firstname lastname))
;; should be operationally equivalent to
(define make-person
(lambda (firstname lastname)
@manuel
manuel / noooooooooooooo.txt
Created December 6, 2011 22:52
noooooooooooooo
Welcome to Racket v5.1.1.
> (define-syntax bar (syntax-rules () ((_) 1)))
> (define (foo) (bar))
> (foo)
1
> (define-syntax bar (syntax-rules () ((_) 55555)))
> (foo)
1 ;; NOOOOOOOOOO!!!
@manuel
manuel / delimc.lisp
Created August 26, 2011 13:49
Delimited control example
(run-cc
($lambda ()
($let ((p (make-prompt)))
(+ 2 ($push-prompt p
($if (with-sub-cont p
($lambda (k)
(+ ($push-sub-cont k #f)
($push-sub-cont k #t))))
3
4))))))
@manuel
manuel / edgelisp-content-based-storage.lisp
Created June 12, 2011 18:24
Sample of EdgeLisp's content-based storage
;;; Sample of EdgeLisp's content-based storage system
;;; https://github.com/manuel/edgelisp/blob/master/client/base.lisp
;; Create a blob containing the file's contents.
(defvar test-blob (base-make-blob "Hello content-centric world!"))
#[base-blob Hello content-centric world!]
;; Create a tree with a directory entry named "hello.txt" pointing to the blob's ID (hash).
(defvar test-tree
(base-make-tree (list (base-make-dentry "hello.txt" (base-object-id test-blob)))))
abstract class Term<T> {
abstract T eval();
}
class IntegerTerm extends Term<Integer> {
Integer value;
IntegerTerm(Integer value) { this.value = value; }
Integer eval() { return value; }
}