legacy-dotfiles/sbcl/.sbclrc
2013-11-03 22:13:10 +01:00

69 lines
2 KiB
Common Lisp

;; -*- mode: lisp; -*-
;;; The following lines added by ql:add-to-init-file:
#-quicklisp
(let ((quicklisp-init (merge-pathnames ".local/share/quicklisp/setup.lisp"
(user-homedir-pathname))))
(when (probe-file quicklisp-init)
(load quicklisp-init)))
(defun primep (number)
(unless (= number 1)
(let ((max (floor (sqrt number))))
(or (= number 2)
(and (not (evenp number))
(= (length (loop for i from 3 upto max by 2
if (integerp (/ number i))
if (< 1 i number)
do (return-from primep nil)
else
collect i))
0))))))
(defun next-prime (number)
(loop for i from (1+ number)
if (primep i)
return i))
(defun previous-prime (number)
(loop for i from (1- number) downto 0
if (primep i)
return i))
(defun primes (number)
(do ((prime 2 (next-prime prime))
(primes (list)))
((> prime number) (reverse primes))
(setf primes (cons prime primes))))
(defun prime-factors (number)
(do ((i 1 (1+ i)))
((> i (/ number 2)) (list number))
(let ((d (/ number i)))
(if (and (integerp d) (primep i))
(return (cons i (prime-factors d)))))))
(defun largest-prime-factor (number)
(apply #'max (prime-factors number)))
(defun start-local-server ()
(ql:quickload "usocket")
(usocket:socket-server
"localhost" 4006
(lambda (stream)
(handler-case
(let ((*standard-input* stream)
(*standard-output* stream)
(type (read stream)))
(case type
(:eval
(princ (eval (read)))
(force-output))
(:shell
(loop
(fresh-line)
(princ "CL> " stream)
(force-output)
(print (eval (read)))))))
(end-of-file () nil)))
() :in-new-thread t :multi-threading t))