Skip to content

Instantly share code, notes, and snippets.

View lispm's full-sized avatar

Rainer Joswig lispm

  • Germany
View GitHub Profile
@lispm
lispm / lottery
Created June 9, 2014 09:13
lottery
;; here is a version with a better shuffle function. Note that the shuffle
;; function is written in a functional Lisp style. You need to read it from
;; inside to outside.
;; it uses a vector where the elements get a random double float attached.
;; The vector gets sorted by the random double floats.
;; the vector-iota function is another utility function
;; the 'domain' level LOTTERY function then is just a composition of the utility functions
@lispm
lispm / gist:5990c341a20003de493b
Last active August 29, 2015 14:10
non-recursive quicksort
;;; non-recursive quicksort
;;; http://bertrandmeyer.com/2014/12/07/lampsort/
(defun partition (array low high)
(let ((pivot-value (aref array high))
(insert-at low))
(loop for i from low upto high do
(when (< (aref array i) pivot-value)
(rotatef (aref array i) (aref array insert-at))
(incf insert-at)))
@lispm
lispm / gist:6066e1eeadf943910c47
Last active August 29, 2015 14:11
longest graph, version without a NODE structure
; https://github.com/logicchains/LPATHBench/blob/master/writeup.md
(eval-when (:load-toplevel :compile-toplevel :execute)
(defstruct route
(dest 0 :type fixnum)
(cost 0 :type fixnum)))
(defun parse-line (line &aux (pos 0) n)
(declare (ignorable n))
(loop repeat 3
@lispm
lispm / gist:e9372894519f8e6feae1
Last active August 29, 2015 14:11
longest path, route and node structures, but no visited array
;;; optimizations copyright Rainer Joswig, 2014, joswig@lisp.de
;;; Original: https://github.com/logicchains/LPATHBench/blob/master/writeup.md
;;; Structure declarations
;; In Common Lisp the slot declarations might save space for some types. But
;; that might not make it faster, since access gets more complicated..
;; It also might take more time, when type checks are done at runtime.
;; Some implementations check slot updates for correct types under some
;; SAFETY optimization values.
@lispm
lispm / gist:e063918e5c354c138922
Last active August 29, 2015 14:11
longest path, without node and route structures
; https://github.com/logicchains/LPATHBench/blob/master/writeup.md
(defun parse-line (line &aux (pos 0) n)
(declare (ignorable n))
(loop repeat 3
collect (multiple-value-setq (n pos)
(parse-integer line :start pos :junk-allowed t))))
(defparameter *file* "agraph")
@lispm
lispm / gist:4b200a7a5a7f5c3fd911
Last active August 29, 2015 14:12
day of week
; https://github.com/d4gg4d/it-factors/blob/master/day-of-the-week.lisp
(defvar *month-to-code*
'(nil 1 4 4 0 2 5 0 3 6 1 4 6))
(defun fetch-month-code (month)
(nth month *month-to-code*))
(defvar *code-to-day*
'("Saturday" "Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday"))
@lispm
lispm / gist:52d49e61cea60b287074
Last active August 29, 2015 14:13
cross product for two lists
(defun cp (l1 l2)
(mapcan (lambda (e2)
(mapcar (lambda (e1) (list e1 e2)) l1))
l2))
(defun cp (l1 l2)
(iter:iter outer (iter:for i iter:in l1)
(iter:iter (iter:for j iter:in l2)
(iter:in outer (iter:collect (list i j))))))
@lispm
lispm / gist:082860afd06a6209fc76
Created April 9, 2015 11:57
Retrieving Source Code from a running Clozure CL
(defun retrieve-source-code (&optional (package *package*))
(do-symbols (s package)
(multiple-value-bind (symbol where)
(find-symbol (symbol-name s)
package)
(declare (ignore symbol))
(when (member where '(:internal :external))
(let ((ds (find-definition-sources s)))
(when (and ds (listp ds))
(loop for (nil sn) in ds
@lispm
lispm / gist:c143c01c5b52d806249f
Created May 25, 2015 22:16
get the nth prime
(defmethod get-prime ((a integer))
(let ((vector (make-array 100)))
(setf (aref vector 0) 2)
(loop for i from 1 upto a do
(loop for j from (1+ (aref vector (1- i))) do
(when (= i (loop for k below i
until (zerop (mod j (aref vector k)))
finally (return k)))
(setf (aref vector i) j)
(return))))
; http://ruslanspivak.com/lsbasi-part3/
; Lisp / CLOS version Rainer Joswig, joswig@lisp.de, 2015
;;; ================================================================
;;; Token
(defclass token ()
((type :accessor token-type :initarg :type)