[Haskell-cafe] I/O Haskell question
Gwern Branwen
gwern0 at gmail.com
Mon Oct 5 10:38:03 EDT 2009
On Mon, Oct 5, 2009 at 9:48 AM, Maria Boghiu <maria.boghiu at gmail.com> wrote:
> Hey.
>
> I'm trying to configure Xmonad, the window manager.
>
> for this purpose, I'm trying to read the workspaces variable in the code
> below from file.
> As you can see, I do workspaces = readWS where
>
> readWS = do
> l <- readFile "~/bla"
> return l
>
> I get an error saying I am mismatching types IO [String] and [String].
> But I thought that once one does l <- readFile "~/bla", l is the a string
> or, in this case, a String list. If I run that line in prelude it seems to
> be working fine, prints the content of the file "~/bla" on screen (though
> I'm not sure if as a String or as a list of Strings, i.e. [String])
>
> Can anyone help please?
> I've been struggling with this for days...
>
> 94 main = do
> 95 xmonad
> 96 $ defaultConfig {
> 97 manageHook = manageDocks <+> myManageHook <+> manageHook
> defaultConfig,
> 98 layoutHook = avoidStruts $ layoutHook defaultConfig,
> 99 logHook = dynamicLogWithPP $ conkyPP "",
> 100 terminal = "gnome-terminal",
> 101 keys = myKeys,
> 102 workspaces = readWS,
> 103 mouseBindings = myMouseBindings,
> 104 focusedBorderColor = "#008E00",
> 105 borderWidth = 3
> 106 --modMask = mod1Mask, --This is to rebind mod key
> 107 } `additionalKeys`
A quick tip: when GHC gives you a compile error like that, it's because you've made an error where 'what I say' is not 'what I [want to] do'. In this case, you're *saying* 'use readFile, which returns a String', and *doing* 'ok, here, use this [String]'.
The way to know that it's an issue is to find out what type readFile is; in ghci you can do ':type readFile' and it'll tell you String -> IO String. Now you know workspaces needs [String], so you need something which goes 'String -> [String]'. If you don't already know that that is 'lines', then you can ask Hoogle http://www.haskell.org/hoogle/ that question: http://www.haskell.org/hoogle/?hoogle=String+-%3E+[String] and it will give you as the first hit exactly what you need. (Hoogle is great for beginners who don't know the libraries.)
Obviously, this is a real issue; there's no way the compiler could possibly figure out what the right thing to do because there are so many ways to do String->[String] (just consider the specific example of 'lines' - how does it handle Windows v Unix line-endings?). So you have to tell it.
The monad issue with the difference between IO [String] and [String] I will leave to others to explain. :)
--
gwern
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 197 bytes
Desc: OpenPGP digital signature
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20091005/597b78be/signature.bin
More information about the Haskell-Cafe
mailing list