
Hi, I am using Haskell to write Alfred programs, but I have run into an issue I just can not fix. The routine for reading a data file in a application specific location for Alfred never detects the file as non-existant. It then dies on the read. Here is the code: getAlfredCacheFileContents :: String -> IO (String) getAlfredCacheFileContents fileName = do h <- getHomeDirectory fExist <- doesFileExist $ h ++ cacheDirBasic ++ getBundleID ++ "/" ++ fileName if fExist then do contents <- readFile $ h ++ cacheDirBasic ++ getBundleID ++ "/" ++ fileName return contents else return "" I just installed the latest version on my Mac Air. It always does the call to readFile, even when the file doesn't exist. -- Sent with Postbox http://www.getpostbox.com

On Wed, Mar 4, 2015 at 12:38 PM, Richard Guay
fExist <- doesFileExist $ h ++ cacheDirBasic ++ getBundleID ++ "/" ++ fileName if fExist then do contents <- readFile $ h ++ cacheDirBasic ++ getBundleID ++ "/" ++ fileName
First of all, let's refactor that into a let binding, e.g. let pathName = h ++ cacheDirBasic ++ getBundleID ++ "/" ++ fileName fExist <- doesFileExist pathName if fExist then readFile pathName else return "" The advantage of keeping DRY here should be obvious. Defining the same thing in 2 places can lead to both getting out of lockstep. Now as for debugging the actual problem, have you tried the REPL? That is, launch ghci, load the appropriate libraries, and see what happens when you enter doesFileExist "/home/me/myfile" and similarly for readFile. That way you'd rule things out like a file permissions problem, etc. -- Kim-Ee

It's not a file permission problem because it works great when the file exists. But, when the file doesn't exist, it still tries to read the file. It acts like the file is there when it isn't. In ghci, it returns the true boolean when the file is there and a false when it isn't. But, in the program, it is always executing the readFile. Kim-Ee Yeoh wrote:
On Wed, Mar 4, 2015 at 12:38 PM, Richard Guay
mailto:raguay@customct.com> wrote: fExist <- doesFileExist $ h ++ cacheDirBasic ++ getBundleID ++ "/" ++ fileName if fExist then do contents <- readFile $ h ++ cacheDirBasic ++ getBundleID ++ "/" ++ fileName
First of all, let's refactor that into a let binding, e.g.
let pathName = h ++ cacheDirBasic ++ getBundleID ++ "/" ++ fileName fExist <- doesFileExist pathName if fExist then readFile pathName else return ""
The advantage of keeping DRY here should be obvious. Defining the same thing in 2 places can lead to both getting out of lockstep.
Now as for debugging the actual problem, have you tried the REPL? That is, launch ghci, load the appropriate libraries, and see what happens when you enter
doesFileExist "/home/me/myfile"
and similarly for readFile. That way you'd rule things out like a file permissions problem, etc.
-- Kim-Ee _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Richard Guay
It's not a file permission problem because it works great when the file exists. But, when the file doesn't exist, it still tries to read the file.
Is this also the case with the version provided by Kim-Ee Yeoh?
It acts like the file is there when it isn't. In ghci, it returns the true boolean when the file is there and a false when it isn't. But, in the program, it is always executing the readFile.
This is most unlikely a problem of ghc, i.e. I think there is some problem in your programs logic. Can you provide further code possibly restricting it to a minimal breaking example? Also: have you tried running the function in question in ghci? Regards Thomas.

Hi, Sorry. I found my mistake. I figured my newness in the language was showing, so I asked for help. But, to my surprise, I have been modifying the function for the cache file and testing the data file. :Palm Plant:. Sorry for wasting your time. It is working. Richard Thomas Bach wrote:
Richard Guay
writes: It's not a file permission problem because it works great when the file exists. But, when the file doesn't exist, it still tries to read the file.
Is this also the case with the version provided by Kim-Ee Yeoh?
It acts like the file is there when it isn't. In ghci, it returns the true boolean when the file is there and a false when it isn't. But, in the program, it is always executing the readFile.
This is most unlikely a problem of ghc, i.e. I think there is some problem in your programs logic. Can you provide further code possibly restricting it to a minimal breaking example?
Also: have you tried running the function in question in ghci?
Regards
Thomas. _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
participants (3)
-
Kim-Ee Yeoh
-
Richard Guay
-
Thomas Bach