How to Check whether a file exists and IO[String] vs [String]

Alexandre Weffort Thenorio alethenorio@home.se
Fri, 2 May 2003 23:06:36 +0200


Hello. I am having two problems. I have a program that reads two texts file
and writes a new text with its info. So I have done so that if main file
doesn't exist I just output an error string but my problem is when it comes
to the second file. This file is not exactly needed so if it is not there,
the program should just ignore the file. I tried doing that but if the
second file is there I also get an error saying that it couldn;t find the
file. So is it possible to check whether a file exists or not outputting and
empty string if not or the actual data from it if so without giving an error
and making the program not run??

For example

main = do
  mainfile <- readFile "xxxxx.txt"
  secondfile <- readMyFile "yyyy.txt"
  writeFile "test.txt" mainfile++secondfile


readMyFile file = do
 list <- readFile file
 if list == "" then return "" else return list


Apparently this doesn't work and if file doesn't exist it still gives me an
error.


Another problem I ran into is with IO[String] and [String].

I have a function like

foo = do
 nf <- readFile "xxx.txt"
 return (lines nf)

Then when I try using foo like a [String] in other programs I run into type
error. How can I turn this IO[String] into a [String] so I don't actually
have to open this file in the main and then use it as an argument of another
function running "lines' on main method????

Best Regards

Alex

----- Original Message ----- 
From: "Ron de Bruijn" <rondebruijn@yahoo.com>
To: <haskell-cafe@haskell.org>
Sent: Friday, May 02, 2003 12:56 PM
Subject: Re: Fast Mutable arrays and a general question about IO


>
> > Why?  It's not always necessary to go for maximum
> > speed; do you have a
> > reason for needing it in this case?
>
> Almost any operation in my program works on array's
> and before the program terminates, there have been an
> awful lot of operations on it. If there is an other
> kind of array that mutates and doesn't create a copy
> of it when changing a value and isn't more than 1,5
> times as slow as an array in C++ then that should also
> be ok. The program would run in a scale of hours or
> days. And if it's twice as fast, than I could
> calculate two times as much.
>
> > > Constructing mutable arrays
> > >
> > > newArray :: (MArray a e m, Ix i) => (i, i) -> e ->
> > m (a i e)
> >
> > > Builds a new array, with every element initialised
> > to undefined.
> >
> > No; that's newArray_.  (newArray bounds x) builds an
> > array with every
> > element initialized to x.
> >
> > > Reading and writing mutable arrays
> > >
> > > readArray :: (MArray a e m, Ix i) => a i e -> i ->
> > m e
> > >
> > > Read an element from a mutable array
> > >
> > > writeArray :: (MArray a e m, Ix i) => a i e -> i
> > -> e -> m ()
> > > Write an element in a mutable array
> > >
> > > I see it takes two arguments: a tuple of an index
> > type (I use Int, so
> > > for example (0,1) 0 for the lower bound, 1 for the
> > upper bound.),
> >
> > I assume you're talking about newArray here, right?
> > If so, you're
> > correct.
> >
> > > but the e (according to "GHC documentation" it
> > says the "element
> > > type").  Well the type of my elements should be my
> > own datatype
> > > myDataType, but I can't even get it done to create
> > simple mutable
> > > array's of Int or Bool.
>
> >
> > I think you mean `above'.  In any case, could you
> > show us some code you
> > have that's not working?  That'll make it much
> > easier for us to answer
> > your questions.
> >
>
> Well I don't have really code, because I want to begin
> programming it.
>
> I will restate my goal: I want to be able to read,
> update and create arrays in Haskell of type
> myDataType(suppose my constructor function is Con Int
> String Int on a way that's fast, preferabily the
> fastest method.
>
> > > Is it OK to say that in Haskell any value that is
> > put from an extern
> > > source of the program (reading a file, reading
> > stdin etc.) to a really
> > > pure function, must be in a do notation
> > (essentially being a Monadic
> > > operation)?
> >
> > Yes.  More specifically, they don't need to use
> > do-notation (although if
> > you don't know why not, don't worry about it), but
> > they do have to be
> > monadic.  Specifically, they have to use the IO
> > monad.
>
> I know it's also possible to use (>>=) and a function
> that works on values of the bound variable.
> >
> > > So to put it more concrete: In any Haskell program
> > that does IO there
> > > is always some function that has this form:
> > >
> > > do x<-someSource
> > >
> >
> putBoundVariableToSomeOtherFunctionThatDoesMonadicOperations
> > >      (restOfPureFunctionalProgramForExample x)
> >
> > No.
>
> Why not?
>
> Greets Ron
>
>
>
> __________________________________
> Do you Yahoo!?
> The New Yahoo! Search - Faster. Easier. Bingo.
> http://search.yahoo.com
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>