How to read haskell string

I am a complete newbie to Haskell, although I am familiar with the functional paradigm in languages ​​such as Python, F #, Java, C # and C ++ (in a limited way).

Something that just eludes me is IO in haskell. I tried several times, even studied C # and F # between my attempts to get around this.

To be more specific, I mean getting IO without a designation, using a designation, IO becomes trivial. It may be bad practice, but in my free time I like to see if I can do something in one continuous expression. So much bad practice is fun.

Such an expression usually has the form (in a pseudo-haskell):

main = getStdinContentsAsString 
           >>= ParseStringToDataStructureNeeded
           >>= DoSomeComputations 
           >>= ConvertToString 
           >>= putStrLn

I have no problem with the last four parts. One of the reasons I recognized F # was to see if there was something that I could not distract from IO, but as soon as I had a convenient console console. ReadLine () from F #, which returns a plain old line, it was basically a smooth sail.

Which returns me to another in haskell, is again stopped by the IO engine.

I managed (using another question here) to get an int reading from the console and print "Hello World!". that many times

main = (readLn :: IO Int) >>= \n -> mapM_ putStrLn $ replicate n "Hello World!"

I would like to get at least some “general use” method to just read the entire contents of stdin (maybe several lines, so getContents should be a select function) as a string, and then I can process a string using other functions like unlines and then display.

, :

, getContents , ( ).

,

getContents :: IO String

-, IO . ( )

unsafePerformIO :: IO a -> a

- ghc :

* Couldn't match type `[Char]' with `IO (IO b)'
  Expected type: String -> IO b
    Actual type: IO (IO b) -> IO b
* In the second argument of `(>>=)', namely `unsafePerformIO'
  In the expression: getContents >>= unsafePerformIO

, : ;

main = getContents >>= putStrLn

, getContents, IO, , putStrLn

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

- , put.

, -, , "" :

main = getContents >>= (++ " hello") >>= putStrLn

:

Couldn't match type `[]' with `IO'
  Expected type: String -> IO Char
    Actual type: [Char] -> [Char]
* In the second argument of `(>>=)', namely `(++ " hello")'
  In the first argument of `(>>=)', namely
    `getContents >>= (++ " hello")'
  In the expression: getContents >>= (++ " hello") >>= putStrLn

- IO (, , ).

getLine, readLn, getContents, unsafePerformIO, read, fmap .

, , haskell (, , ), , , , . .

:

  • -, ? (99% )

  • , ?

  • stdin ? ( , , , getLine sine, getContents)

!

+4
3

, , , >>=:

(>>=) :: IO a -> (a -> IO b) -> IO b

, "" IO String String; >>= String ():

getContents >>= (\s -> ...)
--                ^ s :: String

getContents >>= (++ " hello") , >>= , IO ..., (++ "hello") :: String -> String.

, return :: a -> IO a :

getContents >>= (return . (++ "hello"))

IO String. stdin, "hello" .

,

getContents >>= (return . (++ "hello")) >>= putStrLn

. , . , return IO >>= ().

return/>>= :

getContents >>= (\s -> putStrLn (s ++ "hello"))

.. , getContents :: IO String, "hello", IO String, putStrLn :: String -> IO (), putStrLn, String -> IO () ( "hello" putStrLn).

, , s :

getContents >>= (putStrLn . (++ "hello"))

IO : , IO ... Haskell. . >>= ; IO something IO somethingelse.

Haskell , (.. ) . , , - , Main.main. , , , Haskell main :: IO (). :

main :: IO ()
main =
    putChar 'H' >>
    putChar 'i' >>
    putChar '\n'

main , print 'H'; print 'i'; print newline. Haskell , .

: Haskell. >>= "" Haskell , ( ) , , .. Haskell; IO, f x >>= f (f ).

+11
  • -, ? (99% )

    .

  • , ?

    An IO String - a String. - , - .
    , , , unsafePerformIO. , Haskell, FFI C- .

  • stdin ?

    main = getContents >>= putStrLn . (++ " hello")
    

    , -: getContents putStrLn. bind operator, .
    (++ " hello"). , , .
    , , :

    main = putStrLn . (++ " hello") =<< getContents
    

    , IO ( ):

    main = getContents >>= pure . (++ " hello") >>= putStrLn
    

    , " putStrLn " hello" , ", " transform getContents " hello" , ":

    main = (++ " hello")<$>getContents >>= putStrLn
    

    .

+4
  • -, ? (99% )

. >>= , a -> m b m .

:

-, ( >>=), - IO.

( , IO ) . , Maybe a, .

Maybe a "". ( Just x) " " ( Nothing).

>>= f :: a -> Maybe b . , f , . / . , >>= , , .

, : . (++ " hello") , ( :().

, , . return ( , ). :

getContents >>= return . (++ " hello") >>= putStrLn

, "", "" () . , putStrLn String -> IO (). , putStrLn - , , () ( , , (): ()).

, , :

getContents >>= putStrLn . (++ " hello")

, , :

a >>= b >>= c >>= d >>= e

, a . bind b. b ( / c ). bind c ..

I/O

/ . " -", , . bind "IO" ( >>= , , -) . , , , .

Maybe revisited

I/O - - , . Maybe. , Maybe :

data Maybe a = Nothing | Just a

Nothing :

  +---+
 /   /|
+---+ +
|   |/
+---+

Just x :

  +---+
 /   /|
+---+ +
| x |/
+---+

. , , , , , Just x x. , . Maybe >>=.

:

instance Monad Maybe where
    return x = Just x

    (>>=) Nothing _ = Nothing
    (>>=) (Just x) f = f x

return, Maybe ", return " ".

bind . , , Nothing.

, x, f, , f . >>= . / " ".

, somthing like:

Just 2 >>= Just . (+5) >>= Just . (*6)

Just 42. ? , a 2. >>= Just . (+2), (Just . (+2)) 2. , Just of Just 2 . , , Just 7.

This package opens again and guesses what it contains 7, is now 7processed by the last function Just . (*6), so the last function will multiply it by 6and wrap it in the field again.

If you, however, write:

Just 2 >>= (+5) >>= Just . (*6)

it will fail. What for? Because, apparently, the second function is not in a good mood and forgot to wrap your gift in a box.

+3
source

Source: https://habr.com/ru/post/1688742/


All Articles