在CL里我们可以这样:

 $ sbcl
* (+ 1 2 3)
6
* (< 1 2 3)
T
* (< 2 3 1)
NIL
*

从简单的方面看, CL的+和<就是一个接收多参数的函数,有点类似cpp的add(1,2,3)和less(1,2,3)这样.

所以当C++11开始有了变参模板以后, 就可以玩多参数的加法和多参数比较了

 #include <functional>
template<typename O, typename A,typename B>
bool cmp(O o, A a,B b){
return o(a, b);
}
template<typename O, typename A,typename B,typename... C>
bool cmp(O o,A a,B b,C... c){
return o(a, b) and cmp(o,b,c...);
}
template<typename O, typename A,typename B>
A reduce(O o, A a,B b){
return o(a, b);
}
template<typename O, typename A,typename B,typename... C>
A reduce(O o,A a,B b,C... c){
return reduce(o,o(a, b),c...);
} bool foo(int a,int b,int c,int d){
return cmp(std::less<int>(), a,b,c,d);
}
int bar(int a,int b,int c,int d){
return reduce(std::plus<int>(), a,b,c,d);
}

可能有人会说,这不是坑爹么, 先不管写法比CL的丑, 你还递归调用了,简直弱爆了.....

让我们来看看真相(clang):

 foo(int, int, int, int):                             # @foo(int, int, int, int)
cmpl %esi, %edi
jge .LBB0_1
cmpl %edx, %esi
setl %sil
cmpl %ecx, %edx
setl %al
andb %sil, %al
ret
.LBB0_1:
xorl %eax, %eax
ret bar(int, int, int, int): # @bar(int, int, int, int)
addl %esi, %edi
leal (%rdi,%rdx), %eax
addl %ecx, %eax
ret

和手写的完全没差噢, 稍微是丑一点,不过你看下CL的汇编, 肯定比C++的慢....(逃

最后,这有啥用?

多参数加法似乎是没啥用处了,还不如写a+b+c+d呢
多参数比较还行

a < b and b < c and c < d

c++实现类似Common Lisp的多参数加法和比较的更多相关文章

  1. ANSI Common Lisp Practice - My Answers - Chatper - 3

    Ok, Go ahead. 1 (a) (b) (c) (d) 2 注:union 在 Common Lisp 中的作用就是求两个集合的并集.但是这有一个前提,即给的两个列表已经满足集合的属性了.具体 ...

  2. Common Lisp编译程序的小技巧

    这几天开始玩Common Lisp,遇上了一个有意思的问题,CL一般是解释运行,也有实现可以编译生成字节码(fas文件).我正在用的两种CL实现是SBCL和CLISP,前者是我从<实用Commo ...

  3. Common Lisp第三方库介绍 | (R "think-of-lisper" 'Albertlee)

    Common Lisp第三方库介绍 | (R "think-of-lisper" 'Albertlee) Common Lisp第三方库介绍 一个丰富且高质量的开发库集合,对于实际 ...

  4. common lisp和scheme的区别

    1. 在Common Lisp 眼中,一个符号的symbol-value 和symbol-function 是不一样的,而Scheme对两者不作区分.在Scheme 里面,变量只有唯一对应的值,它可以 ...

  5. Common Lisp中的读取宏 ' #' `( , ,@) #( ) #na( ) #<OBJECT> :Keyword

    当你把  xx 当做符号使用时   'xx  ,  这个符号是没有任何函数/变量语义的, 仅仅是一个 符号而已(就像一个string一样) 但你可以对这个string有其他的用法,比如使用它所bind ...

  6. 搭建fedora开发环境 common lisp, c++, go

    第三方软件库: http://download1.rpmfusion.org/free/fedora/releases/25/Everything/x86_64/os/repoview/index.h ...

  7. Difference between LET and LET* in Common LISP

    Difference between LET and LET* in Common LISP   LET   Parallel binding which means the bindings com ...

  8. scheme和common lisp 区别

    Scheme and Common Lisp use different names for some of the basic system functions. Many Lisp program ...

  9. slime+sbcl for common lisp

    sudo apt-get install slime audo apt-get install sbcl ;;sbcl+slime for common lisp ;;sudo apt-get ins ...

随机推荐

  1. ALV可输入状态下输入金额字段变小数的问题

    http://blog.163.com/mxb_sap@yeah/blog/static/10335262520167109022155/ 小数位数两位    当我在给ALV上给该字段输入整数 '12 ...

  2. VMware 虚拟机安装 Mac OS X Mountain Lion 苹果系统

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  3. BAPI_ACC_DOCUMENT_POST Enter rate / GBP rate type M for Error SG105

    Folks, I was wondering if I could get a bit of help here as I've been racking my brains on it for ag ...

  4. wcf自身作为宿主的一个小案例

    第一步:创建整个解决方案 service.interface:用于定义服务的契约(所有的类的接口)引用了wcf的核心程序集system.ServiceModel.dll service:用于定义服务类 ...

  5. Maven学习小结(二 项目构建过程)

    1.创建Maven项目 1.1 创建Maven项目的约定目录结构 1.2 编辑pom.xml <project xmlns="http://maven.apache.org/POM/4 ...

  6. Java基础知识强化之IO流笔记64:合并流SequenceInputStream

    1. SequenceInputStream合并流的概述: SequenceInputStream类可以将多个输入流串联在一起,合并为一个输入流,因此,该流也被称为合并流.   2. Sequence ...

  7. Android_sharePreference

    /** * Android的四中数据存储方式: * 1.SharePreferences * 2.SQLite * 3.Content Provider * 4.File * * SharePrefe ...

  8. 3D Touch的简单使用

    6s发布以后新增了一个3D touch功能,我个人觉得这个功能点在某些时候还是挺方便的,比如说微信的扫码功能. 直接长按图标就可以进入这个功能里面,不用再打开app.一层层查找了,比较方便. 其实这个 ...

  9. 键盘工具栏的快速集成--HcCustomKeyboard

    源项目地址:HcCustomKeyboard HcCustomKeyboard是一个键盘工具栏控件: 效果: HcCustomKeyboard使用很方便: 三部: 添加控件->操作处理-> ...

  10. 圆满完成平安科技Web安全与App应用安全测试培训!

    圆满完成平安科技Web安全与App应用安全测试培训!