Empecé a estudiar Haskell hace una semana y tengo un problema extraño. Creé un tipo de datos simple y quiero mostrarlo en una consola. Creé 2 funciones para 2 constructores de mi tipo. El compilador puede llamar a la función si uso un constructor con 2 argumentos. Pero no puede llamar a otra función que atrape un constructor con 1 argumento.Funciones para diferentes constructores de datos
module Main (
main
) where
data MyContainter a b = FirstVersion a b
| SecondVersion a
deriving(Show,Eq)
showContainer (FirstVersion a b) = show b
showContainer (SecondVersion a) = show a
--startF = showContainer (FirstVersion 1 2) -- it works
startF = showContainer (SecondVersion 1) -- it doesn't work
main = putStr startF
Los compiladores dice:
Ambiguous type variable `a0' in the constraint:
(Show a0) arising from a use of `showMaybe'
Probable fix: add a type signature that fixes these type variable(s)
In the expression: showMaybe (SecondVersion 1)
In an equation for `startF': startF = showMaybe (SecondVersion 1)
¿Por qué dice eso? Creé (SecondVersion 1) directamente y no entiendo por qué el compilador no llama a showContainer (SecondVersion a).
Ayuda a darse cuenta de que la variable de tipo 'a0' en el mensaje de error es igual que' b', y no tiene nada que ver con 'a'. (El compilador simplemente eligió ese nombre porque la clase 'Show' usa el nombre' a'). – hammar