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

  • 相关阅读:
    10055
    国外程序员推荐:每个程序员都应该读的非编程书
    Volume 0. Getting Started
    如何成为一名 Google 软件工程师?【Google招聘信息】作者: 丁鑫哲
    毕设-家校通
    如何快速创建数据库连接字符串
    LeetCode day13
    LeetCode day12
    LeetCode day11
    LeetCode day10 Called it
  • 原文地址:https://www.cnblogs.com/flowjacky/p/2827181.html
Copyright © 2011-2022 走看看