clisp的一些function
阅读原文时间:2023年07月09日阅读:2
;; example: (write (A-Z :start (+ 65 1) :end 87))
(defmacro A-Z (&key (start 65) (end 90))
  (let* ((s-start (gensym))
     (s-end (gensym))
     (s-start start)
     (s-end end))
      `(loop for i from ,s-start to ,s-end
        collect (code-char i))))

(defmacro A-Z (&key (start 65) (end 90))
  (let ((s-start (gensym))
    (s-end (gensym)))
    `(let ((,s-start ,start)
       (,s-end ,end))
       (loop for i from ,s-start to ,s-end
         collect (code-char i)))))

(defun prime-p (n)
  "判读n是否为素数"
  (when (> n 1)
    (do ((i 2 (1+ i)))
    ((= i n) t)
      (when (zerop (mod n i))
    (return nil)))))

(defun typeof (obj)
 "return the type of OBJ"
 (typecase obj
  (list 'list)
  (number 'number)
  (array 'array)
  (function 'function)
  (string 'string)))

(defun our-filter (lst fn)
  ;; 这个过滤器感觉更 remove差不多
  (let ((res nil))
    (dolist (el lst)
      (let ((is (funcall fn el)))
        (if (not is) (push el res))))
  (nreverse res)))

(defun _sort (lst)
  ;;; 这是一个默认从小到大的排序函数
  ;;; > (setf a '(1 3 2))
  ;;; (1 3 2)
  ;;; > (_sort a)
  ;;; (1 2 3)
  ;;; > a
  ;;; (1 2 3)
  (let ((len (length lst)))
    (do ( (i 0 (+ i 1)) ) ( (not (< i len)) )
      (do ( (j 0 (+ j 1)) ) ( (not (< j (- len 1 i))) )
        (let (
              (a (nth j lst))
              (b (nth (+ j 1) lst))
             )
          (and (> a b)
               (setf (nth (+ j 1) lst) a
                     (nth j lst) b
                )))))) lst)

(defun distinct (lst)
  ;;; 去除重复数据
  ;;; > (distinct '(1 2 3 2 1 6))
  ;;; (1 2 3 6)
  (let ((res '()))
    (dolist (el lst)
      (if (not (member el res))
        (setf res (append res (cons el nil)))
      ))
  res))

(defun our-push (lst &rest addData)
  ;; 返回新的lst
  ;; > (our-push '(1 23) 'name "hello" '(a bc))
  ;; (1 23 NAME "hello" (A BC))
  (dolist (el addData)
    (setf lst (append lst (cons el nil))))
  lst)

(defun our-length (lst)
;;; 这个函数返回一个lst的长度
  (if (null lst)
      0
      (+ 1 (our-length (cdr lst)))
  ))

(defun our-qa ()
;;; 一个询问1+1等于几的函数
  (format t "1+1= ")
  (let ((a (read)))
    (if (and (numberp a) (= a 2))
      'yes
      (our-qa)
    )
  ))

(defun null-list (lst)
;;; 若果是空表返回 t
  (if (and (listp lst) (null lst))
      t))

(defun our+ (numberlist)
;;; 这个函数会对 一个全是数字的列表进行求和
;;;> (our+ '(1  2 3 "233"))
;;;6
  (let ( (firstNumber (car numberlist)) )
    (if (or (null numberlist) (null (numberp firstNumber)))
      0
      (+ firstNumber (our+ (cdr numberlist)))
    )
  )
)

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章