在Haskell中如何实现列表推导? [英] How are list comprehensions implemented in Haskell?

查看:81
本文介绍了在Haskell中如何实现列表推导?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 列表理解仅仅是语言功能吗?
  • 使用纯Haskell伪造列表理解的最简单方法是什么?
  • 您是否必须使用do块/>> = 来执行此操作,或者可以使用其他方法一起破解列表理解的方法?
  • Are list comprehensions simply a language feature?
  • What's the easiest way to fake a list comprehension using pure Haskell?
  • Do you have to use a do block/>>= to do this or could you use some other method for hacking a list comprehension together?

澄清:通过伪造"列表理解,我的意思是创建一个函数,该函数接受相同的输入并产生相同的输入,即返回值的形式,列表要紧缩在一起以及谓词或多个谓词.

Clarification: By "fake" a list comprehension I mean create a function that takes the same input and produces the same input, i.e. a form for the return values, lists to crunch together, and a predicate or multiple predicates.

推荐答案

第3.11节在Haskell报告中准确地描述了列表理解的含义,以及如何将其翻译掉.

Section 3.11 in the Haskell report describes exactly what list comprehensions mean, and how to translate them away.

如果您想要单子理解,则基本上需要将 [e] 替换为 return e ,将 [] 替换为 mzero concatMap ,由(>> =)进行翻译.

If you want monad comprehensions you basically need to replace [e] by return e, [] by mzero, and concatMap by (>>=) in the translation.

这篇关于在Haskell中如何实现列表推导?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆