943,878 Members | Top Members by Rank

Ad:
Mar 28th, 2009
0

Haskell - help with using contents read from a file

Expand Post »
I normally program in Java and Haskell to me seems completely alien and weird.

All I want to do is read the contents of a file into a big string then be able to do stuff with that string like split it on newline and store it in a list then split each line in that list with a " " and store them somewhere but I have read and read online without any sort of enlightenment. Apparently I cannot convert an IO String to a String? But then how am I ever going to be able to use the functions I have written that need an input String?

What I have so far...
read :: String -> IO ()
read input = do
	inh <- openFile input ReadMode
	inputString <- hGetContents inh
	putStr (storeReadFile inputString)
	hClose inh 

storeReadFile :: String -> String
storeReadFile input = input

split :: String -> Char -> [String]
split [] delim = [""]
split (c:cs) delim
   | c == delim = "" : rest
   | otherwise = (c : head rest) : tail rest
   where
       rest = split cs delim

I guess I eventually want to be able to say in some main method that you call the read function use the value it gets (ie the inputString into which the contents of the file were read) then split it on the newline and store that in a list ...then do something with that list etc.

How can I do this?


Thanks
Reputation Points: 10
Solved Threads: 0
Light Poster
artemis_f is offline Offline
30 posts
since Aug 2008
Mar 28th, 2009
0

Re: Haskell - help with using contents read from a file

Click to Expand / Collapse  Quote originally posted by artemis_f ...
I normally program in Java and Haskell to me seems completely alien and weird.
That's because it is. I normally program in Haskell and Java seems deficient and limiting. I don't think you really get a benefit out of learning Haskell until you learn more about type classes. But the benefit is huge.

Quote ...
All I want to do is read the contents of a file into a big string
Note that there is a function, readFile :: FilePath -> IO String , that does the opening and closing for you.

Quote ...
then be able to do stuff with that string ... Apparently I cannot convert an IO String to a String? But then how am I ever going to be able to use the functions I have written that need an input String?
He he he he he he he.

If I want to read a file and pass it to a function that needs a String, it's as simple as this:

countSpaces :: String -> Int
countSpaces s = length (filter (== ' ') s)

countSpacesInFile :: String -> IO Int
countSpacesInFile fileName = do
  text <- readFile fileName
  return (countSpaces text)

-- our program counts the number of spaces in input.txt
main :: IO ()
main = do
  n <- countSpacesInFile "input.txt"
  print n

In fact, you were already doing that, no? You were reading the contents of the file and passing it to the function storeReadFile, which happened to be doing nothing to it.
Reputation Points: 231
Solved Threads: 12
Junior Poster
thoughtcoder is offline Offline
139 posts
since Mar 2009
Mar 29th, 2009
0

Re: Haskell - help with using contents read from a file

!!! Thanks so much for getting me started on this.

I guess Haskell is just a different way of thinking - one I am just not used to. I have been writing some more things and just the amount of little things I have to define - like how to count some instead of just using .size like I do in Java etc makes me annoyed. But I am sure there are several benefits to functional programming that I just haven't discovered.
Reputation Points: 10
Solved Threads: 0
Light Poster
artemis_f is offline Offline
30 posts
since Aug 2008
Nov 19th, 2010
0

Haskell Programming

can you please help to function these basic haskell programme as it will be good practice for me in the future thank you!!!!!!!!



1. Define a function prodsum that takes a positive Int value n and returns the product of positive odd integers not exceeding n and the sum of the positive even integers not exceeding n multiplied together, e.g.

Main> prodsum 4
18

2. Define a function rectangle which takes two positive Int values m and n and returns a String value which can be displayed as a m by n rectangle, e.g.

Main> putStr (rectangle 3 4)
****
****
****

3. Define a function flagpattern that takes a positive Int value greater than or equal to five and returns a String that can be displayed as the following `flag' pattern of dimension n, e.g.

Main> putStr (flagpattern 7)
*******
** **
* * * *
* * *
* * * *
** **
*******

4. Define a function swapwords that takes three String values w1, w2 and s, and returns s with all occurrences of the String w1, in the String s, replaced by the String w2, e.g.

Main> swapwords "lamb" "buffalo" "Mary had a little lamb whose fleece"
"Mary had a little buffalo whose fleece"

5. Define a polymorphic function split that is applied to two arguments of types [a] and a, where a is a type on which == is defined, and returns a list of lists that partitions the original list at occurrences
of the second argument, e.g.

Main> split [1,2,3,0,4,5,0,0,7,8,9] 0
[[1,2,3],[4,5],[7,8,9]]
Main> split "Mary had a little lamb" ' '
["mary","had","a","little","lamb"]
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sweetypie is offline Offline
1 posts
since Nov 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Legacy and Other Languages Forum Timeline: can't success in change image extension and size in Matlab
Next Thread in Legacy and Other Languages Forum Timeline: ActionScript 3 Pause the Frame AND Code





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC