Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell IO (String) and String

Tags:

io

input

haskell

I want to write functions and put result to string.

I want function:

read' :: FilePath -> String

I use:

:t readFile
readFile :: FilePath -> IO String

I make:

read' :: IO ()
read' = do
     str <- readFile "/home/shk/workspace/src/test.txt" 
     putStrLn str

I want to ask str is string or not?

We know that:

:t putStrLn
putStrLn :: String -> IO ()

Then why i can't:

read' :: String
read' = do
     str <- readFile "/home/shk/workspace/lxmpp/src/test.txt" 
     str

I get error that:

 Couldn't match expected type `[t0]' with actual type `IO String'
    In the return type of a call of `readFile'
    In a stmt of a 'do' expression:
        str <- readFile "/home/shk/workspace/lxmpp/src/test.txt"
    In the expression:
      do { str <- readFile "/home/shk/workspace/src/test.txt";
           str }

Thank you.

like image 708
0xAX Avatar asked Jul 08 '11 18:07

0xAX


People also ask

What is IO String?

The StringIO module is an in-memory file-like object. This object can be used as input or output to the most function that would expect a standard file object. When the StringIO object is created it is initialized by passing a string to the constructor. If no string is passed the StringIO will start empty.

What is IO () Haskell?

IO is the way how Haskell differentiates between code that is referentially transparent and code that is not. IO a is the type of an IO action that returns an a . You can think of an IO action as a piece of code with some effect on the real world that waits to get executed.

How do you define a String in Haskell?

In Haskell a String is just a list of Char s, indeed type String = [Char] . String is just an "alias" for such list. So all functions you define on lists work on strings, given the elements of that list are Char s.

Is IO pure in Haskell?

Haskell is a pure language and even the I/O system can't break this purity. Being pure means that the result of any function call is fully determined by its arguments. Procedural entities like rand() or getchar() in C, which return different results on each call, are simply impossible to write in Haskell.


1 Answers

Just to quibble a bit more, while the other answers are perfectly correct, I want to emphasize something: Something with the type IO String isn't just a string that the type system won't let you get at directly. It's a computation that performs I/O to get a string for you. Applying readFile to a file path doesn't return a String value any more than putting a steak next to a meat grinder magically turns them into a hamburger.

When you have some code like this:

foo = do let getStr = readFile "input.txt"
         s1 <- getStr
         s2 <- getStr
         -- etc.

That doesn't mean you're "taking the string out of getStr twice". It means you're performing the computation twice and can easily get different results between the two.

like image 152
C. A. McCann Avatar answered Sep 20 '22 23:09

C. A. McCann