[Haskell-beginners] Understandig types

Michael Xavier nemesisdesign at gmail.com
Sat Jul 2 21:00:44 CEST 2011


I do agree you should look at the tutorial but to answer your questions, see
below:

1) what's the difference between these two code statements? I'm
> convinced they should be the same
>
> type T = [Char]
> type CurrentValue = Char
>
>
You could think of the type keyword as aliasing an existing type to one that
you name. For instance, T can now be used interchangeably with the [Char]
type, like in a type declaration:

upcase :: [Char] -> [Char]
can be:
upcase :: T -> T

and they would mean the same thing. You do this usually for the sake of
documentation, where the alias you are creating is more readable than that
which it aliases.

[Char] means an array of Chars. String, for example is I believe defined as:
type String = [Char]


> 2) What is Maybe String?
>
>
Maybe String is a datatype that could be 1 of 2 things, a String or Nothing.
Maybe is used a lot of the time for computations that may fail, such as
looking for a needle in a haystack:

findNeedle :: NeedleID -> Haystack -> Maybe Needle

In that example, findNeedle would return the value: Just someneedle OR it
would return Nothing if it could not be found. I encourage you to read up on
the tutorial that David Place submitted for more information.



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

P in this case is a tuple or pair. That's what the parentheses are for. It
means that the first field of the tuple is a Char, the second field of   of
the tuple can either be a string or nothing. Again, this is a type, not a
value or a function.



> 3) What is Maybe Char ?
>
> See above.
-- 
Michael Xavier
http://www.michaelxavier.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110702/ed6cffa2/attachment.htm>


More information about the Beginners mailing list