Jump to content

Recommended Posts

Posted

É 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?

Posted

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

Posted

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

Posted

Pois pode , obrigado 🙂

Mas como indico os dois pra fazer pattern-matching.?  ->funcao (Teste a b c | Teste1 e f ) | Teste =c

                                                                                                                                                |otherwise =f

Posted

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

Posted

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)?

Posted

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.

Posted

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 ? :|

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.