Getting Lazy with C++

259 views
Skip to first unread message

Yongwei Wu

unread,
Nov 21, 2014, 11:02:26 AM11/21/14
to pon...@googlegroups.com
看了这篇和他的朋友的完成作品:

http://bartoszmilewski.com/2014/04/21/getting-lazy-with-c/
http://ericniebler.com/2014/04/27/range-comprehensions/

真是脑洞大开啊。至少我之前想象不到,可以在C++里表达出Haskell的lazy evaluation。对应于以下的Haskell代码:

main = print (take 10 triples)
 
triples = [(x, y, z) | z <- [1..]
                     , x <- [1..z]
                     , y <- [x..z]
                     , x^2 + y^2 == z^2]


C++至少能写出真正等价的代码了!
using namespace ranges;
 
// Lazy ranges for generating integer sequences
auto const intsFrom = view::iota;
auto const ints = [=](int i, int j)
    {
        return view::take(intsFrom(i), j-i+1);
    };
 
// Define an infinite range of all the Pythagorean
// triples:
auto triples =
    view::for_each(intsFrom(1), [](int z)
    {
        return view::for_each(ints(1, z), [=](int x)
        {
            return view::for_each(ints(x, z), [=](int y)
            {
                return yield_if(x*x + y*y == z*z,
                    std::make_tuple(x, y, z));
            });
        });
    });
 
// Display the first 10 triples
for(auto triple : triples | view::take(10))
{
    std::cout << '('
        << std::get<0>(triple) << ','
        << std::get<1>(triple) << ','
        << std::get<2>(triple) << ')' << '\n';
}
--
Wu Yongwei
URL: http://wyw.dcweb.cn/

Zhenghui Zhou

unread,
Nov 22, 2014, 11:51:49 AM11/22/14
to pon...@googlegroups.com
这两个人都很厉害,Eric Niebler是boost::Proto,模板元编程基础库的作者,是名列前茅的c++顶尖高手。
不过,我的看法是自C++引入lambda之后,类似功能,包括monad,能在语言层面上实现是水到渠成的事情,等我有空整理一下相关链接。

--

---
您收到此邮件是因为您订阅了Google网上论坛中的“TopLanguage”论坛。
要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到pongba+un...@googlegroups.com
要查看更多选项,请访问https://groups.google.com/d/optout

Milo Yip

unread,
Nov 22, 2014, 9:09:39 PM11/22/14
to TopLanguage
记得很久之前在 TL 谈过类似的想法,用C++做 lazy evaluation,不过当时没有lambda。

Yongwei Wu

unread,
Nov 23, 2014, 8:34:08 AM11/23/14
to pon...@googlegroups.com
有Lambda当然就可以做很多事情了——至少map和reduce都能实现了。我是在试图实现pipeline_funcs、但发现做不到时搜索找到Bartosz的网站的。我现在的观点是可以实现编译时提供函数列表的pipeline_funcs,如果使用可变参数模板的话。但鉴于C++是一种静态语言,运行时提供函数列表仍然是不行的。

我没想到的是,作为一种eager evaluation的语言,C++也可以实现lazy evaluation。不知道有没有其他人想到如何去实现,对于我来讲,这个是十分巨大的一步。

2014-11-23 0:51 GMT+08:00 Zhenghui Zhou <zhouzh...@gmail.com>:

Yongwei Wu

unread,
Nov 23, 2014, 8:36:05 AM11/23/14
to pon...@googlegroups.com
哈,我居然参加了这个讨论,我自己都不记得了。看来大家走得太早了(还没有C++11),没做到足够generic,也不行啊。

Zhenghui Zhou

unread,
Nov 23, 2014, 9:26:58 AM11/23/14
to pon...@googlegroups.com
我感觉eric niebler接受的这个挑战,更多是展示他提交的range提案中custom的能力。

Xinyu LIU

unread,
Nov 23, 2014, 9:01:33 PM11/23/14
to pon...@googlegroups.com
2007年我写的:
https://sites.google.com/site/liuxinyu95/softdev.books.book2007.essay5.chn
一眨眼7年都过去了。

现在我的想法变化挺大:我觉得在语义层面支持正则序和应用序的显式控制并不难,也许真正有趣的是graph reduction:
http://en.wikipedia.org/wiki/Graph_reduction
http://en.wikibooks.org/wiki/Haskell/Graph_reduction

下面这本书读了一半就中断了,不知道什么时候能把它读完:
  • Simon Peyton Jones, The Implementation of Functional Programming Languages, Prentice Hall, 1987. Full text online.[1]


Larry, LIU Xinyu
https://github.com/liuxinyu95/AlgoXY

e^(πi)+1 = 0

Reply all
Reply to author
Forward
0 new messages