Jump to content

Recommended Posts

Posted

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 ?

Posted (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 by Baderous
geshi

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.