Function of continuous operation without interruption. Haskell

Consider the function: import Data.Char

convertStringToInt :: String -> Int
convertStringToInt s = convertStringToInt' s 1 0
    where 
          convertStringToInt'  [] _ res    = res
          convertStringToInt' (h:t) m res = convertStringToInt' t (m*10) (res + (((ord h) - 48)*m))

f :: String -> String 
f s = (show . convertStringToInt)s

main = interact f

When I enter "1212", nothing will happen. Just the invitation is still waiting. Why?

0
source share
2 answers

interactfirst reads all the input, then prints the result of the transfer of the entire input to the function. And if the working function does not require only the initial part of the input, the Haskell runtime should be read until the end of the input. Your function consumes the entire string, so this is happening.

Therefore, you will not get the result until you finish typing (i.e. CTRL + D on Unix).

0
source

Haskell . IO . , , , unline:

interact (unlines . map (show . length) . lines)

eachLine :: (String -> String) -> (String -> String)
eachLine f = unlines . map f . lines

:

main = interact (eachLine inputLength)

0

All Articles