euler/lisp/euler.lisp

60 lines
1.4 KiB
Common Lisp
Raw Normal View History

2016-10-19 21:34:59 +00:00
(defun range (min max) (loop for n from min below (+ max 1) by 1 collect n))
2016-10-19 06:02:57 +00:00
(defun sum (list) (apply '+ list))
(defun select (expr list) (remove-if-not expr list))
2016-10-19 21:34:59 +00:00
(defun filter (expr list) (remove-if expr list))
2016-10-21 16:52:08 +00:00
(defun empty (list) (eq list nil))
2016-10-19 21:34:59 +00:00
(defun partial (func &rest args1)
(lambda (&rest args2)
(apply func (append args1 args2)))
)
(defun prime (n)
(eq (length (member T (mapcar (lambda (a) (eq (mod n a) 0)) (range 2 (sqrt n))))) 0)
)
2016-10-21 16:52:08 +00:00
(defun cartesian-helper (thing list)
(if (empty list)
nil
(cons (list thing (first list)) (cartesian-helper thing (rest list)))
)
)
(defun cartesian (list-one list-two)
(if (empty list-one)
nil
(append (cartesian-helper (first list-one) list-two) (cartesian (rest list-one) list-two))
)
)
(defun number-to-digit-list (num)
(if (< num 10)
(list num)
(append (number-to-digit-list (floor (/ num 10))) (list (mod num 10)))
)
)
(defun digit-list-to-number (digit-list)
(if (empty digit-list)
0
(+ (* (first digit-list) (expt 10 (- (length digit-list) 1))) (digit-list-to-number (rest digit-list)))
)
)
(defun is-palindrome (num)
(let* ((digit-list (number-to-digit-list num)))
(equal (reverse digit-list) digit-list))
)
(defun resolve-tuple (expr tuple)
(funcall expr (first tuple) (second tuple))
)
(defun resolve-tuples (expr tuple-list)
(mapcar (partial #'resolve-tuple expr) tuple-list)
)
(defun max-of (list)
(reduce #'max list)
)