Haskell 列表理解语法
Haskell List comprehension syntax
我正在尝试了解 Monad.Reader Issue 8 中 Brent Yorgey 的多集划分算法。好久没写 Haskell 了,我想我连最基本的东西都忘记了。我被困在这个清单中的代码上:
我应该告诉你 Vec
是 [Int]
的类型别名,而 <|=
表示向量的分量比较:
函数 withinFromTo
应该 return 一个非负向量列表 <= m
,并且按字典序在 s
和 e
之间(包括)。
我在线上卡住了:
[x:xs | x <- [start, (start-1)..end]
[start, (start-1)..end]
是什么意思?我去了 tryhaskell.org 并尝试评估 [3,2..7]
但它只给了我 []
.
我知道这看起来一定是个愚蠢的问题,但我无法通过谷歌搜索找到任何内容,而且我觉得在 运行 解决这个问题之前我可以阅读文档很长时间。
感谢您的帮助。
[a, b .. c]
语法旨在生成以 a
和 b
开始的算术级数,一直持续到达到 c
。这可能是最简单的解释几个例子:
> [3, 2 .. 0]
[3,2,1,0]
> [2, 4 .. 10]
[2,4,6,8,10]
> [3, 1 .. -10]
[3,1,-1,-3,-5,-7,-9]
Arithmetic Sequences, together with the section on the Enum
type class 上的 Haskell 报告部分包含完整的详细信息:
Arithmetic sequences satisfy these identities:
[ e1.. ] = enumFrom e1
[ e1,e2.. ] = enumFromThen e1 e2
[ e1..e3 ] = enumFromTo e1 e3
[ e1,e2..e3 ] = enumFromThenTo e1 e2 e3
where enumFrom
, enumFromThen
, enumFromTo
, and enumFromThenTo
are class methods in the class Enum
as defined in the Prelude
(see Figure 6.1).
For the types Int
and Integer
, the enumeration functions have the following meaning:
- The sequence
enumFrom e1
is the list [e1,e1 + 1,e1 + 2,…].
- The sequence
enumFromThen e1 e2
is the list [e1,e1 + i,e1 + 2i,…], where the increment, i
, is e2 − e1
. The increment may be zero or negative. If the increment is zero, all the list elements are the same.
- The sequence
enumFromTo e1 e3
is the list [e1,e1 + 1,e1 + 2,…e3]. The list is empty if e1 > e3
.
- The sequence
enumFromThenTo e1 e2 e3
is the list [e1,e1 + i,e1 + 2i,…e3], where the increment, i
, is e2 − e1
. If the increment is positive or zero, the list terminates when the next element would be greater than e3
; the list is empty if e1 > e3
. If the increment is negative, the list terminates when the next element would be less than e3
; the list is empty if e1 < e3
.
我正在尝试了解 Monad.Reader Issue 8 中 Brent Yorgey 的多集划分算法。好久没写 Haskell 了,我想我连最基本的东西都忘记了。我被困在这个清单中的代码上:
我应该告诉你 Vec
是 [Int]
的类型别名,而 <|=
表示向量的分量比较:
函数 withinFromTo
应该 return 一个非负向量列表 <= m
,并且按字典序在 s
和 e
之间(包括)。
我在线上卡住了:
[x:xs | x <- [start, (start-1)..end]
[start, (start-1)..end]
是什么意思?我去了 tryhaskell.org 并尝试评估 [3,2..7]
但它只给了我 []
.
我知道这看起来一定是个愚蠢的问题,但我无法通过谷歌搜索找到任何内容,而且我觉得在 运行 解决这个问题之前我可以阅读文档很长时间。
感谢您的帮助。
[a, b .. c]
语法旨在生成以 a
和 b
开始的算术级数,一直持续到达到 c
。这可能是最简单的解释几个例子:
> [3, 2 .. 0]
[3,2,1,0]
> [2, 4 .. 10]
[2,4,6,8,10]
> [3, 1 .. -10]
[3,1,-1,-3,-5,-7,-9]
Arithmetic Sequences, together with the section on the Enum
type class 上的 Haskell 报告部分包含完整的详细信息:
Arithmetic sequences satisfy these identities:
[ e1.. ] = enumFrom e1 [ e1,e2.. ] = enumFromThen e1 e2 [ e1..e3 ] = enumFromTo e1 e3 [ e1,e2..e3 ] = enumFromThenTo e1 e2 e3
where
enumFrom
,enumFromThen
,enumFromTo
, andenumFromThenTo
are class methods in the classEnum
as defined in thePrelude
(see Figure 6.1).For the types
Int
andInteger
, the enumeration functions have the following meaning:
- The sequence
enumFrom e1
is the list [e1,e1 + 1,e1 + 2,…].- The sequence
enumFromThen e1 e2
is the list [e1,e1 + i,e1 + 2i,…], where the increment,i
, ise2 − e1
. The increment may be zero or negative. If the increment is zero, all the list elements are the same.- The sequence
enumFromTo e1 e3
is the list [e1,e1 + 1,e1 + 2,…e3]. The list is empty ife1 > e3
.- The sequence
enumFromThenTo e1 e2 e3
is the list [e1,e1 + i,e1 + 2i,…e3], where the increment,i
, ise2 − e1
. If the increment is positive or zero, the list terminates when the next element would be greater thane3
; the list is empty ife1 > e3
. If the increment is negative, the list terminates when the next element would be less thane3
; the list is empty ife1 < e3
.