在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. Swift基本语法以及与OC的比较

    一.注释: 1.单行注释和OC一致. 2.块注释中有与OC不同点:可以嵌套注释 二.常量和变量: 1.常量:初始化后可读不可写 let 2.变量:初始化后可读可写 var //不需要指定类型,系统会自 ...

  2. ThinkPHP3.1快速入门(13)自动完成

    自动完成是ThinkPHP提供用来完成数据自动处理和过滤的方法,使用create方法创建数据对象的时候会自动完成数据处理.因此,在ThinkPHP使用create方法来创建数据对象是更加安全的方式,而 ...

  3. Ewebeditor最新漏洞及漏洞大全

    Ewebeditor最新漏洞及漏洞大全[收集] 来源:转载作者:佚名时间:2009-06-03 00:04:26 下面文章收集转载于网络:) 算是比較全面的ewebeditor编辑器的漏洞收集,如今的 ...

  4. [Practical Git] Remove unnecessary git tracking with .gitignore files

    Most projects have automatically generated files or folders from the operating system, applications, ...

  5. G711

    G.711就是语音模拟信号的一种非线性量化.细分有二种:G.711 a-lawand G.711 u-law.不同的国家和地方都会选取一种作为自己的标准. G.711a/u bitrate 是64kb ...

  6. js 三元运算符以及|| 和 && 测试

    var  a = '0';var  b = a ? 'me':'hi'; console.log(b);//false 有: undefined , 0, '', false,null//true  ...

  7. C#_MVC_分页update

    private static string getLinkHtml(UrlHelper urlHelper, bool useAjax, string ajaxSuccessFunction, str ...

  8. C#_delegate - 值参数和引用参数

    值参数不能加,引用参数可以. 引用参数是共享的 using System; using System.Collections.Generic; using System.Linq; using Sys ...

  9. OpenCms Application dev-ref

    OpenCms Application Overview Before undertaking development, it will be helpful to understand the ba ...

  10. C# 之 后台加载图片Image

    命名空间为 System.Drawing ,Image.FromFile  一旦使用后,对应的文件在一直调用其生成的Image对象被Disponse前都不会被解除锁定,这就造成了一个问题,就是在这个图 ...