Recursive definition of the pre-closed Enumfromthento function

Asked

Viewed 126 times

1

How to recursively define the Enumfromthento function

  • The idea of this site is to help with the solution, tried something?

1 answer

0

The function enumFromThenTo returns a sequence starting at the first parameter of the function (in most cases), followed by its successors according to a given distance (difference between the first and the second parameter) until the end of the sequence is reached (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 integers. The function enumFromThenTo library Prelude, signature enumFromThenTo :: Enum a => a -> a -> a -> [a] can be applied to class Enum types, so its definition is much more complex.

The implementation of the Generic function remains as exercise :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.