"fromJust" versus "Just =" in Haskell

Haskell is new here. I am trying to understand this example from the Haskell library diagrams. In particular, there is such a line:

Just t = <thing>where <thing>has typeMaybe (Tree a)

I do not understand what this does. I understand that we need to get the value from Maybe. I replaced this line of code with

t = fromJust <thing>

and it works the same way. Is there a difference between the two lines, and can anyone explain what the first line does?

+4
source share
3 answers

fromJust pretty much equivalent to:

fromJust :: Maybe a -> a
fromJust (Just t) = t

, ! , Maybe Just, Nothing, fromJust, , , .

+9

,

. (fromJust , , )

$ ghci
Prelude> :m +Data.Maybe

Prelude Data.Maybe> let t = fromJust Nothing
Prelude Data.Maybe> t
*** Exception: Maybe.fromJust: Nothing

Prelude Data.Maybe> let Just t = Nothing
Prelude Data.Maybe> t
*** Exception: <interactive>:7:5-20: Irrefutable pattern failed for pattern Data.Maybe.Just t
+3

, Maybe Just, .

:

Just t = Just 3 3 t. ghci . fromJust , .

+1

Source: https://habr.com/ru/post/1541666/


All Articles