Recursive definition of the predefined function EnumFromThenTo

1

How to recursively define the EnumFromThenTo function

    
asked by anonymous 30.10.2017 / 18:51

1 answer

0

The function enumFromThenTo returns a sequence beginning with the first parameter of the function (in most cases), followed by its successors according to a certain distance (difference between the first and second parameters) until it reaches the end of the sequence (third parameter of the function).

Entrada: enumFromThenTo 1 3 7 
Saída: [1,3,5,7]

Entrada: enumFromThenTo 2 6 8 
Saída: [2,6] 

Here is an example of a recursive implementation for the universe of integers:

myEnumFromThenTo:: Int -> Int -> Int -> [Int]
myEnumFromThenTo p d u 
    | d == 0 = []
    | u < d = [p]
    | otherwise = p : myEnumFromThenTo d (2 * d - p) u

Note that this is just an example for the integers. The enumFromThenTo function of the Prelude library, with enumFromThenTo :: Enum a => a -> a -> a -> [a] signature can be applied to class Enum type, so its definition is much more complex.

The generic function implementation is as an exercise:)

    
28.11.2017 / 16:39