交互功能不间断。哈斯克尔 [英] interact function waiting non-stop. Haskell
问题描述
让我们考虑这个函数:
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 - >字符串
fs =(show。convertStringToInt)s
main = interact f
当我输入1212时,它什么也不会发生。只是提示仍在等待。
为什么?
因此,直到输入完成后才能得到结果(例如,Unix中的CTRL + D)。
Let's 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 input "1212" it will happen nothing. Just prompt is still waiting. Why?
interact
reads the whole input first, then prints the result of passing the whole input to the function. And unless the working function needs only an initial portion of the input, the Haskell run time needs to read until the end of input. Your function consumes the whole string, so that is what happens.
Hence, you won't get a result until you finish input (i.e. CTRL+D in Unix).
这篇关于交互功能不间断。哈斯克尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!