3
I have these two functions:
sequenciaInt :: Int -> Int -> [Int]
sequenciaInt i j |j >= i = [i..j]
|otherwise = sequenciaInt j i
sequenciaChar :: Char -> Char -> [Char]
sequenciaChar i j |j >= i = [i..j]
|otherwise = sequenciaChar j i
A function returns a list of Int
and the other a list of Char
.
It is possible to have a generic function that returns a list of Int
or Char
depending on the parameter passed (i.e. Int
or Char
)?
Something like:
sequencia :: a -> a -> [a]
sequencia i j |j >= i = [i..j]
|otherwise = sequencia j i
However, this above code returns error:
main.hs:2:16: error:
• No instance for (Ord a) arising from a use of ‘>=’
Possible fix:
add (Ord a) to the context of
the type signature for:
sequencia :: forall a. a -> a -> [a]
• In the expression: j >= i
In a stmt of a pattern guard for
an equation for ‘sequencia’:
j >= i
In an equation for ‘sequencia’:
sequencia i j
| j >= i = [i .. j]
| otherwise = sequencia j i
|
2 | sequencia i j |j >= i = [i..j]
| ^^^^^^
main.hs:2:25: error:
• No instance for (Enum a)
arising from the arithmetic sequence ‘i .. j’
Possible fix:
add (Enum a) to the context of
the type signature for:
sequencia :: forall a. a -> a -> [a]
• In the expression: [i .. j]
In an equation for ‘sequencia’:
sequencia i j
| j >= i = [i .. j]
| otherwise = sequencia j i
|
2 | sequencia i j |j >= i = [i..j]
| ^^^^^^
Thank you so much, solved the error of my code. I am beginner in Haskell, your explanation and link are useful.
– Henrique Silva