首页 \ 问答 \ Data.List.iterate懒惰评估没有发生(Data.List.iterate lazy evaluation not happening)

Data.List.iterate懒惰评估没有发生(Data.List.iterate lazy evaluation not happening)

我有上述功能,但是当我调用它时,它会卡住,Data.List.iterate会在不停止的情况下进行评估。

rp:: RandomGen g => g -> ([Int], g)
rp g = (map (\x -> (last (fst x))) lst , snd (next g))
    where 
        lst = (Data.List.iterate id ([1], g_second))
        (g_first, g_second) = (split g)

为什么会这样?

谢谢!


I'm having the above function, but when I call it, it gets stuck, the Data.List.iterate evaluates without stopping.

rp:: RandomGen g => g -> ([Int], g)
rp g = (map (\x -> (last (fst x))) lst , snd (next g))
    where 
        lst = (Data.List.iterate id ([1], g_second))
        (g_first, g_second) = (split g)

Why does that happend?

Thank you!


原文:https://stackoverflow.com/questions/36726046
更新时间:2023-12-31 20:12

最满意答案

使用附加的状态变量,例如索引变量(通常用于C或PHP语言),被认为是非pythonic。

更好的选择是使用内置函数enumerate() ,可用于Python 2和3:

for idx, val in enumerate(ints):
    print(idx, val)

查看PEP 279了解更多。


Using an additional state variable, such as an index variable (which you would normally use in languages such as C or PHP), is considered non-pythonic.

The better option is to use the built-in function enumerate(), available in both Python 2 and 3:

for idx, val in enumerate(ints):
    print(idx, val)

Check out PEP 279 for more.

相关问答

更多
  • tldr,再次; CoffeeScript的作者告诉我, 我是对的 :不要使用_i 。 14:29 You shouldn't use internal variables. ... 14:42 I was hoping something more deeply involved in the language would be able to put some authority behind that opinion 14:43 ... ...
  • 使用附加的状态变量,例如索引变量(通常用于C或PHP语言),被认为是非pythonic。 更好的选择是使用内置函数enumerate() ,可用于Python 2和3: for idx, val in enumerate(ints): print(idx, val) 查看PEP 279了解更多。 Using an additional state variable, such as an index variable (which you would normally use in languag ...
  • 一个简单的方法是使用python的切片符号 - 请参阅基本教程中的部分。 https://docs.python.org/2/tutorial/introduction.html for item in ints[1:]: 这将迭代你的列表跳过第0个元素。 由于您实际上并不需要索引,因此该实现比不必要地维护索引的实现更清晰 A simple way is to use python's slice notation - see the section in the basic tutorial. https ...
  • 前两个基本相同,因为它们将在各种情况下运行相同数量的迭代,并且将index保持在相同的状态。 如果用index.append(a+1)替换index.append(a+1) index.append(a) , for循环将永远运行; 同样,如果用index.append(index[-1] + 1)替换index.append(index[-1] + 1) index.append(index[-1]) , while循环将永远运行。 修改一个你正在迭代的列表有点令人担忧,但它有时是一种合理的策略。 顺便说 ...
  • 如果我理解你要做什么,我不确定你需要count和count2 。 我想你可以使用循环生成的数字。 我建议使用enumerate()而不是range(len()) 。 for i1,rlength1 in enumerate(records): ex = rlength1.id for i2,rlength2 in enumerate(records): if re.search(ex, rlength2.id) != None: print rleng ...
  • 编辑 在你重大改变的批次(实际上是一个新问题)改变 call echo !input[%x%]! 至 call echo %%input[!x!]%% 如果在(代码块)中,您可能需要延迟扩展来强制实际值,如果同时使用索引变量,则需要另一个级别的延迟扩展,您可以使用伪调用完成,并将百分号加倍 Call set Input=%%input[!x!]%% mediainfo --Output=Video;%%Height%% !input! > %temp%\ffmpegres%x%.txt EDIT ...
  • 通常,您有一个嵌套的总和: sum(sum(sum(1,i3=0..i2),i2=0..i1),i1=0..n-1) 你可以使用这些基本的求和公式从内到外简化。 最终结果将仅依赖于i作为izomorphius评论,因为N是i1的上限,i1是i2的上限,... 编辑:好的,通过编辑,我现在看到你有一个不同的问题。 现在它有点复杂但仍然没有问题。 我们需要为此分割一些东西。 我将计算值k = f(4,3,1)。 在那个时间点,我们将执行4个完整的i1循环(i1 = 0,1,2,3)并且正在执行第五个循环。 可 ...
  • 我将此作为第二个答案添加,因为它使用的是另一种语言(现在为C),并且具有更直接的方法。 我保留原来的答案,因为如果没有它,下面的代码几乎是无法解释的。 我将我的两个函数合并为一个函数来减少函数调用开销。 另外,为了100%确定它回答了原始问题,我逐字地使用了该问题的循环。 在驱动程序函数中,我明确地显示输出对于N = 4是正确的,然后对N = 10000进行压力测试(总共100,000,000次通过内部循环)。 我没有任何正式的计时代码,但我的机器需要大约1秒才能运行并测试这些1亿个案例。 我的代码假定为3 ...
  • 如果您希望内部部件是列表,那么您可以将enumerate结果转换为列表理解中的列表: >>> mylist = ["aus","ausser","bei","mit","noch","seit","von","zu"] >>> [[idx, item] for idx, item in enumerate(mylist, 1)] [[1, 'aus'], [2, 'ausser'], [3, 'bei'], [4, 'mit'], [5, 'noch'], [6, 'seit'], [7, 'v ...
  • 你说element3我假设你的意思是element2 for (var i = 0; i < 2; i++) { console.log('i:', i); ["element0", "element1", "element2"].forEach(function (element, index) { console.log('element:', element, 'index:', i); }); } 你使用i作为循环计数器和forEach索引 you said element3 i'm as ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)