4

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."

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 ........

2 Answers2

3

I'd write it as 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.

user253751
  • 57,427
  • 7
  • 48
  • 90
3

The way to do this is via the map function. Infinite lists which step by 1 can be done with the .. syntax.

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. 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.

pyrospade
  • 7,870
  • 4
  • 36
  • 52