[Haskell-beginners] Maybe a and Maybe t

Ivan Uemlianin ivan at llaisdy.com
Sat May 30 16:37:05 EDT 2009


Dear All

I have just started learning Haskell, using the O'Reilly book Real World 
Haskell [1].  I'm finding it a very exciting language, and I'm working 
my way painstakingly through the book (I'm up to the exercises at the 
end of Chapter 3).

I have a query based on some code from the book.  This question has been 
asked on the book web site [2], but no answer.  I have also Googled 
about and scanned the documentation.

So, here is some code (quoted from [3]): two implementations of a 
function to return the second element of a list.  I have commented out 
the type signatures.

-- file: rwh_examples2/ch03/MySecond.hs

-- safeSecond :: [a] -> Maybe a

safeSecond [] = Nothing
safeSecond xs = if null (tail xs)
                then Nothing
                else Just (head (tail xs))

-- tidySecond :: [a] -> Maybe a

tidySecond (_:x:_) = Just x
tidySecond _       = Nothing

My query concerns the inferred types of these functions --- the types 
inferred when the type signatures are commented out.  Here's a ghci session:

Prelude> :load seconds.hs
[1 of 1] Compiling Main             ( seconds.hs, interpreted )
Ok, modules loaded: Main.
*Main>
*Main> :type safeSecond
safeSecond :: [a] -> Maybe a
*Main>
*Main> :type tidySecond
tidySecond :: [t] -> Maybe t
*Main>

Why is safeSecond type [a] -> Maybe a, and tidySecond type [t] -> Maybe 
t?  I mean why does one use "a" and the other "t"?  What does it mean? 

If I change the order of the two functions in the source file, 
safeSecond still uses "a" and tidySecond still uses "t".

Can anyone help, or point me to the right place in the documentation?  
In the meantime, if I find out from elsewhere I'll report back here.

Thanks and best wishes

Ivan

[1]  http://book.realworldhaskell.org/
[2]  
http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html#comment8512
[3]  
http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html#deftypes.morecontrolled


-- 
============================================================
Ivan A. Uemlianin
Speech Technology Research and Development

                    ivan at llaisdy.com
                     www.llaisdy.com
                         llaisdy.wordpress.com
                     www.linkedin.com/in/ivanuemlianin

    "Froh, froh! Wie seine Sonnen, seine Sonnen fliegen"
                     (Schiller, Beethoven)
============================================================



More information about the Beginners mailing list