zoukankan      html  css  js  c++  java
  • 示例:实用函数(Utilities)

    (defun single? (lst)
           (and (consp lst)
            (null (cdr lst))))

    CL-USER> (single? nil)
    NIL
    CL-USER> (single? '(a))
    T

    (defun append1 (lst obj)
           (append lst (list obj)))

    CL-USER> (append1 '(a b c d) 'e)
    (A B C D E)

    (defun map-int (fn n)
           (let ((acc nil))
             (dotimes (i n)
               (push (funcall fn i) acc))
             (nreverse acc)))
    CL-USER> (map-int #'(lambda (x) (+ x 1)) 5)

    (1 2 3 4 5)

    (defun filter (fn lst)
           (let ((acc nil))
             (dolist (x lst)
               (let ((val (funcall fn x)))
             (if val (push val acc))))
             (nreverse acc)))
    CL-USER> (filter #'(lambda (x) (+ x 1)) '(1 2 3 4 5))
    (2 3 4 5 6)
    CL-USER> (filter #'(lambda (x)
                 (and (evenp x) (+ x 10)))
             '(1 2 3 4 5))
    (12 14)

    CL-USER> (defun most (fn lst)
           (if (null lst)
               (values nil nil)
               (let* ((wins (car lst))
                  (max (funcall fn wins)))
             (dolist (obj (cdr lst))
               (let ((score (funcall fn obj)))
                 (when (> score max)
                   (setf wins obj
                     max score))))
             (values wins max))))
    MOST
    CL-USER> (most #'length '((a b) (a b c) (a)))
    (A B C)
    3

  • 相关阅读:

    循环队列
    flask开发遇到 Must provide secret_key to use csrf解决办法
    python中set集合的使用
    python中mysqldb的用法
    Pycharm用上未加环境变量的Anaconda
    使用anaconda prompt和youtube-dl下载Youtu视频
    VirtualBox 6.1.12安装部署Ubuntu18.04
    菜鸡记录自己用visual studio2019写第一个C++
    安装MATLAB2019a
  • 原文地址:https://www.cnblogs.com/flowjacky/p/2827181.html
Copyright © 2011-2022 走看看