common lisp 里的几个操作符(2)
集合 (Set)
member 函数
默认使用 eql比较对象,可传入关键字参数 :test,作为比较的函数。关键字参数 :key,指定在每个元素上应用这个函数。
> (member 2 '((1) (2)) :test #'equal :key #'car)
((2))
直接找出满足条件的元素
> (member-if #'oddp '(2 3 4))
(3 4)
函数 adjoin 像是条件式的 cons 。它接受一个对象及一个列表,如果对象还不是列表的成员,才构造对象至列表上。
> (adjoin 'b '(a b c))
(A B C)
> (adjoin 'z '(a b c))
(Z A B C)
并集、交集 以及差集。 union 、 intersection 以及 set-difference 。
> (union '(a b c) '(c b s))
(A C B S)
> (intersection '(a b c) '(b b c))
(B C)
> (set-difference '(a b c d e) '(b e))
(A C D)
序列 (Sequence)
一系列有特定顺序的对象。在 cl 中序列包括列表与向量 (vectors)
length 返回元素数目
> (length '(a b c))
3
subseq 接受3个参数
> (subseq '(a b c d) 1 2)
(B)
reverse 反转
> (reverse '(a b c))
(C B A)
排序 sort 。接受一个比较函数。注意sort 会修改原序列,可以先使用 copy-list 复制。
> (sort '(0 2 1 3 8) #'>)
(8 3 2 1 0)
every 与 some
> (every #'oddp '(1 3 5))
T
> (some #'evenp '(1 2 3))
T
> (every #'> '(1 3 5) '(0 2 4))
T
栈 (Stack)
Cons 来实现 pushdown stack 很简单。cl 提供了两个宏 push 与 pop
(push obj lst) 等同于
(setf lst (cons obj lst))
(pop lst) 等同于
(let ((x (car lst)))
(setf lst (cdr lst))
x)
pushnew 宏是 push 的变种,使用了 adjoin 而不是 cons
> (let ((x '(a b)))
(pushnew 'c x)
(pushnew 'a x)
x)
(C A B)
列表的 点状表示法,只有两个元素(括号算一个整体)之间可以使用点符号。
'(a . (b . nil))
'(a . (b))
'(a b . nil)
'(a b)
end
common lisp 里的几个操作符(2)的更多相关文章
- common lisp里的几个操作符
setf 赋值操作符,定义一个全局变量.返回值是最后一个赋值的结果. let 局部变量操作符.let表达式有两部分组成.第一部分是任意多的变量赋值,他们被包裹在一个()中,第二部分是任意数量的表示式 ...
- common lisp和scheme的区别
1. 在Common Lisp 眼中,一个符号的symbol-value 和symbol-function 是不一样的,而Scheme对两者不作区分.在Scheme 里面,变量只有唯一对应的值,它可以 ...
- ANSI Common Lisp Practice - My Answers - Chatper - 3
Ok, Go ahead. 1 (a) (b) (c) (d) 2 注:union 在 Common Lisp 中的作用就是求两个集合的并集.但是这有一个前提,即给的两个列表已经满足集合的属性了.具体 ...
- Common Lisp编译程序的小技巧
这几天开始玩Common Lisp,遇上了一个有意思的问题,CL一般是解释运行,也有实现可以编译生成字节码(fas文件).我正在用的两种CL实现是SBCL和CLISP,前者是我从<实用Commo ...
- Common Lisp
[Common Lisp] 1.操作符是什么? 2.quote. 3.单引号是quote的缩写. 4.car与cdr方法. 5.古怪的if语句. 6.and语句. 7.判断是真假. null 与 no ...
- 搭建fedora开发环境 common lisp, c++, go
第三方软件库: http://download1.rpmfusion.org/free/fedora/releases/25/Everything/x86_64/os/repoview/index.h ...
- Difference between LET and LET* in Common LISP
Difference between LET and LET* in Common LISP LET Parallel binding which means the bindings com ...
- scheme和common lisp 区别
Scheme and Common Lisp use different names for some of the basic system functions. Many Lisp program ...
- slime+sbcl for common lisp
sudo apt-get install slime audo apt-get install sbcl ;;sbcl+slime for common lisp ;;sudo apt-get ins ...
随机推荐
- Linux脚本程序
#!/bin/bash # array-ops.sh: 数组更多有趣的用法. array=( zero one two three four five ) # 元素 ]} # zero } # zer ...
- DBS:TestSys
ylbtech-DBS:TestSys 1.返回顶部 1. -- ============================================= -- 测试系统 -- 2018-4-12 ...
- .NET Threadpool的一点认识
说到.NET Threadpool我想大家都知道,只是平时比较零散,顾现在整理一下: 一码阻塞,万码等待:ASP.NET Core 同步方法调用异步方法“死锁”的真相 .NET Threadpool ...
- Jetpack 架构组件 Room 数据库 ORM MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 2-08. 用扑克牌计算24点(25) (ZJU_PAT 数学 枚举)
题目链接:http://pat.zju.edu.cn/contests/ds/2-08 一副扑克牌的每张牌表示一个数(J.Q.K分别表示11.12.13,两个司令都表示6).任取4张牌.即得到4个1~ ...
- MySQL出现Access denied for user ‘root’@’localhost’ (using password:YES)
连接时MySQL出现了下面的错误: Access denied for user ‘root’@’localhost’ (using password:YES) 解决的办法是先停止MySQL服务,在m ...
- 天府大讲堂:5G时代的物联网发展趋势与产业变革
摘要:国家973物联网首席科学家,中科院上海微系统与信息技术研究所副所长,无锡物联网产业研究院院长刘海涛教授讲授的5G时代的物联网发展趋势与产业变革意义深刻.作者根据天府大讲堂听讲内容加工整理所得,旨 ...
- 用分离、附加的方式实现sql server数据库的备份和还原
一.数据库分离.附加的说明 SQL Server提供了"分离/附加"数据库."备份/还原"数据库.复制数据库等多种数据库的备份和恢复方法.这里介绍一种学习中常用 ...
- 用于Spring Boot Jar部署的shell脚本
用于在Jenkins将jar发送到目标节点之后的部署操作, 包含deploy, start, stop, restart功能. 在deploy时会自动备份原jar至指定目录 # Please defi ...
- Delphi 如何操作Excel
摘自:http://wenjieshiyu.blog.163.com/blog/static/10739413201072033115869/ 个人收藏:Delphi 控制Excel(一) 使用动态 ...