openfile :: String - String
The Gofer prelude had a function openfile :: String - String which mapped file path names to strings containing the named files' contents. The Hugs98 Prelude doesn't seem to have anything like that function. Instead, it has things like readFile :: String - IO String Why would I want openfile instead of readFile? The problem in which the need for openfile arose is this: I want to load a binary search tree and a list with words read from a file, and then perform, interactively, several tests comparing the cost of searching the tree with that of searching the list. In addition to performing the tests interactively, I want to separate the cost of searching the list and the tree from the cost of constructing them. In order for the list and the tree to be used in several successive command-line evaluations without being reconstructed each time, they must be named globally. This is no problem with openfile, but readFile forces their names to be local to an IO command. Can anyone suggest a solution? Thanks, --Ham -- Hamilton Richards Jr.Department of Computer Sciences Senior Lecturer Mail Code C0500 512-471-9525 The University of Texas at Austin SHC 434 Austin, Texas 78712-1188 [EMAIL PROTECTED] --
RE: openfile :: String - String
You could try this: --OpenFile.hs module OpenFile where import IOExts openfile :: String - String openfile = unsafePerformIO . readFile -- end of file -Original Message- From: Hamilton Richards [mailto:[EMAIL PROTECTED]] Sent: 26 April 2000 14:04 To: [EMAIL PROTECTED] Subject: openfile :: String - String The Gofer prelude had a function openfile :: String - String which mapped file path names to strings containing the named files' contents. The Hugs98 Prelude doesn't seem to have anything like that function. Instead, it has things like readFile :: String - IO String Why would I want openfile instead of readFile? The problem in which the need for openfile arose is this: I want to load a binary search tree and a list with words read from a file, and then perform, interactively, several tests comparing the cost of searching the tree with that of searching the list. In addition to performing the tests interactively, I want to separate the cost of searching the list and the tree from the cost of constructing them. In order for the list and the tree to be used in several successive command-line evaluations without being reconstructed each time, they must be named globally. This is no problem with openfile, but readFile forces their names to be local to an IO command. Can anyone suggest a solution? Thanks, --Ham -- Hamilton Richards Jr.Department of Computer Sciences Senior Lecturer Mail Code C0500 512-471-9525 The University of Texas at Austin SHC 434 Austin, Texas 78712-1188 [EMAIL PROTECTED] --
Re: openfile :: String - String
On Wed, 26 Apr 2000, Hamilton Richards wrote: The Gofer prelude had a function openfile :: String - String which mapped file path names to strings containing the named files' contents. The Hugs98 Prelude doesn't seem to have anything like that function. Instead, it has things like readFile :: String - IO String Why would I want openfile instead of readFile? The problem in which the need for openfile arose is this: I want to load a binary search tree and a list with words read from a file, and then perform, interactively, several tests comparing the cost of searching the tree with that of searching the list. In addition to performing the tests interactively, I want to separate the cost of searching the list and the tree from the cost of constructing them. In order for the list and the tree to be used in several successive command-line evaluations without being reconstructed each time, they must be named globally. This is no problem with openfile, but readFile forces their names to be local to an IO command. Why is this a problem? After all, the names ARE dependent on IO. Can anyone suggest a solution? I do not understand the problem. Is it something like this you want? parseTree :: String - Tree parseList :: String - List buildTree:: IO Tree buildTree = do f - readFile "thetree" return (parseTree f) buildList:: IO List buildList = do f - readFile "thelist" return (parseList f) test :: IO() test = do tree - buildTree list - buildList dowhateverYouwant_interactive_or_not tree list dowhateverYouwant_interactive_or_not :: Tree - List - IO() /Lars L
Re: openfile :: String - String
Wy not load the list as program? E.g. list1 = ["word1" ,"word2" ,"word3" ] list2 = words "word1 word2 word3" list3 = words "\ \ word1\ \ word2\ \ word3\ \" John Atwood - Lars Lundgren wrote: On Wed, 26 Apr 2000, Hamilton Richards wrote: The Gofer prelude had a function openfile :: String - String which mapped file path names to strings containing the named files' contents. The Hugs98 Prelude doesn't seem to have anything like that function. Instead, it has things like readFile :: String - IO String Why would I want openfile instead of readFile? The problem in which the need for openfile arose is this: I want to load a binary search tree and a list with words read from a file, and then perform, interactively, several tests comparing the cost of searching the tree with that of searching the list. In addition to performing the tests interactively, I want to separate the cost of searching the list and the tree from the cost of constructing them. In order for the list and the tree to be used in several successive command-line evaluations without being reconstructed each time, they must be named globally. This is no problem with openfile, but readFile forces their names to be local to an IO command. Why is this a problem? After all, the names ARE dependent on IO. Can anyone suggest a solution? I do not understand the problem. Is it something like this you want? parseTree :: String - Tree parseList :: String - List buildTree:: IO Tree buildTree = do f - readFile "thetree" return (parseTree f) buildList:: IO List buildList = do f - readFile "thelist" return (parseList f) test :: IO() test = do tree - buildTree list - buildList dowhateverYouwant_interactive_or_not tree list dowhateverYouwant_interactive_or_not :: Tree - List - IO() /Lars L
Re: openfile :: String - String
At 3:18 PM +0200 4/26/00, Lars Lundgren wrote: [...] Is it something like this you want? [...] test :: IO() test = do tree - buildTree list - buildList dowhateverYouwant_interactive_or_not tree list dowhateverYouwant_interactive_or_not :: Tree - List - IO() Sorry, I didn't make my problem clear enough. The reason your suggestion doesn't solve it is that `tree` and `list` are local to `test`, so they can't be used in several separate **command-line** evaluations without being reconstructed each time. My students don't yet know about interactive I/O; even if they did, I know of no way to get separate reduction counts for, say, tree searches and list searches within dowhateverYouwant_interactive_or_not. But thanks anyway for taking the time to answer my ill-posed question. Cheers, --Ham -- Hamilton Richards Jr.Department of Computer Sciences Senior Lecturer Mail Code C0500 512-471-9525 The University of Texas at Austin SHC 434 Austin, Texas 78712-1188 [EMAIL PROTECTED] --
Re: openfile :: String - String
Angus is right on the track. I would only modify it slightly: content_xxx :: String content_xxx = (unsafePerformIO . readFile) "xxx" From Hugs perspective content_xxx is a constant. Your may easily demonstrate it this way: :!echo blah xxx content_xxx == "blah" :!echo Hello xxx content_xxx == "blah" Jan