Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"fromJust" vs. "Just =" in Haskell

Haskell newbie here. I'm trying to understand this example from Haskell's diagrams library. Specifically, there's a line like this:

Just t = <thing> where <thing> is of type Maybe (Tree a)

I don't understand what this is doing. I understand that we need to get the value out from the Maybe. I replaced that line of code with

t = fromJust <thing>

and it works the same. Is there any difference between the two lines, and can anyone explain what the first line is doing?

like image 636
Eli Rose Avatar asked Dec 04 '22 06:12

Eli Rose


1 Answers

fromJust is pretty much equivalent to:

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

Note that it’s the same pattern match! If you’re sure that your Maybe going to be a Just and not a Nothing, you can use fromJust to get its value without pattern matching, but matching is cleaner in most cases, so you don’t need it here.

like image 52
Ry- Avatar answered Jan 01 '23 21:01

Ry-