Polmax Posted January 18, 2012 at 03:54 PM Report #433298 Posted January 18, 2012 at 03:54 PM É possível fazer isto certo ? data Teste = Teste {nome::String, ano::Int, numero::Int} funcao ::Teste->Int funcao (Teste a b c) =c --Ter uma função que me transforme um data num dos seus argumentos --é possível fazer isto ? data Teste = Teste {nome::String,ano::Int,numero::Int} data Teste1 =Teste1{nome::String,numero::Int} data Testef = T Teste |T1 Teste1 funcao ::Testef ->Int funcao (Pega num Testf ) = devolve numero que seja um Teste ou um Teste1 Alguém me podia dar uma ajuda?
Betovsky Posted January 18, 2012 at 04:02 PM Report #433302 Posted January 18, 2012 at 04:02 PM Sim, é possivel. Usas pattern-matching. "Give a man a fish and he will eat for a day; Teach a man to fish and he will eat for a lifetime. The moral? READ THE MANUAL !" Sign on a computer system consultant's desk
Betovsky Posted January 18, 2012 at 04:05 PM Report #433304 Posted January 18, 2012 at 04:05 PM Já agora. O teu primeiro exemplo pode ser simplificado para apenas: data Test = Teste {nome::String, ano::Int, numero::Int} funcao :: Teste -> Int funcao = numero "Give a man a fish and he will eat for a day; Teach a man to fish and he will eat for a lifetime. The moral? READ THE MANUAL !" Sign on a computer system consultant's desk
Polmax Posted January 18, 2012 at 04:15 PM Author Report #433305 Posted January 18, 2012 at 04:15 PM Pois pode , obrigado 🙂 Mas como indico os dois pra fazer pattern-matching.? ->funcao (Teste a b c | Teste1 e f ) | Teste =c |otherwise =f
Betovsky Posted January 18, 2012 at 04:26 PM Report #433308 Posted January 18, 2012 at 04:26 PM Nops. Usas a mesma lógica, que por exemplo listas. Metes um caso por linha. funcao (Teste x) = ... funcao (Teste1 x) = ... "Give a man a fish and he will eat for a day; Teach a man to fish and he will eat for a lifetime. The moral? READ THE MANUAL !" Sign on a computer system consultant's desk
Polmax Posted January 18, 2012 at 05:09 PM Author Report #433315 Posted January 18, 2012 at 05:09 PM Ah agora consegui 😛 E surgiu outro probleminha data Teste = Teste {nome::String,ano::Int,numero::Int} deriving Show data Teste1 =Teste1{nome1::String,numero1::Int} deriving Show data Testef = T Teste |T1 Teste1 deriving Show funcao ::Testef ->Int funcao (T (Teste a b c)) =c funcao (T1 (Teste1 a b))=b ordenar ::(Ord b)=>(a->b)->[a]->[a] ordenar f []=[] ordenar f [x]=x ordenar f (x:x1:xs) |(f x) > (f x1) =x:ordenar f xs |otherwise =x1:ordenar f xs --nesta linha -> ordenar f [x]=x , eu quero dizer que se a lista só tiver um elemento da ele próprio , não é assim que se escreve? Ah agora consegui 😛 E surgiu outro probleminha data Teste = Teste {nome::String,ano::Int,numero::Int} deriving Show data Teste1 =Teste1{nome1::String,numero1::Int} deriving Show data Testef = T Teste |T1 Teste1 deriving Show funcao ::Testef ->Int funcao (T (Teste a b c)) =c funcao (T1 (Teste1 a b))=b ordenar ::(Ord b)=>(a->b)->[a]->[a] ordenar f []=[] ordenar f [x]=x ordenar f (x:x1:xs) |(f x) > (f x1) =x:ordenar f xs |otherwise =x1:ordenar f xs --nesta linha -> ordenar f [x]=x , eu quero dizer que se a lista só tiver um elemento da ele próprio , não é assim que se escreve? pois claro -.- tem de ser = [ x ] , distracção minha desculpa E ja agora uma ultima coisa data Teste = Teste {nome::String,ano::Int,numero::Int} deriving Show data Teste1 =Teste1{nome1::String,numero1::Int} deriving Show data Testef = T Teste |T1 Teste1 deriving Show funcao ::Testef ->Int funcao (T (Teste a b c)) =c funcao (T1 (Teste1 a b))=b ordenar ::(Ord b)=>(a->b)->[a]->[a] ordenar f []=[] ordenar f [x]=[x] ordenar f (x:x1:xs) |(f x)>(f x1) =x:ordenar f (x1:xs) |otherwise =x1:ordenar f (x:xs) pra dois elementos a função vai fazendo o que eu quero , meio aldrabado mas vai . como faço pra escrever esta condição também ->> ordenar f (x:x1:xs) |(f x)>(f x1) && f x > (f da calda) =x:ordenar f (x1:xs)?
Baderous Posted January 18, 2012 at 06:02 PM Report #433322 Posted January 18, 2012 at 06:02 PM 1º - Não se diz "calda", isso é uma cena de culinária. É "cauda", que é toda a lista excepto o 1º elemento (em Inglês, "tail"). 2º - f xs dá erro, porque xs é uma lista. Quando muito tinhas de fazer um map. Mas depois disso não sei o que queres fazer.
Polmax Posted January 18, 2012 at 06:13 PM Author Report #433325 Posted January 18, 2012 at 06:13 PM Eu sei o que é a cauda lol , pensei era que se escrevia com l comparar (x:x1:xs) o x com o x1 -> x>x1 e depois comparar o x com o resto dos elementos de xs , se passar as duas condições da-me o x a cabeça e vou recursivamente comparar o x1 com o xs pelo mesmo processo. se não passar as duas condições é pk o x1 é maior , fica a cabeça e pego no x e comparo com todos os elementos da calda . Alguém sabe como faço isso ? :|
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