Skip to content

Instantly share code, notes, and snippets.

View asandroq's full-sized avatar

Alex Silva asandroq

View GitHub Profile
void sly_io_write_c_string(sly_state_t *S, const char* s, sly_oport_t *port)
{
const char *p;
for(p = s; p; p++) {
port_write_char(S, port, (sly_char_t)*p);
}
}
;; hypothetical Scheme web DSL
;; note that the macro binds the URI variables
;; to local variables
(GET "/api/show/:user/:repo"
(if (user-has-repo? user repo)
(render-repo-xml user repo)
...))
(define (legal? move player board)
(and (eqv? (board-ref board move) *empty*)
(any? (lambda (dir)
(would-flip move player board dir))
*all-directions*)))
(let* ((ser (with-input-from-file "state.scm"
(lambda () (read))))
(obj (u8vector->object ser)))
(cond
((game-state? obj
obj)
((game-state-1? obj)
(game-state-from-v1 obj))
((game-state-2? obj)
(game-state-from-v2 obj))
;; made tail-recursive
;; constant stack use
(define (file-lines file)
(call-with-input-file file
(lambda (port)
(let loop ([line (read-line port)]
[l '()])
(if (eof-object? line) (reverse l)
(loop (read-line port) (cons line l)))))))
(define-syntax define-page
(syntax-rules ()
((_ name title headers body ...) (define (name #!rest params)
(let* ([post-data (process-request)]
[form-data (if post-data
(append post-data
params)
params)])
(html-body name
(lambda ()
> (##expand-code '(or temp (let ((temp (* 2 3))) (and alex temp))))
((lambda (tempv1)
(if tempv1
tempv1
((lambda (tempv2) ((lambda (tempv3) (if tempv3 tempv2 tempv3)) alex))
(* '2 '3))))
temp)
;; joins two consecutives lines like vim's "J"
(defun join-lines ()
(interactive)
(save-excursion
(end-of-line)
(insert-char ? 1)
(delete-char 1)
(while (looking-at "\\s +")
(delete-char 1))))
;;; NQUEENS -- Compute number of solutions to 8-queens problem.
(define (nqueens n)
(define (dec-to n)
(let loop ((i n) (l '()))
(if (= i 0) l (loop (- i 1) (cons i l)))))
(define (try x y z)
(if (null? x)
(if (null? y)
1
0)
POVRAY = povray
CONVERT = convert
ifeq ($(BASE),)
BASE = board
endif
ifeq ($(TYPE),)
TYPE = tiff
endif