4 ms·
I reluctantly confess that, knowing nothing about Haskell, I was lost by the 4th diagram. Presumably if you know Haskell, the line "data Maybe a = Nothing | Jus
by seldo 13y ago
I reluctantly confess that, knowing nothing about Haskell, I was lost by the 4th diagram. Presumably if you know Haskell, the line "data Maybe a = Nothing | Just a" makes sense, but I can't parse it.
- ominous_prime 13y agoI don't know Haskell, but it seems pretty clear: "something called a Maybe named 'a'; is equal to Nothing, or just 'a' itself" (hmm, I'm not sure that's a whole lot clearer :-/ )
- shoo 13y agoThe variable 'a' there is a placeholder for a type. e.g. loosely translating the idea of Maybe to C++ syntax (perhaps more familiar?) might give something resembling: Maybe<int> x = Just<int>(30); Maybe<int> y = Nothing<int>(); x and y are values of type Maybe<int>. I.e. we've plugged in the type int for the type variable a. Given an arbitrary value of type Maybe<int> you have two cases to consider - either it is a "Nothing" which contains no further data, or it is a "Just<int>" which contains an int value. You can think of the type Maybe<int> as being a "nullable int" type or perhaps a special collection of ints that can only contain 0 or 1 int value. (beware: my knowledge of both haskell & c++ is not fantastic)
- chongli 13y agoThis just defines a new type (called Maybe). The Maybe type can either contain a value or Nothing. This is useful in Haskell because regular values cannot be null. When a function has the potential to fail, it can use one of these Maybe types as its return type to let the caller know of (and handle) the possibility of failure. As for the syntax itself? It's called a tagged union. Just and Nothing are defined to be constructors for building values of type Maybe a. Just takes one argument (of any type) and Nothing takes no arguments. Just "hello" This has type Maybe String. Nothing This might also have type Maybe String (or it might not). A function which accepts a Maybe String as one of its arguments will accept Nothing as well.
- grn 13y agoThe line data Maybe a = Nothing | Just a introduces a new data type. Here a denotes another type which serves as a parameter (an example of a parametrized data type is a list; a list of integers, a list of strings, etc.). In this case we define a data type Maybe a that may contain a value of type a. On the right hand side we have type constructors. They describe different ways that can be used to construct a value of the type we're defening. Here we have two constructors: Nothing which takes no arguments and Just which takes one argument of type a. Assume that a is the type String. Then Nothing is a value of type Maybe String but it's an empty box that contains no value. On the other hand Just "Hello, World!" is a box with the specified string inside. Disclaimer: I'm not a Haskell expert. Feel free to correct me.
- chousuke 13y agoTo read that definition, you need to understand that Haskell has a notion of "type variables", and 'data' is used to introduce new algebraic data types, which you can think of as a sort of tagged union. The definition states "For all types a, the data type Maybe a consists of 'Nothing' or 'Just a'", where the a in 'Just a' is a value of type a. Nothing and Just are polymorphic constructors that create the values of any "Maybe a" type. So if you take a concrete type, say String, you get a new concrete type called "Maybe String" whose values are Nothing, Just "Hello", Just "World", etc. For Maybe Int the values are similarly Nothing (polymorphism!) and Just 1, Just 2, etc. Extra: 'a' can be any type so Maybe (Maybe Int) is a perfectly valid type, with values like Nothing, Just Nothing, and Just (Just 1). Something like that would be useful when retrieving a "possibly empty" value from a database: Nothing means it's not in the database, Just Nothing means it's there, but it's empty, and Just (Just x) means it's there and it has a value.
- qznc 13y agoRecently, I imlemented the Maybe, Monad, Applicative, and Functor in D. The code should be understandable, if you are comfortable with C++. It uses a mechanism, which is called "traits" in the C++ world. (Note that "traits" means something different in Scala and other languages.) https://bitbucket.org/qznc/d-monad/src/1ec4ca36e46df1d0fce4150e1a861bdab17749b1/generic.d?at=master https://bitbucket.org/qznc/d-monad/src/1ec4ca36e46df1d0fce41...
- Peaker 13y agogetValue kind of defeats the purpose of using Maybe in the first place.
- qznc 13y agoUsing getValue corresponds to extracting the value via pattern matching in Haskell. foo (Just a) = bar a For sure, Haskell has better syntax for this. ;)
- Peaker 13y agoIf you have: foo (Just a) = bar a It is typically followed, on the next line by: foo Nothing = ... And if it isn't, and you use -Wall (you really should!) you will get a warning for writing a partial function (which is sometimes warranted, but is a smell). The function "fromJust" which is equivalent to "getValue" is very rarely useful, and considered a smell: making that one of the primary ways to use your Maybe value is a very bad idea. if (isNothing (m)) { .. } else { .. m.getValue() .. } Is much less safe than: case m of Nothing -> { .. } Just val -> .. val .. And has no useful advantage...
- eli_gottlieb 13y agoActually, I use Option.get a lot in Scala code. It's very useful for saying, "I expect this Option[A] to be Some[A] every time the code reaches this point due to a proof I cannot (yet) convey to the type-checker, and if it's not, throw an exception for God's sake!"