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

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.

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.

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.

!!! 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.

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"]

This question has already been answered. Start a new discussion instead.