[Haskell-beginners] <-
Karl Voelker
ktvoelker at gmail.com
Fri Aug 31 05:54:43 CEST 2012
On Thu, Aug 30, 2012 at 8:00 PM, Patrick Redmond <plredmond at gmail.com>wrote:
> IO actions are given liberal coverage throughout the chapter, however
> it is never mentioned whether the value-extractor syntax (<-) has a
> type or not.
>
What sorts of things have types? Values have types, but x <- getLine is not
a value. I will elaborate.
> main = do
> x <- getLine
> putStrLn $ reverse x
>
>
As mentioned by others, this can be de-sugared into:
main = getLine >>= \x -> putStrLn $ reverse x
> In this little program, getLine has type "IO String" and x has type
> "String". This implies to me that (<-) has type "IO a -> a". However,
> GHCI chokes on ":t (<-)" and Hoogle says it's just a syntactic element
> <http://www.haskell.org/haskellwiki/Keywords#.3C->.
>
You are right about the types of getLine and x. But look at the part of the
de-sugared code that corresponds to x <- getLine:
getLine >>= \x ->
This isn't an expression. In fact, it's nothing valid on its own. Since you
can't evaluate it to a value, don't expect it to have a type.
I guess I don't have a specific question, but I was kind of expecting
> it to be a function with a type because everything seems to be a
> function with a type in Haskell... Thanks for listening!
>
There are other things in Haskell which don't have a type. Here's something
very similar to your example:
foo = let x = 3 in x + x
Does "let x = 3" have a type? Does the "=" in there have a type? (The
answer is no, and the reasons are basically the same.)
-Karl V.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20120830/d80d3e97/attachment.htm>
More information about the Beginners
mailing list