简体   繁体   English

Haskell-无限列表-

[英]Haskell - infinite list -

I have this data and types: 我有此数据和类型:

data Cliente = Uncliente {nombre::String,resistencia::Int,bebidas::[Bebida],amigos::[Cliente]} deriving (Show) 

type Bebida = Cliente -> Cliente 

type Nombre = String
type Duracion = Float
type Acciones = [Bebida]
type Itinerario = (Nombre,Duracion,Acciones)

And I have this slogan: 我有这个口号:

"Define chuckNorris, who is a customer who was initially called "Chuck", has resistance 1000, is Ana's friend and took all sodas that exist in the universe, starting with a level 1 soda, then a level 2 soda, and So on." “定义chuckNorris(最初被称为“ Chuck”的客户,抵抗力为1000)是Ana的朋友,从宇宙中吸收了所有苏打,从1级苏打开始,然后是2级苏打,依此类推。 “

and i do: 我做:

chuckNorris = Uncliente {
    nombre = "Chuck",
    resistencia = 1000,
    bebidas = [soda 1,2..],
    amigos = [ana]

But this does not work because the infinite list of drinks is not like that 但这是行不通的,因为无限量的饮料不是那样的

How would you write the infinite list? 您将如何编写无限列表?

like soda 1, soda 2, soda 3 ........ 像苏打1,苏打2,苏打3 ........

I'd write it as map soda [1,2..] 我把它写成map soda [1,2..]

map effectively takes each element in the list (1, 2, 3 and so on) and applies soda to it, resulting in another list with soda 1 , soda 2 , soda 3 and so on. map有效地获取列表中的每个元素(1、2、3等),并对其应用soda ,从而生成另一个包含soda 1soda 2soda 3等等的列表。

The way to do this is via the map function. 做到这一点的方法是通过map功能。 Infinite lists which step by 1 can be done with the .. syntax. 这一步无限列表1可以用做..语法。

map soda [1..]

This is semantically equivalent to something like 这在语义上等同于类似

[soda 1, soda 2, soda 3, {- and so on... -}]

The reason this works is thanks to Haskell's lazy evaluation. 之所以能够成功,是因为Haskell的懒惰评估。 The calls to soda only occur once you start traversing the infinite list, so no need to fear getting caught in an infinite loop here. 仅在您开始遍历无限列表时才发生对soda的调用,因此无需担心会陷入无限循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM