[PerlChina] Perl with opensources

agentzh agentzh at gmail.com
Mon Oct 6 00:04:29 PDT 2008


2008/10/6 Jeff Peng <jeffpeng at gmx.net>

> 例如,对这么一组数据:
> {a}-{b,c}-{d,e,f}-{a}
> 每组里抽一个字母,写出所有组合(按顺序)。
> 我想了下,自己用一堆的map{}也七七八八能搞定,但Randal用一行就搞定了:
>
> my @result = glob "{a}-{b,c}-{d,e,f}-{a}";
>
>
这个一定要顶。。。原来 glob 也能这么用。。。哈哈!

Haskell 里可以这么写:

   [ Data.List.intersperse '-' [u,v,w,x] | u <- "a", v <- "bc", w <- "def",
x <- "a"]

GHCi 的输出是

   ["a-b-d-a","a-b-e-a","a-b-f-a","a-c-d-a","a-c-e-a","a-c-f-a"]

在 Python, Erlang 等支持 list comprehension 的语言里都可以写成这样的,呵呵。可惜。。。Perl 里没有 list
comprehension。。。glob 在这个例子中极好,只是对于更复杂的情形还不够一般,呵呵。

Thanks!
-agentzh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/china-pm/attachments/20081006/4859d4ba/attachment.html>


More information about the China-pm mailing list