0

I get through the contents of the page curl. Find the right data and formed on their basis the following query. But the data are of type IO [String]. To request simply String. How to convert IO [String] to String?

3 Answers3

5

No way. What you can get there is IO String only.

Take a look at these related questions to understand why.

Community
  • 1
  • 1
0

This is evil (you didn't hear this from me!) but: unsafePerformIO.

I'm just talking about the type system though. Read the documentation carefully if you want to use it!
It's unsafe as the name suggests which means it is up to you to make extra-sure your usage makes sense.

MasterMastic
  • 20,711
  • 12
  • 68
  • 90
  • `unsafePerformIO` is not just evil, it doesn't really exist in the Haskell language – only, GHC has added it to its implementation so you can optimise some low-level stuff (usually, by invoking bindings to C libraries). – leftaroundabout Oct 18 '14 at 14:01
  • @leftaroundabout is it not part of the FFI spec? – alternative Oct 18 '14 at 14:10
  • @alternative: indeed the FFI spec is part of the Haskell2010 report; still I wouldn't consider it part of the Haskell language. Anyway, the `System.IO.Unsafe` module is not mentioned in [the report](https://www.haskell.org/onlinereport/haskell2010/), or am I missing it? – leftaroundabout Oct 18 '14 at 14:18
  • @leftaroundabout I was under the impression that `unsafePerformIO` was originally defined as something in the FFI modules and then later moved to `System.IO.Unsafe`. But I'm not really familiar with the spec so I'm not sure if its defined to be in the FFI anymore or if it ever really was. But I'm pretty sure at one point it was actually a part of the FFI. That or I'm imagining things... – alternative Oct 18 '14 at 14:23
  • @leftaroundabout So I'm not completely insane, at least. In the Haskell 98 era it was defined as part of the FFI, but I can't find it in the 2010 report. http://www.cse.unsw.edu.au/~chak/haskell/ffi/ffi/ffise5.html#x8-230005 – alternative Oct 18 '14 at 14:27
0

You can use unsafePerformIO (which has type IO a -> a and is thus impure), but this is almost certainly not what you are looking for. Instead, you can use fmap or >>= to perform operations on the string:

f :: String -> Int
f = read

io :: IO String
io = getLine

main :: IO ()
main = (f `fmap` io) >>= \x -> print x

The result of fmap and >>= is of course always IO a for some a, in order to maintain referential transparency.