Isac Carneiro Posted February 6, 2014 at 03:14 PM Report #544220 Posted February 6, 2014 at 03:14 PM data Tree a = Empty | Leaf a | Fork (Tree a) (Tree a) a1 = Fork (Fork (Leaf 4) (Fork Empty (Leaf 13))) (Fork Empty (Fork (Leaf 5) Empty)) ultimo :: Tree a -> Maybe a ultimo (Empty) = Nothing ultimo (Leaf a) = Just a ultimo (Fork e d) | d==Empty = (ultimo e) |otherwise = (ultimo d) Pretende-se devolver o ultimo elemento da direita da arvore. Caso não haja devolver nothing. eu tentei este código mas não está a funcionar alguma sugestão?
pdfrod Posted February 6, 2014 at 07:40 PM Report #544274 Posted February 6, 2014 at 07:40 PM Estás a usar o operador == para comparar dois elementos da àrvore, e no entanto a tua árvore não é uma instância de Eq. Vejos duas possívers soluções: - Tornar a árvore uma instância de Eq, e a alterar a assinatura da função ultimo para restringir a àrvores cujos elementos também são Eq (isto é: ultimo :: Eq a => Tree a -> Maybe a). - Ou então usar pattern matching em vez do operador de igualdade. Exemplo: ... ultimo (Fork e Empty) = ultimo e ultimo (Fork _ d) = ultimo d
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