crislanio_macedo Posted May 25, 2014 at 12:11 AM Report #556897 Posted May 25, 2014 at 12:11 AM Olá Pessoal para fazer instância de uma classe têm-se que instanciar uma 'coisa' obrigatória dessa classe, exemplo para instanciar a classe Show tem que ter uma instância de show. Mas estou em ter muitas dificuldades em saber o que é obrigatório de cada classe e o que é para se fazer ? Uns exemplos são das classes Bounded, Enum e Maybe como proceder com as instâncias dessas classes ? Alguém teria algum tutorial de como fazer instâncias desses classes que possa ajudar ?
Baderous Posted May 25, 2014 at 08:45 PM Report #556940 Posted May 25, 2014 at 08:45 PM As funções obrigatórias de criar para cada instância estão indicadas na documentação das classes.
Rui Carlos Posted May 26, 2014 at 05:24 PM Report #557016 Posted May 26, 2014 at 05:24 PM No GHCi, também podes escrever :i Enum para obter informações sobre a classe. No entanto, para saberes o mínimos que é necessário implementar, penso que só mesmo indo à documentação (onde é indicada a Minimal complete definition). 1 Report Rui Carlos Gonçalves
crislanio_macedo Posted May 28, 2014 at 02:11 AM Author Report #557287 Posted May 28, 2014 at 02:11 AM (edited) Qual a diferença básica do tipo Int para Integer, estou a ter problemas em um contexto de um programa que fiz(para ter aceso a uns índices)e acredito que teria que fazer uma espécie de conversão de Int para Integer, seria possível.? data Int = GHC.Types.I# GHC.Prim.Int# -- Defined in `GHC.Types' instance Bounded Int -- Defined in `GHC.Enum' instance Enum Int -- Defined in `GHC.Enum' instance Eq Int -- Defined in `GHC.Classes' instance Integral Int -- Defined in `GHC.Real' instance Num Int -- Defined in `GHC.Num' instance Ord Int -- Defined in `GHC.Classes' instance Read Int -- Defined in `GHC.Read' instance Real Int -- Defined in `GHC.Real' instance Show Int -- Defined in `GHC.Show' Prelude> :info Integer data Integer = integer-gmp:GHC.Integer.Type.S# GHC.Prim.Int# | integer-gmp:GHC.Integer.Type.J# GHC.Prim.Int# GHC.Prim.ByteArray# -- Defined in `integer-gmp:GHC.Integer.Type' instance Enum Integer -- Defined in `GHC.Enum' instance Eq Integer -- Defined in `integer-gmp:GHC.Integer.Type' instance Integral Integer -- Defined in `GHC.Real' instance Num Integer -- Defined in `GHC.Num' instance Ord Integer -- Defined in `integer-gmp:GHC.Integer.Type' instance Read Integer -- Defined in `GHC.Read' instance Real Integer -- Defined in `GHC.Real' instance Show Integer -- Defined in `GHC.Show' Prelude> Veja o erro que estou a ter, não consigo fazer com que tenha sucesso até o momento.? Não estou a saber proceder! module Calendar (Cal,calendario, calendario_mes, semana,meses) where import Data.Time.Calendar as C -- funções de calendário import Data.Time.Calendar.WeekDate import qualified System.Random as R import Data.List as L data Cal a= Cal[a] deriving (Read, Eq, Ord, Show) -- iniciais dos dias da semana semana :: [string] semana = ["S","T","Q","Q","S","S","D"] -- nomes dos meses meses :: [string] meses = ["Janeiro", "Fevereiro", "Marco", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"] direita :: Int -> String -> String direita n txt = reverse (take n (reverse txt ++ repeat ' ')) calendario_mes :: Integer -> Integer -> String calendario_mes y m =titulo where titulo = (meses!!(m-1)) ++ " de " ++ show y -- dia da semana do inicio do mes (_, _, dia1) = toWeekDate (fromGregorian y m 1) ndias = gregorianMonthLength y m -- número de dias do mes names = [[direita 3 d] | d<-semana] -- nomes dos dias entradas = map dia_do_mes [2-dia1..43-dia1] dia_do_mes d | d>=1 && d<=ndias = [direita 3 (show d)] | otherwise = [replicate 3 ' '] calendario y = year where year = [ (calendario_mes y m) | m<-[1..12]] fnc :: Integer -> IO Integer fnc m = R.randomRIO (1, m) inputDia d m a = do d <- fnc 31 m <- fnc 12 a <- fnc 2014 let r =show d ++ (calendario_mes a d) let s =show d ++ " de " ++ show m ++ " de " ++ show a return s printRandDates = do s <- inputDia 31 12 2014 putStrLn s {- printDates d m y =(calendario_mes y m) ++ " de " ++ show d where (do d <- R.randomRIO(1,31) m <- R.randomRIO(1,12) y <- R.randomRIO(2000,2014) return ()) -} {- dayMesAno.hs:27:29: Couldn't match expected type `Int' with actual type `Integer' In the first argument of `(-)', namely `m' In the second argument of `(!!)', namely `(m - 1)' In the first argument of `(++)', namely `(meses !! (m - 1))' dayMesAno.hs:29:55: Couldn't match expected type `Int' with actual type `Integer' In the second argument of `fromGregorian', namely `m' In the first argument of `toWeekDate', namely `(fromGregorian y m 1)' In the expression: toWeekDate (fromGregorian y m 1) dayMesAno.hs:30:42: Couldn't match expected type `Int' with actual type `Integer' In the second argument of `gregorianMonthLength', namely `m' In the expression: gregorianMonthLength y m In an equation for `ndias': ndias = gregorianMonthLength y m Failed, modules loaded: none. -} Edited May 28, 2014 at 08:53 AM by Baderous geshi
thoga31 Posted May 28, 2014 at 10:10 AM Report #557319 Posted May 28, 2014 at 10:10 AM Qual a diferença básica do tipo Int para Integer, estou a ter problemas em um contexto de um programa que fiz(para ter aceso a uns índices)e acredito que teria que fazer uma espécie de conversão de Int para Integer, seria possível.? http://lmgtfy.com/?q=haskell+int+vs+integer Knowledge is free!
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now