Solved Infinite Lists Functions
Problem 1
The goal of this problem is to work the definition of infinite lists. In particular, you are required to define the function that generates the sequence of ones [1, 1, 1, 1, 1, 1, 1 ...]. Use the function ones:: [Integer]
take 8 ones -> [1, 1, 1, 1, 1, 1, 1, 1]
take 5 ones -> [1, 1, 1, 1, 1]
ones :: [Integer]
ones = repeat 1
take 5 $ repeat 'x'
The dollar sign in this expression tells Haskell that we first need to build the list with the repeat 'x' expression before using it with take 5
Problem 2
The goal of this problem is to work the definition of infinite lists. In particular, you are required to define the function that generates the sequence of the natural numbers [0, 1, 2, 3, 4, 5, 6, 7 ...]. Use the function nats :: [Integer]
take 8 nats -> [0, 1, 2, 3, 4, 5, 6, 7]
take 5 ones -> [0, 1, 2, 3, 4]
nats :: [Integer]
nats = iterate (+1) 0
Problem 3
The goal of this problem is to work the definition of infinite lists. In particular, you are required to define the function that generates the sequence of the integer numbers [0, 1, -1, 2, -2, 3, -3...] Use the function ints :: [Integer]
take 8 ints -> [0, 1, -1, 2, -2, 3, -3, 4]
take 5 ints -> [0, 1, -1, 2, -2]
ints :: [Integer]
ints = iterate integers 0
where
integers :: Integer -> Integer
integers x
| x > 0 = -x
| otherwise = 1 - x
Problem 4
The goal of this problem is to work the definition of infinite lists. In particular, you are required to define the function that generates the sequence of the triangular numbers [0, 1, 3, 6, 10, 15, 21, 28 ...] Use the function triangulars :: [Integer]
take 6 triangulars -> [0, 1, 3, 6, 10, 15]
take 4 triangulars -> [0, 1, 3, 6]
We will use the scanl function:
Input: scanl (/) 64 [4, 2, 4]
Output: [64.0, 16.0, 8.0, 2.0]
triangulars :: [Integer]
triangulars = scanl (+) 0 $ iterate (+1) 1
Problem 5
The goal of this problem is to work the definition of infinite lists. In particular, you are required to define the function that generates the sequence of the factorial numbers [1, 1, 2, 6, 24, 120, 720, 5040 ...] Use the function factorials :: [Integer]
take 6 factorials -> [1, 1, 2, 6, 24, 120]
take 4 factorials -> [1, 1, 2, 6]
Example: factorial of 6 (6!) is 1 * 2 * 3 * 4 * 5 * 6 = 720.
factorials :: [Integer]
factorials = scanl (*) 1 $ iterate (+1) 1
Problem 6
Fibonacci sequence : each number is the sum of the two preceding ones
The goal of this problem is to work the definition of infinite lists. In particular, you are required to define the function that generates the sequence of the Fibonacci numbers [0, 1, 1, 2, 3, 5, 8, 13 ...] Use the function fibs :: [Integer]
take 6 fibs -> [0, 1, 1, 2, 3, 5]
take 4 fibs -> [0, 1, 1, 2]
fibs :: [Integer]
fibs = fibo 0 1
where
fibo :: Integer -> Integer -> [Integer]
fibo x y = x : (fibo y (x + y))
Problem 7
Prime numbers: A prime number is a whole number greater than 1 whose only factors are 1 and itself.
The goal of this problem is to work the definition of infinite lists. In particular, you are required to define the function that generates the sequence of the prime numbers [2, 3, 5, 7, 11, 13, 17, 19 ...] Use the function primes :: [Integer]
take 6 primes -> [2, 3, 5, 7, 11, 13]
take 4 primes -> [2, 3, 5, 7]
primes :: [Integer]
primes = filter isPrime $ iterate (+1) 1
where
isPrime :: Integer -> Bool
isPrime 1 = False
isPrime 2 = True
isPrime n
| even n = False
| otherwise isPrimeAux 3
where
isPrimeAux :: Integer -> Bool
isPrimeAux x
| x >= div n 2 = True
| mod n x == 0 = False
| otherwise isPrimeAux (x + 2)