chr*_*ina 6 haskell list-comprehension
我想使用列表推导返回小于参数Int的所有立方体(x ^ 3).我有以下内容:
cubesLessThanN :: Int -> [Int]
cubesLessThanN int = [if x * x * x <= int then x else * | x <- [0..int]]
Run Code Online (Sandbox Code Playgroud)
星号是我遇到问题的地方.一旦else
发生,我有点想停止处理循环.决赛[list]
应该只有立方体,而不是其他值x
.我真的不在乎它是如何发生的,但想知道选项,以及差异是什么(如果有的话).
如果我尝试返回null
,Nothing
,''
,和一些其他的.我知道int
如果我退回任何东西,我应该返回一种类型.
Ry-*_*Ry- 11
用途takeWhile
:
cubesLessThanN :: Int -> [Int]
cubesLessThanN int = takeWhile ((<= int) . (^3)) [0..]
Run Code Online (Sandbox Code Playgroud)
列表理解支持警卫.
[x | x <- [0..int], x ^ 3 <= int]
Run Code Online (Sandbox Code Playgroud)
由于列表推导是列表monad的糖,这相当于guard
在do
块中使用函数:
do
x <- [0..int]
guard (x ^ 3 <= int)
return x
Run Code Online (Sandbox Code Playgroud)
如果我们将这个>>=
和它内容定义为>>=
和guard
:
concatMap (\x -> if x ^ 3 <= int then [x] else []) [0..int]
Run Code Online (Sandbox Code Playgroud)
这类似于过滤器.
filter (\x -> x ^ 3 <= int) [0..int]
Run Code Online (Sandbox Code Playgroud)
即使在值x ^ 3
超过值之后,检查条件仍将继续(延迟)int
.为了防止这种情况,您可以使用,takeWhile
因为您知道您的功能是单调的.
takeWhile (\x -> x ^ 3 <= int) [0..int]
Run Code Online (Sandbox Code Playgroud)