在Common Lisp中使用 List Comprehension

 

在 Python 和 Haskell中有非常简洁强大的 List Comprehension 用法:

 

>>> [(x, y) for x in range (1, 4) for y in ["A", "B"]] [(1, 'A'), (1, 'B'), (2, 'A'), (2, 'B'), (3, 'A'), (3, 'B')]

 

 

在 Common Lisp 中可以用  incf-cl 库来实现, 通过 quicklisp 安装很方便:

 

 

* (ql:quickload "incf-cl") To load "incf-cl": Load 1 ASDF system: cl-ppcre Install 1 Quicklisp release: incf-cl ; Fetching #<URL "http://beta.quicklisp.org/archive/incf-cl/2010-10-06/incf-cl-20101006-git.tgz"> ; 14.63KB ================================================== 14,981 bytes in 0.01 seconds (1044.99KB/sec) ; Loading "incf-cl" .................................................. [package incf-cl]..... ("incf-cl") * (use-package :incf-cl) T * (lc (cons x y) (<- x (range 1 3)) (<- y '(a b))) ((1 . A) (1 . B) (2 . A) (2 . B) (3 . A) (3 . B))

 

 

incf-cl 是模仿的Haskell的语法:

 

Prelude> [(x, y) | x <- [1..3], y <- ["A", "B"]] [(1,"A"),(1,"B"),(2,"A"),(2,"B"),(3,"A"),(3,"B")]

 

你可能感兴趣的:(list,python,haskell,System,lisp,2010)