Aaron
Aaron

Reputation: 75

Recursive Lists

I would like to create an infinite list as follows

(1:[]):(2:2:[]):(3:3:3:[]) ...

I have tried a lot and the best I can think of is

function = (1:[]) : map ( \n -> n ) (  from 2 )

I am sure that this need to be done recursively but I cant see it

any tips or help would be great thanks

Upvotes: 1

Views: 107

Answers (2)

isanco
isanco

Reputation: 86

Here is a proposal for a recursive definition:

lss = [1]:map (\ls -> (1+head ls):(map (+1) ls)) lss

(even though the replicate solution is shorter!)

Upvotes: 1

somesoaccount
somesoaccount

Reputation: 1267

How about this one:

concatMap (\x -> replicate x x) [1..]

It has no explicit recursion but does what you want.

As Keshav Kini points out you probably want the version without concatenation:

map (\x -> replicate x x) [1..]

Upvotes: 3

Related Questions