777P
777P

Reputation: 29

Understanding type

A few questions about the Haskell programming language:

  1. What's the difference between these two code statements? I'm convinced they should be the same:

    type T = [Char]
    type CurrentValue = Char  
    

    My concern is that, in the second one, there are no brackets.

    Anyway, they are actually declarations, aren't they?

  2. What is Maybe String?

    For instance: type P = (Char, Maybe String)

    Is that a function which has two arguments?

  3. What is Maybe Char?

    For instance : type D = ((Maybe Char) , Char)

    It's another function taking three arguments, right?

Upvotes: 1

Views: 226

Answers (4)

hammar
hammar

Reputation: 139910

  1. What's the difference between these two code statements?

    type T = [Char]
    type CurrentValue = Char  
    

    The first line declares a type alias T for [Char] which is a list of characters. The second line declares another type alias CurrentValue for Char, a single character.

  2. What is Maybe String?

    It is an application of the type constructor Maybe to the type String (which is just an alias for [Char]). It is similar to how the brackets turn a type into a list of that type, except Maybe makes things optional.

    For instance: type P = (Char, Maybe String)

    Is that a function which has two arguments ?

    No, that's a tuple type of two elements. The first element is a Char and the second is a Maybe String.

  3. What is Maybe Char ?

    For instance : type D = ((Maybe Char) , Char)

    It's another function having three arguments. Am I right?

    This is again a tuple type. The type of first element is Maybe Char and the second is Char. The inner parenthesis are redundant, so it's the same as type D = (Maybe Char, Char).

Upvotes: 6

Giorgio
Giorgio

Reputation: 5183

The type Char indicates one single character while [Char] indicates a list of characters (i.e. a string).

type T = [Char]

is equivalent to

type T = String

If you have a type t then, intuitively, Maybe t indicates a type that has the same values of t plus an extra undefined value (Nothing). In its use this corresponds (very) roughly to the null value for Java objects.

So while String and Char represent the string and character types, Maybe String and Maybe Char represent a string resp. chararacter that can also be non-defined.

Upvotes: 0

Michael Kohl
Michael Kohl

Reputation: 66857

Re 1) T is a type synonym for a list of characters ([Char]), whereas CurrentValue is a type synonym for single characters (Char).

More info: http://learnyouahaskell.com/making-our-own-types-and-typeclasses#type-synonyms

Re 2) Maybe a can have values of Just a or Nothing. Imagine a computation that could go wrong: if it passes, it will return the result wrapped in a Just, if it fails it will return Nothing. In this specific case it would return a String (which btw is a type synonym for [Char] too) wrapped in a Just.

GOA> Just "foo"
Just "foo"
GOA> :type Just "foo"
Just "foo" :: Maybe [Char]

Re 3) See the answer for question number two, but for a single character instead of a list of characters.

GOA> :type Just 'f'
Just 'f' :: Maybe Char

More info: http://en.wikibooks.org/wiki/Haskell/Hierarchical_libraries/Maybe

Upvotes: 5

Antti
Antti

Reputation: 12489

1) With type you can declare a type synonym. So with type T = [Char], T is a type synonym for [Char] (list of Chars; square brackets denote a list in Haskell), meaning that T can be replaced with [Char] anywhere in the program (and vice versa). So don't be concerned about the lack of brackets.

2) Maybe String is a type a value of which may contain a string, or not. Maybe is used like pointers are often used in languages like C: if a function returns a Maybe String, it means that the function either succeeded and returns a string, or it failed and doesn't return a string. You can read more about the Maybe type here.

With type P = (Char, Maybe String) a type synonym P is declared as a synonym for a tuple. The first element of the tuple has the type Char and the second has the type Maybe String.

3) type D = ((Maybe Char), Char), or type D = (Maybe Char, Char) (the inner parentheses are unnecessary), declares a type synonym for a tuple with Maybe Char and Char as inner types.

Upvotes: 3

Related Questions