English 中文(简体)
为什么有多台发电机把最合适的发电机视为最紧的通道?
原标题:Why do Haskell list comprehensions with multiple generators treat the rightmost generator as the tightest loop?
最佳回答

因此,事情可以轻松的方式处理。

[(x, y) | x <- [1..10], y <- [1..x]]

——<条码>x在<条码>上可理解:

[(x, y) | y <- [1..x], x <- [1..10]]

感受较少。

此外,这符合<代码>do。 monad syntax

do x <- [1..10]
   y <- [1..x]
   return (x, y)
问题回答

如果你把首先列入<代码>do的对照表,然后将清单加起来,从而成为固定的束缚,则会更有意义。 让我们说,我们想写一个谅解书,我们在其中再次提及已受约束的名字:

[ (x,y) | x <- [1,2,3], y <- [x+1,x+2] ]

范围扩大

do x <- [1,2,3]
   y <- [x+1,x+2]
   return (x,y)

范围扩大

[1,2,3] >>= x ->
[x+1,x+2] >>= y -> 
return (x,y)

该条明确指出,<代码>x在需要时即行适用。

如果将范围扩大到do <>,则不再发生右对冲,而不是左对right,那么,我们的原表述就会扩大为:

[x+1,x+2] >>= y ->
[1,2,3] >>= x ->
return (x,y)

这显然是非意见性的,它指的是在<代码>x未受约束的范围之内的<代码>x的价值。 因此,我们必须把我们原来的理解写成我们原来的理解。

[ (x,y) | y <- [x+1,x+2], x <- [1,2,3] ]

to get the result we wanted, which seems unnatural - at the time your eye scans over the phrase y <- [x+1,x+2] you don t actually know what x is. You d have to read the comprehension backwards to find out.

因此,如果权利最强的约束力不落到“内 lo”中,那么当你认为人必须读到由此产生的法典时,就具有意义。

确实,Adhur使用与Haskell相同的范围结构,以列入对照表。

比较 Haskell:

*Main> concat [[(x,y) | x <- [0..2]] | y <- [0..2]]
[(0,0),(1,0),(2,0),(0,1),(1,1),(2,1),(0,2),(1,2),(2,2)]
*Main> [(x,y) | x <- [0..2], y <- [0..2]]
[(0,0),(0,1),(0,2),(1,0),(1,1),(1,2),(2,0),(2,1),(2,2)]

页: 1

>>> from itertools import chain
>>> list(chain(*[[(x,y) for x in range(3)] for y in range(3)]))
[(0, 0), (1, 0), (2, 0), (0, 1), (1, 1), (2, 1), (0, 2), (1, 2), (2, 2)]
>>> [(x,y) for x in range(3) for y in range(3)]
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
>>> 




相关问题
Euler Problem in Haskell -- Can Someone Spot My Error

I m trying my hand at Euler Problem 4 in Haskell. It asks for that largest palindrome formed by multiplying two three-digit numbers. The problem was simple enough, and I thought my Haskell-fu was up ...

How does foldr work?

Can anybody explain how does foldr work? Take these examples: Prelude> foldr (-) 54 [10, 11] 53 Prelude> foldr (x y -> (x+y)/2) 54 [12, 4, 10, 6] 12.0 I am confused about these executions....

Efficient queue in Haskell

How can I efficiently implement a list data structure where I can have 2 views to the head and end of the list, that always point to a head a tail of a list without expensive calls to reverse. i.e: ...

Problem detecting cyclic numbers in Haskell

I am doing problem 61 at project Euler and came up with the following code (to test the case they give): p3 n = n*(n+1) `div` 2 p4 n = n*n p5 n = n*(3*n -1) `div` 2 p6 n = n*(2*n -1) p7 n = n*(5*n -3)...

Ways to get the middle of a list in Haskell?

I ve just started learning about Functional Programming, using Haskel. I m slowly getting through Erik Meijer s lectures on Channel 9 (I ve watched the first 4 so far) and in the 4th video Erik ...

haskell grouping problem

group :: Ord a => [(a, [b])] -> [(a, [b])] I want to look up all pairs that have the same fst, and merge them, by appending all the list of bs together where they have the same a and discarding ...

Closest equivalent to subprocess.communicate in Haskell

I want to do a popen() / python s subprocess.communicate from Haskell - start a program, give it stdin, and get its stdout/stderr. What s the most direct / Haskellish way to do this?

热门标签