Skip to content

Instantly share code, notes, and snippets.

View chrisosaurus's full-sized avatar

Chris Hall chrisosaurus

View GitHub Profile
@chrisosaurus
chrisosaurus / for.scm
Last active December 25, 2015 08:19
example of scheme macros; adding a traditional for loop to scheme.
(define-syntax for
(syntax-rules (as in) ;; this makes 'as' and 'is' literals (keywords), rather than things to be captured
((for each in list body ...) ;; 'for' and 'in' as literals; 'each', 'list' and 'body ...' are 'variables' to fill
(for-each
(lambda (each) body ...)
list))
((for list as each body ...) ;; 'for' and 'as' as literals; 'each', 'list' and 'body ...' are 'variables' to fill
(for each in list body ...)))) ;; this macro is a 'wrapper' around the previous
(for i in '(1 2 3 4)
@chrisosaurus
chrisosaurus / with.scm
Last active December 25, 2015 08:28
trying to emulate a python-style `with` statement
(define-syntax with
(syntax-rules ()
((with func body ...)
(begin
(func 'start)
body ...
(func 'end)))))
(define-syntax println ;; this could easily be a function
(syntax-rules ()
@chrisosaurus
chrisosaurus / sum.scm
Last active May 30, 2016 01:46
simple example macro (showing begin and terminating tokens) for user in #scheme
(define (sum . args)
(apply + args))
(define-syntax start
(syntax-rules (end)
((start body ... end)
(sum body ...))))
(display (start 10 20 30 end))
@chrisosaurus
chrisosaurus / avl.c
Last active August 29, 2015 14:17 — forked from tonious/avl.c
#define _XOPEN_SOURCE 500 /* Enable certain library functions (strdup) on linux. See feature_test_macros(7) */
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <string.h>
#include <assert.h>
struct avl_node_s {
@chrisosaurus
chrisosaurus / diamond-op.scm
Created October 29, 2015 02:03 — forked from mikeyhc/diamond-op.scm
adding the perl diamond operator to scheme
(define-syntax <>
(syntax-rules ()
((_ v e ...)
(if (null? (command-line-arguments))
(let loop ((v (read-line)))
(unless (eof-object? v)
e ...
(loop (read-line))))
(let file-loop ((port (open-input-file (car (command-line-arguments))))
(rest (cdr (command-line-arguments))))
@chrisosaurus
chrisosaurus / hashing.py
Last active November 2, 2015 06:31
simple python example exploring hashing
def my_hc(c):
return ord(c) - ord('a') + 1
def my_hs(s, a):
hash = 0
for i in range(len(s)):
hash += (a**i) * my_hc(s[i])
return hash
@chrisosaurus
chrisosaurus / puzzler.js
Created November 11, 2015 00:59
pauline puzzler.js
// you can also find an example running at http://ideone.com/zeudW8
//In one line, what is obtained when the result of passing 9 into
//the fourth function of the puzzlers array is then
//passed into the function whose array index matches
//the result of passing 3 into the second function of
//the puzzlers array?
var puzzlers = [
@chrisosaurus
chrisosaurus / foo.ic
Last active November 20, 2015 01:32
icarus 'api' style doc generation
type Foo
a::Int
b::String
end
fn plus(this::Foo, other::Foo) -> Foo
return Foo(this + other.a, this + other.b)
end
fn plus(this::Foo, i::Int) -> Int
@chrisosaurus
chrisosaurus / overloading.ic
Last active December 9, 2015 03:08
icarus operator overloading exploration
type Map<K, V>
...
end
slice<K, V>(m::Map<K,V>, k::K) -> V
...
end
slice<K, V>(&m::Map<K,V>, k::K) -> &V
@chrisosaurus
chrisosaurus / gist:397cd0869219f0500288
Created December 11, 2015 04:53
operator musings
in icarus we want to allow a function to be named the same as an operator
fn and(a::Bool, b::Bool) -> Bool ... end
a and b
and(a, b)
this can make parsing interesting
a and(b or c)