交互功能不间断。哈斯克尔 [英] interact function waiting non-stop. Haskell

查看:136
本文介绍了交互功能不间断。哈斯克尔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们考虑这个函数:
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时,它什么也不会发生。只是提示仍在等待。
为什么?

解决方案

interact ,然后输出将整个输入传递给函数的结果。除非工作函数只需要输入的初始部分,Haskell运行时需要读取直到输入结束。你的函数会消耗整个字符串,所以会发生什么情况。



因此,直到输入完成后才能得到结果(例如,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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆