Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a other way to use Maybe on my path search function through the BTree? (Haskell)

I want to be able to specify an element and the tree and the function should give me a list of directions and if the element is not contained it should not return an error but rather a "Nothing" made by Maybe.

data BTree a = Nil | Node a ( BTree a) ( BTree a) deriving Show

data Direction = L | R deriving (Show , Eq)

 getPath :: (Ord a) => a -> BTree a -> Maybe [Direction]
   getPath y (Node x lt rt)
    | (Node x lt rt) == Nil = Nothing
    | y == x = Just []
    | y < x = Just (L:(getPath y lt))
    | otherwise = Just (R:(getPath y rt))

But with this code i am getting an error:

    * Couldn't match expected type: [Direction]
                  with actual type: Maybe [Direction]
    * In the second argument of `(:)', namely `(getPath y lt)'
      In the first argument of `Just', namely `(L : (getPath y lt))'
      In the expression: Just (L : (getPath y lt))
   |
57 |   | y < x = Just (L:(getPath y lt))

    * Couldn't match expected type: [Direction]
                  with actual type: Maybe [Direction]
    * In the second argument of `(:)', namely `(getPath y rt)'
      In the first argument of `Just', namely `(R : (getPath y rt))'
      In the expression: Just (R : (getPath y rt))
   |
58 |   | otherwise = Just (R:(getPath y rt))

It would be great if someone could help me.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The reason Just (L : getPath y lt) does not work is because getPath y lt is a Maybe [Direction], not a [Direction], hence it is not a list, and you thus can not prepend this with L or R.

You can make use of fmap :: Functor f => (a -> b) -> f a -> f b to perform a mapping on the item wrapped in the Just data constructor, and fmap f Nothing will return Nothing.

You thus can implement this as:

getPath :: Ord a => a -> BTree a -> Maybe [Direction]
getPath Nil = Nothing
getPath y (Node x lt rt)
    | y == x = Just []
    | y < x = fmap (L:) (getPath y lt)
    | otherwise = fmap (R:) (getPath y rt)

The condition (Node x lt rt) == Nil can never be True: you should pattern match on Nil, and in that case return Nothing.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading