Library for Atcoder

Table of Contents

1 anaphoric macros

1.1 aif

1.1.1 definition

(defmacro aif (test-form then-form &optional else-form)
  `(let ((it ,test-form))
     (if it ,then-form ,else-form)))

1.1.2 usage

(aif 11 (princ it)) ;; => 11

1.2 awhen

1.2.1 definition

(defmacro aif (test-form then-form &optional else-form)
  `(let ((it ,test-form))
     (if it ,then-form ,else-form)))

(defmacro awhen (test-form &body body)
  `(aif ,test-form
        (progn ,@body)))

1.2.2 usage

(awhen 10 (print it)) ;; => 10

1.3 aand

1.3.1 definition

(defmacro aand (&rest args)
  (cond ((null args) t)
        ((null (cdr args)) (car args))
        (t `(aif ,(car args) (aand ,@(cdr args))))))

1.3.2 TODO usage


1.4 acond

1.4.1 definition

(defmacro acond (&rest clauses)
  (if (null clauses)
      nil
      (let ((cl1 (car clauses))
            (sym (gensym)))
        `(let ((,sym ,(car cl1)))
           (if ,sym
               (let ((it ,sym)) ,@(cdr cl1))
               (acond ,@(cdr clauses)))))))

1.4.2 TODO usage


1.5 alambda

1.5.1 definition

(defmacro alambda (parms &body body)
  `(labels ((self ,parms ,@body))
     #'self))

1.5.2 TODO usage


1.6 ablock

1.6.1 definition

(defmacro alambda (parms &body body)
  `(labels ((self ,parms ,@body))
     #'self))

(defmacro ablock (tag &rest args)
  `(block ,tag
     ,(funcall (alambda (args)
                        (case (length args)
                          (0 nil)
                          (1 (car args))
                          (t `(let ((it ,(car args)))
                                ,(self (cdr args))))))
               args)))

1.6.2 usage


1.7 amap

1.7.1 definition

(defmacro alambda (parms &body body)
  `(labels ((self ,parms ,@body))
     #'self))

1.7.2 TODO usage


1.8 aevery

1.8.1 definition

(defmacro aevery (form list)
  `(every #'(lambda (it) ,form) ,list))

1.8.2 TODO usage


1.9 areduce

1.9.1 definition

(defmacro areduce (form list &key initial-value)
  `(reduce #'(lambda (it-accum it) ,form) ,list :initial-value ,initial-value))

1.9.2 TODO usage


2 string

2.1 string+

2.1.1 definition

(defmacro string+ (&rest str)
  `(concatenate 'string ,@str))

2.1.2 TODO usage


2.2 every-string=

2.2.1 definition

(defmacro every-string= (c sequence)
  `(aevery (string= it ,c) ,sequence))

2.2.2 TODO usage


2.3 string-case

2.3.1 definition

(defmacro string-case (my-condition my-lst)
  `(cond ,@(mapcar #'(lambda (x)
                       `((string= ,my-condition ,(car x)) ,(cadr x)))
                   my-lst)))

2.3.2 usage

(string-case "Sunny"
             (("Sunny" (progn (print "Cloudy")))
              ("Cloudy" "Rainy")
              ("Rainy" "Sunny")))

2.4 string-case

2.4.1 definition

(defmacro string-case (my-condition my-lst)
  `(cond ,@(mapcar #'(lambda (x)
                       `((string= ,my-condition ,(car x)) ,(cadr x)))
                   my-lst)))

2.4.2 usage

(string-case "Sunny"
             (("Sunny" "Cloudy")
              ("Cloudy" "Rainy")
              ("Rainy" "Sunny"))) ;; => "Cloudy"

2.5 split-string

2.5.1 definition

(defun split-string (string split)
  (loop :for i = 0 :then (1+ j)
        :as j = (position split string :start i)
        :collect (subseq string i j)
        :while j))

2.5.2 usage

(split-string "2019/04/30" #\/) ;; => ("2019" "04" "30")

2.6 shift-string

2.6.1 definition

(defun shift-string (str shift)
  (map 'string
       #'(lambda (c)
           (code-char
            (+
             (mod (+ (- (char-int c) (char-int #\A)) shift) 26)
             (char-int #\A))))
       str))

2.6.2 usage

(shift-string "ABCXYZ" 2)

3 char

3.1 char+

3.1.1 definition

(defmacro char-1+ (char)
  `(code-char (1+ (char-code ,char))))

3.1.2 TODO usage


3.2 char-code+

3.2.1 definition

(defmacro char-code+ (char num)
  `(code-char (+ (char-code ,char) ,num)))

3.2.2 TODO usage


3.3 char-case

3.3.1 definition

(defmacro char-case (my-condition my-lst)
  `(cond ,@(mapcar #'(lambda (x)
                       `((char= ,my-condition ,(car x)) ,(cadr x)))
                   my-lst)))

3.3.2 usage

(char-case #\1
           ((#\1 #\9)
            (#\9 #\1)
            (t x))) ;; => #\9

4 utility

4.1 nested-loops

4.1.1 definition

(defmacro nested-loops (vars values &rest body)
  (if vars
      `(loop :for ,(first vars) :from ,(car (first values)) :to ,(cdr (first values))
             :do (nested-loops ,(rest vars) ,(rest values) ,@body))
      `(progn ,@body)))

4.1.2 usage

(nested-loops (x y) ((1 . 2) (1 . 2))
              (princ (* x y)))

4.2 nested-nth

4.2.1 definition

(defmacro nested-nth (idxs my-lst)
  `(if ,idxs (nth (car (reverse ,idxs)) (nested-nth (cdr (reverse ,idxs)) ,my-lst)) ,my-lst))

4.2.2 usage

(nested-nth '(1 1) '(1 (1 2)))

4.3 get-even-list

4.3.1 definition

(defun get-even-list (lst)
  (loop :with return-lst = '()
        :for index :from 0 :below (length lst)
        :unless (zerop (mod index 2))
          :do (setf return-lst (append return-lst (list (nth index lst))))
        :finally (return return-lst)))

4.3.2 TODO usage


4.4 remove-nth

4.4.1 definition

(defun remove-nth (n list)
  (declare
    (type (integer 0) n)
    (type list list))
  (if (or (zerop n) (null list))
    (cdr list)
    (cons (car list) (remove-nth (1- n) (cdr list)))))

4.4.2 TODO usage


4.5 check-bit-frag

4.5.1 definition

(defun check-bit-frag (bit index)
  (logand (ash bit (- 0 index)) 1))

4.5.2 TODO usage


4.6 while

4.6.1 definition

(defmacro while (test &body body)
  `(do ()
     ((not ,test))
     ,@body))

4.6.2 TODO usage


4.7 convert-sum-list

4.7.1 definition

(defun convert-sum-list (lst)
  (cdr (reduce #'(lambda (accum elm)
                   (append accum (list (+ (car (last accum)) elm))))
               lst
               :initial-value '(0))))

4.7.2 TODO usage


5 algorithms

5.1 binary-search

5.1.1 definition

(defun binary-search (item buffer)
  (do ((low 0)
       (high (1- (length buffer))))
      ((> low high))
    (let ((mid (floor (+ low high) 2)))
      (cond
        ((= (nth mid buffer) item) (return mid))
        ((< (nth mid buffer) item) (setq low (1+ mid)))
        (t (setq high (1- mid)))))))

5.1.2 TODO usage


5.2 等差数列の和

5.2.1 definition

(defun arithmetic-progression-sum (a b)
  (let ((n (1+ (- b a))))
    (+ (* n (1- a))
       (/ (* n (1+ n)) 2))))

5.2.2 usage

(arithmetic-progression-sum 2 8) ;; => 35

5.3 マンハッタン距離

5.3.1 definition

(defun manhattan-distance (lst)
  (reduce #'+ (mapcar #'abs lst)))

5.3.2 usage

(manhattan-distance '(3 -1 -4 1 -5 9 2 -6 5 -3)) ;; => 39

5.4 ユークリッド距離

5.4.1 definition

(defun euclidean-distance (lst)
  (sqrt (reduce #'+ (mapcar #'(lambda (x) (coerce (* x x) 'double-float)) lst))))

5.4.2 usage

(euclidean-distance '(3 -1 -4 1 -5 9 2 -6 5 -3)) ;; => 14.387494569938159

5.5 チェビシェフ距離

5.5.1 definition

(defun chebyshev-distance (lst)
  (reduce #'max (mapcar #'abs lst)))

5.5.2 usage

(chebyshev-distance '(3 -1 -4 1 -5 9 2 -6 5 -3)) ;; => 9

Author: takeounn

Created: 2020-11-18 Wed 10:38

Validate