Jump to content

Recommended Posts

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

Posted

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

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.