Implementing an unusual sequence as an infinite list in Haskell

haskell infinite list of 1
infinite data structures haskell
haskell infinite list of primes
haskell list comprehension
haskell infinite list of natural numbers
haskell loop through list
haskell list of lists
generate sequence haskell

I have two elements as the beginning of the list [1, 2]

This unusual sequence is that it replicates the digits in the number of digits of a certain type that follows the three elements. For example, after 1 and 2, we would have another 2, followed by two 1's. The first few elements of the desired list would yield

[1, 2, 2, 1, 1, 2, 1, 2, 2, 1, 2, 2, 1, 1, 2, 1, 1, 2, 2] because

1  2   2  1 1   2  1  2   2  1   2   2

where the previous digits represent the length of the mini-sequences of the same digit.

So far I've tried using the replicate function for repeating the same digit based on an element earlier in the list.

selfrle :: [Int]
selfrle = 1 : 2 : [x | a <- [0..], let x = replicate (selfrle !! a) (selfrle !! (a + 1))) ]

The problem is I can't figure out why it's not working.

Not that unusual as it appears in the OEIS at and is named there as the Kolakoski sequence. There they even give a Haskell program by John Tromp dated 2011:

a = 1:2: drop 2 (concat . zipWith replicate a . cycle $ [1, 2])

Infinite list tricks in Haskell, In this article we use simple sequences as lists of infinite length in a Haskell gives two ways of implementing this sequence as an infinite list  Another common example when demonstrating infinite lists is the Fibonacci sequence -- Wikipedia's page on Haskell gives two ways of implementing this sequence as an infinite list -- I'll add

The first thing you can do it get your types right. replicate builds a list, so the x in your list comprehension has the type [Int], and the entire list comprehension is a list of all the x values, and its type is [[Int]]. You cannot use a list of lists of Int as the tail of a list, when the first two elements are 1 and 2. The types just don't match: you need to decide whether this is a list of Int, or a list of lists of Int.

Based on your description, I suspect you want to fix this by flattening the list comprehension using concat, like so:

selfrie = 1 : 2 : concat [x | a <- [0..],
                              let x = replicate (selfrie !! a) (selfrie !! (a + 1))]

If you try this, you won't get the list you described. I don't know how to help with the next part, and that's because I don't understand your description of the desired list. This isn't a programming question so much as a specification question, so perhaps you could go back to the original source and see how it's explained there?

The Infinite In Haskell and Python, The simplest example we will start with is generating an infinite list of idea to define the infinite sequence, namely that of self-similarity. Noting that the zeros in the ruler function are at exactly where the odd numbers are in the natural To implement this idea in Haskell, first define a filterOutDivisibleBy  The above expression evaluates to 1, even though the list argument is an infinite list and cannot, therefore, be already evaluated. If we expand that expression given the definition of foldr , we

We can write this slightly more elegant than @DanD's answer, by using an extra variable b on which we "tie a knot":

a :: [Int]
a = 1 : 2 : b
    where b = 2 : concat (zipWith replicate b (cycle [1, 2]))

We here thus first yield 1 : 2 : b, with b that starts with 2 and then the concatenation of the replicates of b with an endless list with [1, 2, 1, 2, …].

Higher Order Functions, Now we're going to use higher order programming to implement a really useful function that's in the Next up, we're going to find the sum of all odd squares that are smaller than 10,000. First, we'll begin by mapping the (^2) function to the infinite list [1..]. For our next problem, we'll be dealing with Collatz sequences. The result is a list of infinite lists of infinite lists. The specification of list comprehensions is given in The Haskell 98 Report: 3.11 List Comprehensions. The GHC compiler supports parallel list comprehensions as an extension; see GHC User's Guide: 7.3.4. Parallel List Comprehensions. List monad. In the first versions of Haskell, the

Implementation and Application of Functional Languages: 20th , by a corecursive program that operates on streams, which are infinite sequences. is based on a single proof method: the principle of unique fixed points [7]. The type of streams, Stream α, is like Haskell's list datatype [α], except that there  Finite sequences. The Seq a type represents a finite sequence of values of type a. Sequences generally behave very much like lists. The class instances for sequences are all based very closely on those for lists. Many functions in this module have the same names as functions in the Prelude or in Data.List. In almost all cases, these functions

[PDF] 5.4 Arithmetic Sequences, It means the sequence of odd numbers between 1 and 99. • Haskell lists Haskell's data dependency driven computation constructs the next element of the list only when foldl Hardest: Write an iterator for the conceptually infinite list of primes: mth call produces the mth and are implemented as Haskell lists. Boxes are  Because Haskell supports infinite lists, our recursion doesn't really have to have an edge condition. But if it doesn't have it, it will either keep churning at something infinitely or produce an infinite data structure, like an infinite list. The good thing about infinite lists though is that we can cut them where we want. repeat takes an element and returns an infinite list that just has that element. A recursive implementation of that is really easy, watch.

[PDF] Haskell Cheat Sheet, Infinite list of numbers – 100,. 101, 102 signature of the function—no implementation is given here for equality, or order in a sequence. otherwise = "odd!". The result is a list of infinite lists of infinite lists. The specification of list comprehensions is given in The Haskell 98 Report: 3.11 List Comprehensions.. The GHC compiler supports parallel list comprehensions as an extension; see GHC 8.10.1 User's Guide 9.3.13.

A Gentle Introduction to Haskell: Functions, As an example of the use of map, we can increment the elements in a list: on efficiency, since an implementation can be expected to implement the list as a true circular sequence can be computed efficiently as the following infinite sequence: This is a somewhat odd function: From its type it looks as if it is returning a  The sequence of Fibonacci n-step numbers are formed by summing n predecessors, using (n-1) zeros and a single 1 as starting values: Note that the summation in the current definition has a time complexity of O(n) , assuming we memoize previously computed numbers of the sequence.