This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; https://www.reddit.com/r/dailyprogrammer/comments/3ggli3/20150810_challenge_227_easy_square_spirals/ | |
; [2015-08-10] Challenge #227 [Easy] Square Spirals | |
; Rainer Joswig, joswig@lisp.de, 2015 | |
; portable Common Lisp code | |
; 'math' version based on Java version by 9speedy | |
;;; ================================================================ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; https://www.reddit.com/r/dailyprogrammer/comments/3irzsi/20150828_challenge_229_hard_divisible_by_7/ | |
(defun lucky7s (n &aux | |
(nx0 (floor (+ n 2) 3)) | |
(ny0 (floor (+ n 1) 3)) | |
(nz0 (floor n 3)) | |
(cache (make-hash-table :test 'equal))) | |
(labels ((nt (k m &aux (n 0) (t0 0) nsub tsub) | |
(cond ((zerop m) | |
(values (if (zerop k) 1 0) 0)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun split-with (pred list) | |
"splits the LIST at the first NIL result of PRED" | |
(values (loop while (and (not (null list)) | |
(funcall pred (first list))) | |
collect (pop list)) | |
list)) | |
(defun map-while (predicate function list) | |
(loop with e | |
while (not (null list)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun random-text (string &key (n 300) (color-filter nil)) | |
(flet ((one-of (list) | |
(elt list (random (length list)))) | |
(filter-colors (colors string) | |
(loop for color in colors | |
when (search string (symbol-name color) :test #'equalp) | |
collect color))) | |
(let* ((s (make-instance 'capi:output-pane)) | |
(colors (color:get-all-color-names))) | |
(capi:contain s :width 2000 :height 1400) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; http://stackoverflow.com/questions/12345647/rewrite-this-script-by-designing-an-intepreter-in-racket | |
(defmacro verti (n &body body) | |
`(progn | |
(loop repeat ,n | |
do ,@body | |
(terpri)))) | |
(defun hori (n s) | |
(loop repeat n do (princ s))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun average (list) | |
(/ (tree-reduce #'+ list) | |
(tree-reduce #'+ list :key (constantly 1)))) | |
(defun tree-reduce (fn tree &key (key #'identity)) | |
(reduce fn tree | |
:key (lambda (item) | |
(if (consp item) | |
(tree-reduce fn item :key key) | |
(funcall key item))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun factors (n) | |
(iterate | |
(with limit = (isqrt n)) | |
(for factor from 1 below limit) | |
(for (values q r) = (floor n factor)) | |
(when (zerop r) | |
(collect factor into lows) | |
(collect q into highs)) | |
(finally (setf highs (reverse highs)) | |
(when (= n (* limit limit)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun find-query (query descriptions) | |
(find query descriptions | |
:test (lambda (q b) | |
(interpret-query q b)) | |
:key #'second)) | |
(defun lookup (v bindings) | |
(let ((result (assoc v bindings))) | |
(if result | |
(second result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; http://tapestryjava.blogspot.se/2013/02/crafting-code-in-clojure.html | |
; | |
; Extract all the keys from both maps | |
; Remove any duplicates | |
; Convert the keys to strings | |
; Sort the strings into ascending order | |
; Build and return one big string, by concatenating all the key strings, using ", " as a separator | |
; Return "<none>" if both maps are empty | |
(defun hash-table-keys (hash-table) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmacro -> (form &rest forms) | |
(loop with result = form | |
with next-form = nil | |
while forms | |
do (setf next-form (pop forms)) | |
(if (consp next-form) | |
(setf result (destructuring-bind (function . args) next-form | |
`(,function ,result ,@args))) | |
(setf result `(,next-form ,result))) | |
finally (return result))) |