Skip to content

Instantly share code, notes, and snippets.

@zeptometer
Last active December 12, 2015 00:28
Show Gist options
  • Select an option

  • Save zeptometer/4683441 to your computer and use it in GitHub Desktop.

Select an option

Save zeptometer/4683441 to your computer and use it in GitHub Desktop.
(defpackage :rosalind
(:use :common-lisp
:iterate)
(:shadow :read-line
:read-char
:peek-char))
(in-package :rosalind)
(defvar *default-input-path* "E:\\Documents\\downloads\\")
(defvar *default-output-path* "E:\\Documents\\emacs-home\\programs\\rosalind\\")
(defun id->filename (id)
(declare (type (or symbol string) id))
(format nil "rosalind_~(~A~).txt" (if (symbolp id) (symbol-name id) id)))
(defun solve (function id output)
(with-open-file (*standard-input* (merge-pathnames (id->filename id) *default-input-path*)
:direction :input)
(with-open-file (*standard-output* (merge-pathnames output *default-output-path*)
:direction :output
:if-exists :supersede)
(funcall function))))
(defmacro solve! (name)
`(solve #',name ',name ,(format nil "~(~a~).out" (symbol-name name))))
;;;macro
(defmacro with-gensyms (args &body body)
`(let ,(mapcar #'(lambda (arg) `(,arg (gensym ,(symbol-name arg)))) args)
,@body))
;;;syntax
(defmacro aif (pred then &optional else)
`(let ((it ,pred))
(if it
,then
,else)))
(defmacro awhen (pred &body body)
`(aif ,pred
(progn ,@body)))
(defmacro defmemo (name args &body body)
(with-gensyms (table var win)
`(let ((,table (make-hash-table :test #'equal)))
(defun ,name ,args
(multiple-value-bind (,var ,win) (gethash (list ,@args) ,table)
(if ,win
,var
(setf (gethash (list ,@args) ,table)
(progn ,@body))))))))
;;;reader
(defun read-line (&optional default)
(cl:read-line *standard-input* nil default))
(defun read-char (&optional default)
(cl:read-char *standard-input* nil default))
(defun peek-char (&optional default)
(cl:peek-char t *standard-input* nil default))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment